Skip to content

Commit

Permalink
adding llvm opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonywww committed Dec 2, 2023
1 parent 242f31a commit 8e0446e
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,5 @@ Thumbs.db
output/
*.bin
*.o
*.ll
/bin/
7 changes: 3 additions & 4 deletions src/main/java/me/anthonyw/darkmatter/Darkmatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ public class Darkmatter {
public static final String VERSION = "0.1.0";






public Darkmatter() {

}

}
24 changes: 18 additions & 6 deletions src/main/java/me/anthonyw/darkmatter/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.anthonyw.darkmatter;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

Expand All @@ -10,29 +11,28 @@
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = Darkmatter.NAME, mixinStandardHelpOptions = true, version = Darkmatter.VERSION, description = "Darkmatter compiler.")
public class Main implements Callable<Integer> {

private static final Logger logger = LoggerFactory.getLogger(Main.class);
private int threads = Runtime.getRuntime().availableProcessors();

@Option(names = { "-i", "--input-file" }, description = "Darkmatter source file.")
private String inputFile;
@Parameters(index = "0", description = "Darkmatter source file.")
private File[] inputFile;

@Option(names = { "-o", "--output-file" }, description = "Output binary.")
private String outputFile;

@Option(names = { "-t",
"--threads" }, defaultValue = "-1", description = "Number of threads to use when processing. (default # of cores)")
@Option(names = { "-t", "--threads" }, defaultValue = "-1", description = "Number of threads to use when processing. (default # of cores)")
private void setThreads(int threads) {
if (threads > 0) {
this.threads = threads;
}
}

@Option(names = { "-l",
"--log-level" }, defaultValue = "INFO", description = "Set the default logger level. (default INFO)")
@Option(names = { "-l", "--log-level" }, defaultValue = "INFO", description = "Set the default logger level. (default INFO)")
private void setLogLevel(String level) {

java.util.logging.Level julLevel = java.util.logging.Level.INFO;
Expand Down Expand Up @@ -68,6 +68,18 @@ private void setLogLevel(String level) {
public Integer call() throws Exception {

logger.info("{} v{}", Darkmatter.NAME, Darkmatter.VERSION);


// TODO: check compiler flags

// TODO: initialize darkmatter

// TODO: scan input files

// TODO: parse input into AST

// TODO: compile to LLVM-IR


return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.anthonyw.darkmatter.syntax;
package me.anthonyw.darkmatter.compiler;

public enum CompilerMessage {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package me.anthonyw.darkmatter.syntax;
package me.anthonyw.darkmatter.compiler;

public enum CompilerSeverity {

/**
* Debugging messages.
*/
DEBUG,

/**
* Informational messages.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.anthonyw.darkmatter.compiler;

public abstract class JvmCompilable {

/**
* This will generate JVM byte-code
* @return
*/
public abstract String jvmCodeGen();

}
23 changes: 23 additions & 0 deletions src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.anthonyw.darkmatter.compiler;

import me.anthonyw.darkmatter.compiler.llvm.LlvmOpCode;

public class LlvmAssembly {

private final LlvmOpCode opcode;
private final Object[] args;

public LlvmAssembly(final LlvmOpCode opcode, Object...args) {
this.opcode = opcode;
this.args = args;
}

public LlvmOpCode getOpCode() {
return opcode;
}

public Object[] getArgs() {
return args;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.anthonyw.darkmatter.compiler;

public abstract class LlvmCompilable {

public abstract String llvmCodeGen();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.anthonyw.darkmatter.compiler.jvm;

public enum JvmOpCode {

}
93 changes: 93 additions & 0 deletions src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.anthonyw.darkmatter.compiler.llvm;

public enum LlvmOpCode {

COMMENT(";"),

RETURN("ret"),

BR("br"),

SWITCH("switch"),

INVOKE("invoke"),

UNWIND("unwind"),

UNREACHABLE("unreachable"),


// BINARY


/**
* Addition
*
* Description:
* <p>
* The 'add' instruction returns the sum of its two operands.
* </p>
*
* Example:
* <p>
* {result} = add {type} {var1} {var2}
* {result} = add i32 4, %var
* </p>
*/
ADD("add"),

/**
* Subtraction
*
* <p>
* The value produced is the integer or floating point difference of the two operands.
* </p>
*
* <result> = sub <type> <var1> <var2>
* <result> = sub i32 4, %var
*/
SUBTRACT("sub"),

/**
* Multiply
*
* <p>
* The value produced is the integer or floating point product of the two operands.
* Because the operands are the same width, the result of an integer multiplication is the same whether the operands should be deemed unsigned or signed.
* </p>
*
* <result> = mul i32 4, %var
*/
MULTIPLY("mul"),

/**
* Unsigned division
*
* <result> = udiv <type> <var1>, <var2>
*/
DIVISION_UNSIGNED("udiv"),


/**
* Signed division
*
* <p>
* The value produced is the unsigned integer quotient of the two operands.
* This instruction always performs an unsigned division operation, regardless of whether the arguments are unsigned or not.
* </p>
*
* <result> = sdiv <type> <var1>, <var2>
*/
DIVISION_SIGNED("sdiv");

private final String opcode;

private LlvmOpCode(final String opcode) {
this.opcode = opcode;
}

public String getOpCode() {
return opcode;
}

}
5 changes: 2 additions & 3 deletions src/main/resources/i18n/en_us.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ text.on_lines=on lines %s to %s
# source_code.dm:7:10:
text.source_msg_prefix=%s:%s%s:

statement.terminator.missing=Expected a '%s' at the end of the statement.

statement.terminator.missing=Expected a '%s' at the end of the statement.


scope.open.missing=Expected a scope opening symbol: '%s'.
scope.close.missing=Expected a scope closing symbol: '%s'.

variable.reassignment.useless=Variable %s is being unnecessarily re-assigned %s times.
variable.reassignment.useless=Variable %s is being unnecessarily re-assigned.
variable.reassignment.immutable=Variable %s cannot be re-assigned a value because it is immutable!


variable.name.reserved=The variable name '%s' is a reserved keyword, rename it.
variable.name.convention=Variable %s is not following convention, it should be written in camel-case.



0 comments on commit 8e0446e

Please sign in to comment.