Skip to content

Commit 3b2a679

Browse files
committed
LLVM: Add if/ifelse, extract IRCoercion, flatten irVariableTableType
- Implement if/ifelse control flow for the LLVM backend. ifelse handles the general [cond, code]... pattern; _if delegates to it. Branch bodies push/pop variable scopes matching the intended strict-mode semantics. - Extract coercion helpers (coerceToBoolean, coerceToDouble, emitCoerceToBoolRuntime, emitCoerceToDoubleRuntime) from BasicLogic into a new shared IRCoercion class. Add unified toBool() that handles both compile-time known types and runtime ms_value dispatch. - Flatten irVariableTableType from Stack<Map<Integer, IRType>> to a plain Map, since SSA numbers are function-global and must remain visible after scope pops. MS-level variable scoping stacks are unchanged. - Add IRBuilder.appendLabel() for type-safe label emission, replacing manual string concatenation with ":".
1 parent b846e13 commit 3b2a679

31 files changed

+1758
-171
lines changed

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,23 @@
11661166
<maven.test.failure.ignore>false</maven.test.failure.ignore>
11671167
</properties>
11681168
</profile>
1169+
<profile>
1170+
<id>llvm-integration-tests</id>
1171+
<build>
1172+
<plugins>
1173+
<plugin>
1174+
<groupId>org.apache.maven.plugins</groupId>
1175+
<artifactId>maven-surefire-plugin</artifactId>
1176+
<version>3.2.5</version>
1177+
<configuration>
1178+
<systemPropertyVariables>
1179+
<methodscript_run_llvm_integration_tests>true</methodscript_run_llvm_integration_tests>
1180+
</systemPropertyVariables>
1181+
</configuration>
1182+
</plugin>
1183+
</plugins>
1184+
</build>
1185+
</profile>
11691186
</profiles>
11701187
<pluginRepositories>
11711188
<pluginRepository>

src/main/java/com/laytonsmith/core/asm/AsmCommonLibTemplates.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ private static void register(String code, Environment env) {
6363
register("declare dso_local i32 @time(...)", env);
6464
};
6565

66+
/**
67+
* C Standard
68+
*/
69+
public static final Generator STRCMP = (env) -> {
70+
register("declare dso_local i32 @strcmp(i8*, i8*)", env);
71+
};
72+
73+
/**
74+
* C Standard
75+
*/
76+
public static final Generator STRLEN = (env) -> {
77+
register("declare dso_local i64 @strlen(i8*)", env);
78+
};
79+
6680
public static final Generator LLVM_MEMCPY_P0I8_P0I8_I64 = (env) -> {
6781
register("declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) nounwind", env);
6882
};

src/main/java/com/laytonsmith/core/asm/AsmCompiler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ public void compileEntryPoint(File file, File outputDirectory, String exeName)
228228
}
229229
StringBuilder program = new StringBuilder();
230230
llvmenv.newMethodFrame("@main");
231-
llvmenv.pushVariableScope();
232231
// TODO: Eventually add the argument processing for this
233232
// Pop 0..2 for the main args and entry label
234233
llvmenv.getNewLocalVariableReference(IRType.INTEGER32);

src/main/java/com/laytonsmith/core/asm/AsmMain.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,28 @@
1919
*/
2020
public class AsmMain {
2121

22-
private static void LogErrorAndQuit(String error, int code) {
23-
StreamUtils.GetSystemErr().println(TermColors.RED + error + TermColors.RESET);
24-
System.exit(code);
25-
}
26-
2722
@tool(value = "asm", undocumented = true)
2823
public static class AsmMainCmdlineTool extends AbstractCommandLineTool {
2924

25+
private boolean throwOnError = false;
26+
27+
/**
28+
* When set to true, errors that would normally call System.exit will instead throw a
29+
* RuntimeException. This is intended for use in test environments where System.exit
30+
* would kill the test runner.
31+
*/
32+
public void setThrowOnError(boolean value) {
33+
this.throwOnError = value;
34+
}
35+
36+
private void logErrorAndQuit(String error, int code) {
37+
StreamUtils.GetSystemErr().println(TermColors.RED + error + TermColors.RESET);
38+
if(throwOnError) {
39+
throw new RuntimeException("ASM compilation failed (exit code " + code + "): " + error);
40+
}
41+
System.exit(code);
42+
}
43+
3044
@Override
3145
public ArgumentParser getArgumentParser() {
3246
// Normally we would keep this builder in here, but there are so many options that it makes
@@ -41,6 +55,9 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
4155
new AsmInstaller().install(parsedArgs.isFlagSet("install-toolchain-non-interactive"));
4256
} catch (IOException | InterruptedException e) {
4357
StreamUtils.GetSystemErr().println(e.getMessage());
58+
if(throwOnError) {
59+
throw new RuntimeException("Toolchain installation failed", e);
60+
}
4461
System.exit(1);
4562
}
4663
return;
@@ -59,15 +76,15 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
5976
try {
6077
try {
6178
if(input.isDirectory()) {
62-
LogErrorAndQuit("Currently only single files are supported.", 1);
79+
logErrorAndQuit("Currently only single files are supported.", 1);
6380
} else {
6481
AsmCompiler compiler = new AsmCompiler(parsedArgs);
6582
try {
6683
compiler.compileEntryPoint(input, output, exeName);
6784
} catch (IOException ex) {
68-
LogErrorAndQuit(ex.getMessage(), 1);
85+
logErrorAndQuit(ex.getMessage(), 1);
6986
} catch (InternalException ex) {
70-
LogErrorAndQuit(ex.getMessage() + "\nAn internal exception occurred, which is not caused by your code. "
87+
logErrorAndQuit(ex.getMessage() + "\nAn internal exception occurred, which is not caused by your code. "
7188
+ (parsedArgs.isFlagSet("verbose")
7289
? "Please report this with all the above information."
7390
: "Please re-run with the --verbose switch."), 2);

src/main/java/com/laytonsmith/core/asm/AsmUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public static String formatLine(Target t, LLVMEnvironment llvmenv, String line,
6868
if(s.length() < commentTab) {
6969
s += StringUtils.stringMultiply(commentTab - s.length(), " ");
7070
}
71-
s += " ; " + t.toString();
71+
if(t == Target.UNKNOWN) {
72+
s += " ; <<synthetic>>";
73+
} else {
74+
s += " ; " + t.toString();
75+
}
7276
}
7377
s += OSUtils.GetLineEnding();
7478
return s;

0 commit comments

Comments
 (0)