Skip to content

Commit 345ffd6

Browse files
committed
frontend: Optimize assembly definition expansion
1 parent 472b9b0 commit 345ffd6

4 files changed

Lines changed: 22 additions & 15 deletions

File tree

vadl/main/vadl/ast/MacroExpander.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public List<AssemblyDefinition> expandAssemblies(AssemblyDefinition definition)
215215
result.add(new AssemblyDefinition(
216216
new ArrayList<>(List.of(identifier)),
217217
expandExpr(definition.expr),
218-
copyLoc(definition.loc)
218+
definition.loc
219219
));
220220
}
221221
return result;

vadl/main/vadl/ast/Parser.frame

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import vadl.utils.VirtualFileSystem;
3232
import vadl.error.Diagnostic;
3333
import static vadl.error.Diagnostic.error;
3434

35+
@SuppressWarnings("NonApiType") // We know what we are doing, the implementation matters sometimes.
3536
public class Parser {
3637

3738
/**

vadl/main/vadl/ast/ParserUtils.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import java.util.Arrays;
2626
import java.util.HashMap;
2727
import java.util.Iterator;
28+
import java.util.LinkedList;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Objects;
3132
import java.util.regex.Pattern;
3233
import java.util.stream.Collectors;
33-
import java.util.stream.Stream;
3434
import javax.annotation.Nullable;
3535
import vadl.error.Diagnostic;
3636
import vadl.error.DiagnosticList;
@@ -681,21 +681,26 @@ static Node expandNode(Parser parser, Node node) {
681681
* Assembly definitions can be written with multiple identifiers to be bound to multiple
682682
* (pseudo) instructions. However, for correct further processing they need to be expanded into
683683
* multiple definitions.
684+
* The input requires a linked list because otherwise performance would degredate.
684685
*
685686
* @param isaDefs to be expanded.
686687
* @return returns the original isaDefs with the Assemblies expanded.
687688
*/
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+
@SuppressWarnings("NonApiType")
690+
static LinkedList<Definition> expandAssemblyDefinitionsInIsa(LinkedList<Definition> isaDefs) {
691+
for (var iter = isaDefs.listIterator(); iter.hasNext(); ) {
692+
var def = iter.next();
693+
if (!(def instanceof AssemblyDefinition assembly) || assembly.identifiers.size() == 1) {
694+
continue;
695+
}
696+
697+
var expanded = new MacroExpander(Map.of(), Map.of(), def.location().fullExpandedFromStack())
698+
.expandAssemblies(assembly);
699+
iter.set(expanded.getFirst());
700+
expanded.subList(1, expanded.size()).forEach(iter::add);
701+
}
702+
703+
return isaDefs;
699704
}
700705

701706
/**

vadl/main/vadl/ast/vadl.ATG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import java.util.ArrayDeque;
2020
import java.util.Deque;
2121
import java.util.HashMap;
2222
import java.util.List;
23+
import java.util.LinkedList;
2324
import java.util.Map;
2425
import java.util.Objects;
2526
import javax.annotation.Nullable;
@@ -440,12 +441,12 @@ The #commonDefinitionList production rule lists (beside macro definitions) all c
440441
readMacroSymbols(macroTable, extendingIsa);
441442
}); .)
442443
SYM_EQ SYM_BRACE_OPEN
443-
isaDefinitionList<.out List<Definition> definitions.> (. macroTable = macroTable.pop(); .)
444+
isaDefinitionList<.out LinkedList<Definition> definitions.> (. macroTable = macroTable.pop(); .)
444445
SYM_BRACE_CLOSE (. isaDef = new InstructionSetDefinition(identifier, extending, ParserUtils.expandAssemblyDefinitionsInIsa(definitions), startLocation.join(lastTokenLoc()));
445446
macroTable.defineSymbol(isaDef); .)
446447
.
447448

448-
isaDefinitionList<.out List<Definition> definitions.> (. definitions = new ArrayList<>(); .)
449+
isaDefinitionList<.out LinkedList<Definition> definitions.> (. definitions = new LinkedList<>(); .)
449450
= {
450451
annotations<.out List<AnnotationDefinition> annotations.>
451452
isaDefinition<out Definition def> (. def.withAnnotations(annotations); definitions.add(def); .)

0 commit comments

Comments
 (0)