Skip to content

Commit 6eb5059

Browse files
Some improvements to aarch64 pseudo-assembly
Signed-off-by: Hernan Ponce de Leon <[email protected]>
1 parent 90c3a7b commit 6eb5059

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

dartagnan/src/main/antlr4/LitmusAArch64.g4

+30-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ instruction
7676
| branchRegister
7777
| branchLabel
7878
| fence
79+
| return
80+
| nop
7981
;
8082

8183
mov locals [String rD, int size]
@@ -131,6 +133,14 @@ branchLabel
131133
: label Colon
132134
;
133135

136+
return
137+
: Ret
138+
;
139+
140+
nop
141+
: Nop
142+
;
143+
134144
loadInstruction locals [String mo]
135145
: LDR {$mo = MO_RX;}
136146
| LDAR {$mo = MO_ACQ;}
@@ -244,15 +254,15 @@ register64 returns[String id]
244254
;
245255

246256
register32 returns[String id]
247-
: r = Register32 {$id = $r.text.replace("W","X");}
257+
: r = Register32 {$id = $r.text.replace("W","X").replace("w","x");}
248258
;
249259

250260
location
251261
: Identifier
252262
;
253263

254264
immediate
255-
: Num constant
265+
: Num Hexa? constant
256266
;
257267

258268
label
@@ -265,6 +275,18 @@ assertionValue
265275
| constant
266276
;
267277

278+
Hexa
279+
: '0x'
280+
;
281+
282+
Ret
283+
: 'ret'
284+
;
285+
286+
Nop
287+
: 'nop'
288+
;
289+
268290
Locations
269291
: 'locations'
270292
;
@@ -371,10 +393,16 @@ BitfieldOperator
371393

372394
Register64
373395
: 'X' DigitSequence
396+
| 'x' DigitSequence
397+
| 'XZR' // zero register
398+
| 'xzr' // zero register
374399
;
375400

376401
Register32
377402
: 'W' DigitSequence
403+
| 'w' DigitSequence
404+
| 'WZR' // zero register
405+
| 'wzr' // zero register
378406
;
379407

380408
LitmusLanguage

dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java

+42-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.dat3m.dartagnan.exception.ParsingException;
55
import com.dat3m.dartagnan.expression.Expression;
66
import com.dat3m.dartagnan.expression.ExpressionFactory;
7+
import com.dat3m.dartagnan.expression.ExpressionVisitor;
78
import com.dat3m.dartagnan.expression.integers.IntLiteral;
9+
import com.dat3m.dartagnan.expression.processing.ExprTransformer;
810
import com.dat3m.dartagnan.expression.type.IntegerType;
911
import com.dat3m.dartagnan.expression.type.TypeFactory;
1012
import com.dat3m.dartagnan.parsers.LitmusAArch64BaseVisitor;
@@ -13,10 +15,12 @@
1315
import com.dat3m.dartagnan.program.Program;
1416
import com.dat3m.dartagnan.program.Register;
1517
import com.dat3m.dartagnan.program.event.EventFactory;
18+
import com.dat3m.dartagnan.program.event.RegReader;
1619
import com.dat3m.dartagnan.program.event.arch.StoreExclusive;
1720
import com.dat3m.dartagnan.program.event.core.Label;
1821
import com.dat3m.dartagnan.program.event.core.Load;
1922

23+
import java.math.BigInteger;
2024
import java.util.HashMap;
2125
import java.util.Map;
2226

@@ -53,9 +57,32 @@ public Object visitMain(LitmusAArch64Parser.MainContext ctx) {
5357
visitVariableDeclaratorList(ctx.variableDeclaratorList());
5458
visitInstructionList(ctx.program().instructionList());
5559
VisitorLitmusAssertions.parseAssertions(programBuilder, ctx.assertionList(), ctx.assertionFilter());
56-
return programBuilder.build();
60+
Program prog = programBuilder.build();
61+
replaceXZRRegister(prog);
62+
63+
return prog;
64+
5765
}
5866

67+
/*
68+
The "xzr" register plays a special role in AArhc64:
69+
1. Reading accesses always return the value 0.
70+
2. Discards data when written.
71+
TODO: The below code is a simple fix to guarantee point 1. above.
72+
Point 2. might also be resolved: although we do not prevent writing to xzr,
73+
the value of xzr is never read after the transformation so its value is effectively 0.
74+
However, the exists/forall clauses could still refer to that register and observe a non-zero value.
75+
*/
76+
private void replaceXZRRegister(Program program) {
77+
final ExpressionVisitor<Expression> xzrReplacer = new ExprTransformer() {
78+
@Override
79+
public Expression visitRegister(Register reg) {
80+
return reg.getName().equals("xzr") ? expressions.makeGeneralZero(reg.getType()) : reg;
81+
}
82+
};
83+
program.getThreadEvents(RegReader.class)
84+
.forEach(e -> e.transformExpressions(xzrReplacer));
85+
}
5986

6087
// ----------------------------------------------------------------------------------------------------------------
6188
// Variable declarator list, e.g., { 0:EAX=0; 1:EAX=1; x=2; }
@@ -211,6 +238,12 @@ public Object visitFence(LitmusAArch64Parser.FenceContext ctx) {
211238
return programBuilder.addChild(mainThread, EventFactory.newFenceOpt(ctx.Fence().getText(), ctx.opt));
212239
}
213240

241+
@Override
242+
public Object visitReturn(LitmusAArch64Parser.ReturnContext ctx) {
243+
Label end = programBuilder.getEndOfThreadLabel(mainThread);
244+
return programBuilder.addChild(mainThread, EventFactory.newGoto(end));
245+
}
246+
214247
@Override
215248
public Expression visitExpressionRegister64(LitmusAArch64Parser.ExpressionRegister64Context ctx) {
216249
Expression expr = programBuilder.getOrNewRegister(mainThread, ctx.register64().id, archType);
@@ -255,4 +288,12 @@ private Register visitOffset(LitmusAArch64Parser.OffsetContext ctx, Register reg
255288
programBuilder.addChild(mainThread, EventFactory.newLocal(result, expressions.makeAdd(register, expr)));
256289
return result;
257290
}
291+
292+
@Override
293+
public Expression visitImmediate(LitmusAArch64Parser.ImmediateContext ctx) {
294+
final int radix = ctx.Hexa() != null ? 16 : 10;
295+
BigInteger value = new BigInteger(ctx.constant().getText(), radix);
296+
return expressions.makeValue(value, archType);
297+
}
298+
258299
}

dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep
9696
ComplexBlockSplitting.newInstance(),
9797
BranchReordering.fromConfig(config),
9898
Simplifier.fromConfig(config)
99-
), Target.FUNCTIONS, true
99+
), Target.ALL, true
100100
),
101101
ProgramProcessor.fromFunctionProcessor(NormalizeLoops.newInstance(), Target.ALL, true),
102102
RegisterDecomposition.newInstance(),

0 commit comments

Comments
 (0)