Skip to content

Commit 8aec602

Browse files
lcb: Move operand format fields method to llvm record
1 parent 1b7325f commit 8aec602

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

vadl/main/vadl/lcb/codegen/expansion/PseudoExpansionCodeGenerator.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.stream.Collectors;
33-
import java.util.stream.Stream;
3433
import vadl.cppCodeGen.FunctionCodeGenerator;
3534
import vadl.cppCodeGen.SymbolTable;
3635
import vadl.cppCodeGen.context.CGenContext;
@@ -43,8 +42,6 @@
4342
import vadl.gcb.valuetypes.TargetName;
4443
import vadl.gcb.valuetypes.VariantKind;
4544
import vadl.lcb.passes.llvmLowering.domain.LlvmLoweringRecord;
46-
import vadl.lcb.passes.llvmLowering.tablegen.model.tableGenOperand.TableGenInstructionOperand;
47-
import vadl.lcb.passes.llvmLowering.tablegen.model.tableGenOperand.tableGenParameter.TableGenParameterTypeAndName;
4845
import vadl.lcb.passes.relocation.GenerateLinkerComponentsPass;
4946
import vadl.utils.Pair;
5047
import vadl.viam.Format;
@@ -423,12 +420,9 @@ private List<Pair<Format.Field, ExpressionNode>> reorderParameters(
423420

424421
var llvmRecord = ensureNonNull(machineInstructionRecords.get(instruction),
425422
() -> Diagnostic.error("Cannot find llvmRecord for instruction used in pseudo instruction",
426-
instruction.sourceLocation()));
423+
instruction.sourceLocation()));
427424

428-
var order = Stream.concat(
429-
llvmRecord.info().outputs().stream().map(op -> getFormatField(op, instruction)),
430-
llvmRecord.info().inputs().stream().map(op -> getFormatField(op, instruction))
431-
).toList();
425+
var order = llvmRecord.info().outputInputOperandsFormatFields();
432426

433427
for (var item : order) {
434428
var l = ensureNonNull(lookup.get(item),
@@ -440,20 +434,6 @@ private List<Pair<Format.Field, ExpressionNode>> reorderParameters(
440434
return result;
441435
}
442436

443-
private Format.Field getFormatField(TableGenInstructionOperand operand, Instruction instruction) {
444-
for (var field : instruction.format().fields()) {
445-
if (field.identifier.simpleName().equals(getParameterName(operand))) {
446-
return field;
447-
}
448-
}
449-
throw Diagnostic.error("Cannot find format's field in pseudo instruction ",
450-
instruction.sourceLocation()).build();
451-
}
452-
453-
private String getParameterName(TableGenInstructionOperand operand) {
454-
return ((TableGenParameterTypeAndName) operand.parameter()).name();
455-
}
456-
457437
@Override
458438
public String genFunctionSignature() {
459439
return "std::vector<MCInst> %sMCInstExpander::%s_%s".formatted(targetName.value(),

vadl/main/vadl/lcb/passes/llvmLowering/LlvmLoweringPass.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import vadl.pass.Pass;
6969
import vadl.pass.PassName;
7070
import vadl.pass.PassResults;
71+
import vadl.utils.SourceLocation;
7172
import vadl.viam.Abi;
7273
import vadl.viam.Format;
7374
import vadl.viam.Instruction;
@@ -113,6 +114,33 @@ public int findInputIndex(Format.Field field) {
113114
throw Diagnostic.error("Cannot find field in inputs.", field.sourceLocation()).build();
114115
}
115116

117+
/**
118+
* Return the concatenation of {@link #outputs} and {@link #inputs} in that order.
119+
*/
120+
public List<TableGenInstructionOperand> outputInputOperands() {
121+
var result = new ArrayList<>(outputs);
122+
result.addAll(inputs);
123+
return result;
124+
}
125+
126+
/**
127+
* Return the format fields of the {@link #outputs} and {@link #inputs}.
128+
*
129+
* @throws Diagnostic if any operand is not a {@link ReferencesFormatField}.
130+
*/
131+
public List<Format.Field> outputInputOperandsFormatFields() {
132+
var result = new ArrayList<Format.Field>();
133+
for (var operand : outputInputOperands()) {
134+
if (operand instanceof ReferencesFormatField x) {
135+
result.add(x.formatField());
136+
} else {
137+
throw Diagnostic.error("Expected to find format field on operand.",
138+
SourceLocation.INVALID_SOURCE_LOCATION).build();
139+
}
140+
}
141+
return result;
142+
}
143+
116144
public BaseInstructionInfo withFlags(LlvmLoweringPass.Flags newFlags) {
117145
return new BaseInstructionInfo(inputs, outputs, newFlags, uses, defs);
118146
}

0 commit comments

Comments
 (0)