|
25 | 25 | import java.util.Arrays; |
26 | 26 | import java.util.HashMap; |
27 | 27 | import java.util.Iterator; |
| 28 | +import java.util.LinkedList; |
28 | 29 | import java.util.List; |
29 | 30 | import java.util.Map; |
30 | 31 | import java.util.Objects; |
31 | 32 | import java.util.regex.Pattern; |
32 | 33 | import java.util.stream.Collectors; |
33 | | -import java.util.stream.Stream; |
34 | 34 | import javax.annotation.Nullable; |
35 | 35 | import vadl.error.Diagnostic; |
36 | 36 | import vadl.error.DiagnosticList; |
@@ -681,21 +681,25 @@ static Node expandNode(Parser parser, Node node) { |
681 | 681 | * Assembly definitions can be written with multiple identifiers to be bound to multiple |
682 | 682 | * (pseudo) instructions. However, for correct further processing they need to be expanded into |
683 | 683 | * multiple definitions. |
| 684 | + * The input requires a linked list because otherwise performance would degredate. |
684 | 685 | * |
685 | 686 | * @param isaDefs to be expanded. |
686 | 687 | * @return returns the original isaDefs with the Assemblies expanded. |
687 | 688 | */ |
688 | | - static List<Definition> expandAssemblyDefinitionsInIsa(List<Definition> isaDefs) { |
689 | | - // FIXME: We could modify the list inplace here for performance |
690 | | - return isaDefs.stream() |
691 | | - .flatMap(def -> { |
692 | | - if (def instanceof AssemblyDefinition assembly) { |
693 | | - return new MacroExpander(Map.of(), Map.of(), def.location().fullExpandedFromStack()) |
694 | | - .expandAssemblies(assembly).stream(); |
695 | | - } |
696 | | - return Stream.of(def); |
697 | | - }) |
698 | | - .collect(Collectors.toCollection(ArrayList::new)); |
| 689 | + static LinkedList<Definition> expandAssemblyDefinitionsInIsa(LinkedList<Definition> isaDefs) { |
| 690 | + for (var iter = isaDefs.listIterator(); iter.hasNext(); ) { |
| 691 | + var def = iter.next(); |
| 692 | + if (!(def instanceof AssemblyDefinition assembly) || assembly.identifiers.size() == 1) { |
| 693 | + continue; |
| 694 | + } |
| 695 | + |
| 696 | + var expanded = new MacroExpander(Map.of(), Map.of(), def.location().fullExpandedFromStack()) |
| 697 | + .expandAssemblies(assembly); |
| 698 | + iter.set(expanded.getFirst()); |
| 699 | + expanded.subList(1, expanded.size()).forEach(iter::add); |
| 700 | + } |
| 701 | + |
| 702 | + return isaDefs; |
699 | 703 | } |
700 | 704 |
|
701 | 705 | /** |
|
0 commit comments