Skip to content

Commit 3b17afa

Browse files
authored
lcb: Relocation modifiers (#500)
* spec: Add relocation `syntax` to risc-v * ast: Added new annotation * spec: Dropped backslash because it is not supported * chore: Updated tests because obsolete relocattions were deleted * ast: Pass on the annotation's string value * lcb: Implemented the printing of relocation via annotation * lcb: Add pseudo instruction for loading addresses * lcb: Generated case where we print arbitrary expressions for field access functions * lcb: Working on pseudo inst expansion for address loading * lcb: Fixed CheckStyle * lcb: Updated test * lcb: Fixed test
1 parent c4bb167 commit 3b17afa

File tree

19 files changed

+597
-133
lines changed

19 files changed

+597
-133
lines changed

vadl/main/resources/templates/lcb/llvm/lib/Target/MCTargetDesc/AsmUtils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ std::string AsmUtils::formatExpr(const MCExpr *Expr, uint8_t Radix, const MCAsmI
222222
llvm_unreachable("Invalid expression kind!");
223223
}
224224

225+
226+
std::string AsmUtils::FormatModifierString(const [(${namespace})]MCExpr::VariantKind VariantKind)
227+
{
228+
[# th:each="fm : ${formatModifiers}" ]
229+
if(VariantKind == [(${namespace})]MCExpr::VariantKind::[(${fm.variantKind})]) {
230+
return "[(${fm.formatString})]";
231+
}
232+
[/]
233+
234+
return "unknown";
235+
}
236+
225237
std::string AsmUtils::FormatModifier(const [(${namespace})]MCExpr::VariantKind VariantKind)
226238
{
227239
[# th:each="fm : ${formatModifiers}" ]

vadl/main/resources/templates/lcb/llvm/lib/Target/MCTargetDesc/AsmUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace llvm
2626
static std::string formatImm(MCOperand Op, uint8_t Radix, const MCAsmInfo *MAI);
2727
static std::string formatImm(int64_t value, uint8_t Radix, const MCAsmInfo *MAI);
2828
static std::string formatExpr(const MCExpr *Expr, uint8_t Radix, const MCAsmInfo *MAI);
29+
static std::string FormatModifierString(const [(${namespace})]MCExpr::VariantKind VariantKind);
2930
static std::string FormatModifier(const [(${namespace})]MCExpr::VariantKind VariantKind);
3031
static std::string FormatModifier(const MCSymbolRefExpr::VariantKind VariantKind);
3132
static bool MatchRegNo(StringRef Reg, unsigned &RegNo);

vadl/main/resources/templates/lcb/llvm/lib/Target/MCTargetDesc/TargetMCExpr.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "llvm/MC/MCStreamer.h"
1010
#include "llvm/MC/MCValue.h"
1111
#include "llvm/Support/ErrorHandling.h"
12+
#include <sstream>
13+
#include <iostream>
14+
#include <format>
15+
#include <vector>
1216

1317
using namespace llvm;
1418

@@ -28,6 +32,33 @@ void [(${namespace})]MCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) co
2832
OS << format(10, MAI);
2933
}
3034

35+
template<typename T>
36+
std::string to_string_any(const T& value) {
37+
std::ostringstream oss;
38+
oss << value;
39+
return oss.str();
40+
}
41+
42+
// Remove when llvm supports C++ 20.
43+
template<typename... Args>
44+
std::string custom_format(const std::string& fmt, Args&&... args) {
45+
// Convert all arguments to strings
46+
std::vector<std::string> values{ to_string_any(std::forward<Args>(args))... };
47+
48+
std::string result;
49+
size_t argIndex = 0;
50+
51+
for (size_t i = 0; i < fmt.size(); ++i) {
52+
if (fmt[i] == '{' && i + 1 < fmt.size() && fmt[i + 1] == '}' && argIndex < values.size()) {
53+
result += values[argIndex++];
54+
++i; // skip the '}'
55+
} else {
56+
result += fmt[i];
57+
}
58+
}
59+
return result;
60+
}
61+
3162
std::string [(${namespace})]MCExpr::format(uint8_t Radix, const MCAsmInfo *MAI) const
3263
{
3364
bool HasVariant = (Kind != VK_None);
@@ -60,12 +91,9 @@ std::string [(${namespace})]MCExpr::format(uint8_t Radix, const MCAsmInfo *MAI)
6091
return subexpr + suffix;
6192
}
6293

63-
std::string result = "";
64-
result += "%";
65-
result += AsmUtils::FormatModifier(getKind());
66-
result += "(";
67-
result += subexpr;
68-
result += ')';
94+
std::string fmt = AsmUtils::FormatModifierString(getKind());
95+
std::string mod = AsmUtils::FormatModifier(getKind());
96+
std::string result = custom_format(fmt, mod, subexpr);
6997
return result;
7098
}
7199

vadl/main/vadl/ast/AnnotationTable.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.annotation.Nullable;
3939
import vadl.error.Diagnostic;
4040
import vadl.gcb.annotations.OnlyNegativeNumbersAnnotation;
41+
import vadl.gcb.annotations.RelocationSyntaxAnnotation;
4142
import vadl.gcb.annotations.SkipPruningAnnotation;
4243
import vadl.gcb.annotations.StatusRegisterAnnotation;
4344
import vadl.types.Type;
@@ -255,6 +256,11 @@ public class AnnotationTable {
255256
def.addAnnotation(new DefineOperandAnnotation(fields));
256257
}).build();
257258

259+
annotationOn(RelocationDefinition.class, "syntax", StringAnnotation::new)
260+
.applyViam((def, annotation, lowering) -> {
261+
var lit = (StringLiteral) annotation.definition.values.getFirst();
262+
def.addAnnotation(new RelocationSyntaxAnnotation(lit.value));
263+
}).build();
258264
}
259265

260266
/**

vadl/main/vadl/cppCodeGen/common/ValueRelocationFunctionCodeGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static vadl.utils.GraphUtils.getSingleNode;
2020

21+
import java.util.Optional;
2122
import vadl.cppCodeGen.AbstractRelocationCodeGenerator;
2223
import vadl.cppCodeGen.CppTypeMap;
2324
import vadl.cppCodeGen.context.CGenContext;
@@ -48,7 +49,7 @@ public class ValueRelocationFunctionCodeGenerator extends AbstractRelocationCode
4849
* Options for the function which is generated by the code
4950
* generator.
5051
*/
51-
public record Options(boolean isConst, boolean isStatic) {
52+
public record Options(boolean isConst, boolean isStatic, Optional<String> classPrefix) {
5253

5354
}
5455

@@ -62,7 +63,7 @@ public ValueRelocationFunctionCodeGenerator(
6263
this.relocation = relocation;
6364
this.functionName =
6465
relocation.identifier.lower();
65-
this.options = new Options(false, false);
66+
this.options = new Options(false, false, Optional.empty());
6667
this.context = new CNodeContext(
6768
builder::append,
6869
(ctx, node)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-FileCopyrightText : © 2025 TU Wien <vadl@tuwien.ac.at>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package vadl.gcb.annotations;
18+
19+
import vadl.viam.Annotation;
20+
import vadl.viam.Relocation;
21+
22+
/**
23+
* Defines how the compiler generator will print the relocation in the assembly.
24+
*/
25+
public class RelocationSyntaxAnnotation extends Annotation<Relocation> {
26+
private final String formatString;
27+
28+
public RelocationSyntaxAnnotation(String formatString) {
29+
this.formatString = formatString;
30+
}
31+
32+
@Override
33+
public Class<Relocation> parentDefinitionClass() {
34+
return Relocation.class;
35+
}
36+
37+
public String formatString() {
38+
return formatString;
39+
}
40+
}

0 commit comments

Comments
 (0)