Skip to content

Commit ed85e5a

Browse files
authored
Merge pull request #35 from OpenVADL/feature/abi
frontend: Implemented initial lowering of ABI
2 parents 0feb993 + f3b41c3 commit ed85e5a

File tree

64 files changed

+1059
-417
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1059
-417
lines changed

vadl/main/resources/templates/lcb/llvm/lib/Target/InstrInfo.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def ADJCALLSTACKUP : Instruction
9393
let Uses = [ [(${stackPointerRegister})] ]; // stack pointer
9494
}
9595

96-
def RESERVED_PSEUDO_RET : Instruction
96+
def [(${returnInstruction})] : Instruction
9797
{
9898
let Namespace = "[(${namespace})]";
9999
let InOperandList = (ins);
@@ -116,7 +116,7 @@ def RESERVED_PSEUDO_RET : Instruction
116116
* 'target_call', which marks a function call.
117117
* It will be later expanded into the defined calling sequence during code emission.
118118
*/
119-
def RESERVED_PSEUDO_CALL : Instruction
119+
def [(${callInstruction})] : Instruction
120120
{
121121
let Namespace = "[(${namespace})]";
122122
let InOperandList = (ins bare_symbol:$addr);
@@ -135,8 +135,8 @@ def RESERVED_PSEUDO_CALL : Instruction
135135
}
136136

137137
/* Match the call sequence for global and external symbols */
138-
def : Pat<(target_call tglobaladdr:$func), (RESERVED_PSEUDO_CALL tglobaladdr:$func)>;
139-
def : Pat<(target_call texternalsym:$func), (RESERVED_PSEUDO_CALL texternalsym:$func)>;
138+
def : Pat<(target_call tglobaladdr:$func), ([(${callInstruction})] tglobaladdr:$func)>;
139+
def : Pat<(target_call texternalsym:$func), ([(${callInstruction})] texternalsym:$func)>;
140140

141141
def SDT_[(${namespace})]SelectCC : SDTypeProfile<1, 5, [SDTCisSameAs<1, 2>,
142142
SDTCisSameAs<0, 4>,

vadl/main/vadl/ast/AstDumper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ public Void visit(UsingDefinition definition) {
301301
return null;
302302
}
303303

304+
@Override
305+
public Void visit(AbiPseudoInstructionDefinition definition) {
306+
dumpNode(definition);
307+
return null;
308+
}
309+
304310
@Override
305311
public Void visit(FunctionDefinition definition) {
306312
dumpNode(definition);
@@ -486,7 +492,6 @@ public Void visit(AbiSequenceDefinition definition) {
486492
@Override
487493
public Void visit(SpecialPurposeRegisterDefinition definition) {
488494
dumpNode(definition);
489-
dumpChildren(definition.calls);
490495
return null;
491496
}
492497

@@ -495,7 +500,9 @@ public Void visit(MicroProcessorDefinition definition) {
495500
dumpNode(definition);
496501
dumpChildren(definition.id);
497502
dumpChildren(definition.implementedIsas.stream().map(Node.class::cast).toList());
498-
dumpChildren((Node) definition.abi);
503+
if (definition.abi != null) {
504+
dumpChildren((Node) definition.abi);
505+
}
499506
dumpChildren(definition.definitions);
500507
return null;
501508
}

vadl/main/vadl/ast/Definition.java

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import vadl.types.asmTypes.AsmType;
3333
import vadl.utils.SourceLocation;
3434
import vadl.utils.WithSourceLocation;
35+
import vadl.viam.PseudoInstruction;
3536
import vadl.viam.asm.AsmToken;
3637

3738
/**
@@ -159,6 +160,8 @@ interface DefinitionVisitor<R> {
159160
R visit(StageDefinition definition);
160161

161162
R visit(UsingDefinition definition);
163+
164+
R visit(AbiPseudoInstructionDefinition abiPseudoInstructionDefinition);
162165
}
163166

164167
/**
@@ -3325,6 +3328,77 @@ public Identifier identifier() {
33253328
}
33263329
}
33273330

3331+
/**
3332+
* The compiler generator requires a few {@link PseudoInstruction}. Those need to be defined
3333+
* in the ABI. They are distinguised with the {@link AbiPseudoInstructionDefinition#kind}
3334+
* property.
3335+
*/
3336+
class AbiPseudoInstructionDefinition extends Definition {
3337+
3338+
Kind kind;
3339+
IdentifierOrPlaceholder target;
3340+
SourceLocation loc;
3341+
3342+
AbiPseudoInstructionDefinition(Kind kind, IdentifierOrPlaceholder target, SourceLocation loc) {
3343+
this.kind = kind;
3344+
this.target = target;
3345+
this.loc = loc;
3346+
}
3347+
3348+
@Override
3349+
<R> R accept(DefinitionVisitor<R> visitor) {
3350+
return visitor.visit(this);
3351+
}
3352+
3353+
@Override
3354+
SourceLocation location() {
3355+
return loc;
3356+
}
3357+
3358+
@Override
3359+
SyntaxType syntaxType() {
3360+
return BasicSyntaxType.INVALID;
3361+
}
3362+
3363+
@Override
3364+
void prettyPrint(int indent, StringBuilder builder) {
3365+
annotations.prettyPrint(indent, builder);
3366+
builder.append(prettyIndentString(indent));
3367+
builder.append(kind.keyword);
3368+
builder.append(" = ");
3369+
builder.append(target).append("}\n");
3370+
}
3371+
3372+
@Override
3373+
public boolean equals(Object o) {
3374+
if (this == o) {
3375+
return true;
3376+
}
3377+
if (o == null || getClass() != o.getClass()) {
3378+
return false;
3379+
}
3380+
AbiPseudoInstructionDefinition that = (AbiPseudoInstructionDefinition) o;
3381+
return kind == that.kind && target.equals(that.target);
3382+
}
3383+
3384+
@Override
3385+
public int hashCode() {
3386+
return Objects.hash(kind, target);
3387+
}
3388+
3389+
enum Kind {
3390+
RETURN("return"),
3391+
CALL("call"),
3392+
LOCAL_ADDRESS_LOAD("local address load");
3393+
3394+
private final String keyword;
3395+
3396+
Kind(String keyword) {
3397+
this.keyword = keyword;
3398+
}
3399+
}
3400+
}
3401+
33283402
class AbiSequenceDefinition extends Definition {
33293403

33303404
SeqKind kind;
@@ -3401,13 +3475,14 @@ enum SeqKind {
34013475
class SpecialPurposeRegisterDefinition extends Definition {
34023476

34033477
Purpose purpose;
3404-
List<SequenceCallExpr> calls;
3478+
Identifier aliasName;
34053479
SourceLocation loc;
34063480

3407-
SpecialPurposeRegisterDefinition(Purpose purpose, List<SequenceCallExpr> calls,
3481+
SpecialPurposeRegisterDefinition(Purpose purpose,
3482+
Identifier aliasName,
34083483
SourceLocation loc) {
34093484
this.purpose = purpose;
3410-
this.calls = calls;
3485+
this.aliasName = aliasName;
34113486
this.loc = loc;
34123487
}
34133488

@@ -3432,20 +3507,7 @@ void prettyPrint(int indent, StringBuilder builder) {
34323507
builder.append(prettyIndentString(indent));
34333508
builder.append(purpose.keywords);
34343509
builder.append(" = ");
3435-
if (calls.size() == 1) {
3436-
calls.get(0).prettyPrint(0, builder);
3437-
} else {
3438-
builder.append("[");
3439-
var isFirst = true;
3440-
for (SequenceCallExpr call : calls) {
3441-
if (!isFirst) {
3442-
builder.append(", ");
3443-
}
3444-
isFirst = false;
3445-
call.prettyPrint(0, builder);
3446-
}
3447-
builder.append("]");
3448-
}
3510+
builder.append(aliasName);
34493511
}
34503512

34513513
@Override
@@ -3457,12 +3519,12 @@ public boolean equals(Object o) {
34573519
return false;
34583520
}
34593521
SpecialPurposeRegisterDefinition that = (SpecialPurposeRegisterDefinition) o;
3460-
return purpose == that.purpose && Objects.equals(calls, that.calls);
3522+
return purpose == that.purpose && aliasName.equals(that.aliasName);
34613523
}
34623524

34633525
@Override
34643526
public int hashCode() {
3465-
return Objects.hash(purpose, calls);
3527+
return Objects.hash(purpose, aliasName);
34663528
}
34673529

34683530
enum Purpose {
@@ -3471,6 +3533,7 @@ enum Purpose {
34713533
STACK_POINTER("stack pointer"),
34723534
GLOBAL_POINTER("global pointer"),
34733535
FRAME_POINTER("frame pointer"),
3536+
THREAD_POINTER("thread pointer"),
34743537
FUNCTION_ARGUMENT("function argument"),
34753538
CALLER_SAVED("caller saved"),
34763539
CALLEE_SAVED("callee saved");
@@ -3486,6 +3549,7 @@ enum Purpose {
34863549
class MicroProcessorDefinition extends Definition implements IdentifiableNode {
34873550
Identifier id;
34883551
List<IsId> implementedIsas;
3552+
@Nullable
34893553
IsId abi;
34903554
List<Definition> definitions;
34913555
SourceLocation loc;
@@ -3494,7 +3558,7 @@ class MicroProcessorDefinition extends Definition implements IdentifiableNode {
34943558
@Nullable
34953559
ApplicationBinaryInterfaceDefinition abiNode;
34963560

3497-
MicroProcessorDefinition(Identifier id, List<IsId> implementedIsas, IsId abi,
3561+
MicroProcessorDefinition(Identifier id, List<IsId> implementedIsas, @Nullable IsId abi,
34983562
List<Definition> definitions, SourceLocation loc) {
34993563
this.id = id;
35003564
this.implementedIsas = implementedIsas;
@@ -3538,7 +3602,9 @@ void prettyPrint(int indent, StringBuilder builder) {
35383602
implementedIsa.prettyPrint(0, builder);
35393603
}
35403604
builder.append(" with ");
3541-
abi.prettyPrint(0, builder);
3605+
if (abi != null) {
3606+
abi.prettyPrint(0, builder);
3607+
}
35423608
builder.append(" = {\n");
35433609
for (Definition definition : definitions) {
35443610
definition.prettyPrint(indent + 1, builder);

vadl/main/vadl/ast/MacroExpander.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,14 @@ public Definition visit(UsingDefinition definition) {
543543
.withAnnotations(expandAnnotations(definition.annotations));
544544
}
545545

546+
@Override
547+
public Definition visit(AbiPseudoInstructionDefinition definition) {
548+
return new AbiPseudoInstructionDefinition(definition.kind,
549+
resolvePlaceholderOrIdentifier(definition.target),
550+
copyLoc(definition.loc)
551+
).withAnnotations(expandAnnotations(definition.annotations));
552+
}
553+
546554
@Override
547555
public Definition visit(FunctionDefinition definition) {
548556
var name = resolvePlaceholderOrIdentifier(definition.name);
@@ -705,7 +713,7 @@ public Definition visit(AbiSequenceDefinition definition) {
705713
@Override
706714
public Definition visit(SpecialPurposeRegisterDefinition definition) {
707715
return new SpecialPurposeRegisterDefinition(
708-
definition.purpose, definition.calls, copyLoc(definition.loc)
716+
definition.purpose, definition.aliasName, copyLoc(definition.loc)
709717
).withAnnotations(expandAnnotations(definition.annotations));
710718
}
711719

vadl/main/vadl/ast/ModelRemover.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public Definition visit(UsingDefinition definition) {
9595
return definition;
9696
}
9797

98+
@Override
99+
public Definition visit(AbiPseudoInstructionDefinition definition) {
100+
return definition;
101+
}
102+
98103
@Override
99104
public Definition visit(FunctionDefinition definition) {
100105
return definition;

vadl/main/vadl/ast/SymbolTable.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,10 @@ void collectSymbols(SymbolTable symbols, Definition definition) {
539539
collectSymbols(mip.symbolTable, def);
540540
}
541541
} else if (definition instanceof SpecialPurposeRegisterDefinition specialPurposeRegister) {
542-
for (SequenceCallExpr call : specialPurposeRegister.calls) {
543-
collectSymbols(symbols, call);
544-
}
542+
specialPurposeRegister.symbolTable = symbols.createChild();
543+
} else if (definition instanceof AbiPseudoInstructionDefinition
544+
abiPseudoInstructionDefinition) {
545+
abiPseudoInstructionDefinition.symbolTable = symbols.createChild();
545546
} else if (definition instanceof CpuFunctionDefinition cpuFunction) {
546547
collectSymbols(symbols, cpuFunction.expr);
547548
} else if (definition instanceof CpuProcessDefinition cpuProcess) {
@@ -977,19 +978,17 @@ static void resolveSymbols(Definition definition) {
977978
mip.implementedIsaNodes.add(isa);
978979
}
979980
}
980-
var abi = mip.symbolTable()
981-
.requireAs((Identifier) mip.abi, ApplicationBinaryInterfaceDefinition.class);
982-
if (abi != null) {
983-
mip.abiNode = abi;
984-
mip.symbolTable().extendBy(abi.symbolTable());
985-
for (Definition def : mip.definitions) {
986-
resolveSymbols(def);
981+
if (mip.abi != null) {
982+
var abi = mip.symbolTable()
983+
.requireAs((Identifier) mip.abi, ApplicationBinaryInterfaceDefinition.class);
984+
if (abi != null) {
985+
mip.abiNode = abi;
986+
mip.symbolTable().extendBy(abi.symbolTable());
987+
for (Definition def : mip.definitions) {
988+
resolveSymbols(def);
989+
}
987990
}
988991
}
989-
} else if (definition instanceof SpecialPurposeRegisterDefinition specialPurposeRegister) {
990-
for (SequenceCallExpr call : specialPurposeRegister.calls) {
991-
resolveSymbols(call);
992-
}
993992
} else if (definition instanceof CpuFunctionDefinition cpuFunction) {
994993
resolveSymbols(cpuFunction.expr);
995994
} else if (definition instanceof CpuProcessDefinition cpuProcess) {

0 commit comments

Comments
 (0)