diff --git a/.gitattributes b/.gitattributes index 82bdecf..af7ea12 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,7 @@ # GitHub Linguist Coloring *.dm linguist-language=Darkmatter *.asm linguist-language=Assembly -*.ll linguist-language=Assembly +*.ll linguist-language=LLVM *.java linguist-language=Java *.lua linguist-language=Lua *.json linguist-language=JSON diff --git a/.gitignore b/.gitignore index 2201e48..f173b5b 100644 --- a/.gitignore +++ b/.gitignore @@ -164,5 +164,4 @@ Thumbs.db output/ *.bin *.o -*.ll /bin/ diff --git a/README.md b/README.md index f8a09fe..773b35f 100644 --- a/README.md +++ b/README.md @@ -52,31 +52,26 @@ This is the Darkmatter compiler project, its goal is to output LLVM-IR assembly ### Compiler Parts [Compiler Parts]: #compiler-parts -Darkmatter's compiler will be segmented into 3-parts. +Darkmatter's compiler is segmented into 2-parts. -- [ ] Compiler Front-End (lexer, parser, linter, AST) +- Front-End (lexer, parser, linter, AST) - [ ] Currently under discussion to be written in Rust or Java - [ ] Syntax Definitions & Structure - [ ] Parse compiler flags and arguments - [ ] Input Darkmatter (`.dm`) - [ ] Lexer / Parser / Tokenizer - [ ] AST - - [ ] LLVM primitives/library for Java/Rust - - [ ] 1:1 AST mappings to LLVM codegen() functions. + - [ ] LLVM codegen() for AST + - [ ] Optimize produced LLVM-IR (depending on optimization-level) - [ ] Output LLVM-IR (`.ll`) -- [ ] Compiler Middleware (analyze and optimize LLVM-IR) - - [ ] *TBA* - - [ ] Input LLVM-IR (`.ll`) - - [ ] Output LLVM-IR (`.ll`) -- [ ] Compiler Back-End (assemble IR) - Input IR / Output Native Binary or JVM byte-code. +- Back-End (assemble IR) - Input IR / Output Native Binary or JVM byte-code - [ ] *TBA* - [ ] Input LLVM-IR (`.ll`) + - [ ] Linker - https://lld.llvm.org/ - [ ] Output Native Binary Static Library (`.a`, `.lib`) - [ ] Output Native Binary Dynamic Library (`.so`, `.dll`) - [ ] Output Native Binary Executable (`.exec`, `.exe`) - [ ] Output JVM byte-code (`.class`) -- [ ] Darkmatter Standard Library - - [ ] *TBA* ### Syntax [Syntax]: #syntax @@ -94,10 +89,83 @@ The Darkmatter grammer and syntax requirements. ### Compiler [Compiler]: #compiler +```mermaid +flowchart LR; + + +%% Back End %%; + +H(LLVM-IR):::blue --> |*.ll| I(" + Optimization +"); + +subgraph Darkmatter Back-end; +I --> J(" + LLVM-AS + linker/assembler +"); +end; + +J--> |"*.{a,so,lib,dll,dylib}"| K[" + - Library - + ELF,PE,MACH-O + amd64,i386,arm64,armhf,mips +"]:::green; +J--> |"*.{,exe,exec}"| L[" + - Executable - + ELF,PE,MACH-O + amd64,i386,arm64,armhf,mips +"]:::red; + + + + + +%% Front End %%; + +A(Source Code):::cyan --> |*.dm| B(" + Tokenizer + lexical analysis +"):::yellow; + +subgraph Darkmatter Front-end; + B --> |Tokens| C(" + Parser + syntactic analysis + "); + C --> D(" + AST + "); + D --> E(" + Generator + "); +end; + +E --> G(" + LLVM-IR +"):::blue; +E --> F[" + JVM byte-code +"]:::orange; + + + + + +%% Styling %%; + +classDef red stroke:#FF0000; +classDef green stroke:#00FF00; +classDef blue stroke:#0000FF; +classDef yellow stroke:#FFFF00; +classDef orange stroke:#FFAA00; +classDef cyan stroke:#00FFFF; +``` + ##### Currently considering RAII (Resource Acquisition Is Initialization/Scope-Bound Resource Management) for memory management. -- [ ] Output JVM byte-code + - [ ] Output LLVM-IR assembly - [ ] Compile LLVM-IR to platform/architecture binary (supported ABI/ISAs): - [ ] Linux/amd64 (Intel/AMD 64-bit) @@ -108,12 +176,44 @@ The Darkmatter grammer and syntax requirements. - [ ] MacOS/aarch64 (RISC ARMv8 64-bit) - [ ] Windows/x86 (Intel/AMD 32-bit) - [ ] Windows/amd64 (RISC ARMv8 64-bit) -- [ ] Compiler flags/options/args - - [ ] Compiler can handle a variable # of src files (`ARGS`) - - [ ] Set output file (`-o`) - - [ ] Set threads (`-t`) - - [ ] Set log-level (`-l`) - - [ ] REPL/Interpreter mode (`-I`) +- [ ] Output JVM byte-code +- [ ] `dmc` - Compiler flags/options/args (`ARGS*` are the input Darkmatter source-files to compile) + - `-l`, `--log-level` - Specify the logger level (`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`) (Default: `INFO`) + - `-j`, `--threads` - Specify threads for compilation/transpilation + - [ ] `compile` - Compile Darkmatter directly to a native binary (library/executable) + - `-o`, `--output` - Output destination of the library/executable + - `-t`, `--target` - Specify the target platform/architecture + - `-k`, `--library` - Specify the library type (`static`/`dynamic`) (Default: `static`) + - `-A`, `--emit` - Only emit LLVM-IR assembly, do not assemble. + - `-O`, `--optimize` - Optimization level (`0`=no optimization, `2`=aggressive optimization) (Default: `0`) + - [ ] `generate` - Compile Darkmatter to JVM byte-code + - `-o`, `--output` - Output file destination + - `-b`, `--byte-code` - The byte-code language to produce (`jvm`) (Default: `jvm`) + +Examples: +```sh +## AoT Examples + +# Compile `count_to_ten.dm` to a native executable binary named `count` +dmc compile --output count count_to_ten.dm +dmc compile -o count count_to_ten.dm + +# Compile `count_to_ten.dm` to a native `linux/amd64` executable binary (ELF) +dmc compile --output count --target linux/amd64 count_to_ten.dm +dmc compile -o count -t linux/amd64 count_to_ten.dm + + +## JIT Examples + +# Compile `my_class.dm` to a JVM byte-code class file +dmc generate --output MyClass.class --byte-code jvm my_class.dm +dmc generate -o MyClass.class my_class.dm + +# Compile `a.dm` and `b.dm` to a JVM byte-code jar file +dmc generate --output MyApp.jar --byte-code jvm a.dm b.dm +dmc generate -o MyApp.jar a.dm b.dm +``` + ### Standard Library [Standard Library]: #standard-library diff --git a/src/main/java/me/anthonyw/darkmatter/Keywords.java b/src/main/java/me/anthonyw/darkmatter/Keywords.java index ed9ecaa..90e5203 100644 --- a/src/main/java/me/anthonyw/darkmatter/Keywords.java +++ b/src/main/java/me/anthonyw/darkmatter/Keywords.java @@ -107,7 +107,9 @@ public enum Keywords { - + /** + * A compile-time assigned data-type variable (constant/immutable). + */ VAR(KeywordType.DATA_TYPE); private final KeywordType type; diff --git a/src/main/java/me/anthonyw/darkmatter/TokenType.java b/src/main/java/me/anthonyw/darkmatter/TokenType.java index 521e1dc..1f1478f 100644 --- a/src/main/java/me/anthonyw/darkmatter/TokenType.java +++ b/src/main/java/me/anthonyw/darkmatter/TokenType.java @@ -16,7 +16,7 @@ public enum TokenType { PERCENT("%"), - CIRCUMFLEX_ACCENT("^"), + CIRCUMFLEX("^"), AMPERSAND("&"), @@ -83,13 +83,22 @@ public enum TokenType { LITERAL_INTEGER, + LITERAL_FLOAT, + + LITERAL_DOUBLE, + + LITERAL_LONG, + LITERAL_CHARACTER, LITERAL_STRING, LITERAL_BOOLEAN, - LET("let"), + /** + * A compile-time assigned data-type variable (constant/immutable). + */ + VAR("var"), FUNCTION("function"), @@ -124,14 +133,27 @@ public enum TokenType { // Module/Container types + // Structs + STRUCT("struct"), + IMPLEMENTATION("implementation"), + + // Classes (OOP) + CLASS("class"), + /** + * Like Java 'extends' + */ + INHERITS("inherits"), + ENUM("enum"), INTERFACE("interface"), + ABSTRACT("abstract"), + STATEMENT, IF("if"), diff --git a/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java b/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java index 9c37e4a..34408b9 100644 --- a/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java +++ b/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java @@ -1,133 +1,80 @@ package me.anthonyw.darkmatter.cli; -import java.util.regex.Pattern; +public class AnsiColor { + + // Prevent instantiation + private AnsiColor() {} + + // Reset + public static final String RESET = "\033[0m"; // Text Reset -/** * All supported color values for chat */ -public enum AnsiColor { - /** * Represents black */ - BLACK('0', 0x00), - /** * Represents dark blue */ - DARK_BLUE('1', 0x1), - /** * Represents dark green */ - DARK_GREEN('2', 0x2), - /** * Represents dark blue (aqua) */ - DARK_AQUA('3', 0x3), - /** * Represents dark red */ - DARK_RED('4', 0x4), - /** * Represents dark purple */ - PURPLE('5', 0x5), - /** * Represents gold */ - GOLD('6', 0x6), - /** * Represents gray */ - GRAY('7', 0x7), - /** * Represents dark gray */ - DARK_GRAY('8', 0x8), - /** * Represents blue */ - BLUE('9', 0x9), - /** * Represents green */ - GREEN('a', 0xA), - /** * Represents aqua */ - AQUA('b', 0xB), - /** * Represents red */ - RED('c', 0xC), - /** * Represents light purple */ - PINK('d', 0xD), - /** * Represents yellow */ - YELLOW('e', 0xE), - /** * Represents white */ - WHITE('f', 0xF), - /** * Represents magical characters that change around randomly */ - MAGIC('k', 0x10, true), - /** * Makes the text bold. */ - BOLD('l', 0x11, true), - /** * Makes a line appear through the text. */ - STRIKETHROUGH('m', 0x12, true), - /** * Makes the text appear underlined. */ - UNDERLINE('n', 0x13, true), - /** * Makes the text italic. */ - ITALIC('o', 0x14, true), - /** * Resets all previous chat colors or formats. */ - RESET('r', 0x15); - /** - * * The special character which prefixes all chat colour codes. Use this if * - * you need to dynamically convert colour codes from your custom format. - */ - public static final char COLOR_CHAR = '\u00A7'; - private static final Pattern STRIP_COLOR_PATTERN = Pattern - .compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK-OR]"); - private final int intCode; - private final char code; - private final boolean isFormat; - private final String toString; + // Regular Colors + public static final String BLACK = "\033[0;30m"; // BLACK + public static final String RED = "\033[0;31m"; // RED + public static final String GREEN = "\033[0;32m"; // GREEN + public static final String YELLOW = "\033[0;33m"; // YELLOW + public static final String BLUE = "\033[0;34m"; // BLUE + public static final String PURPLE = "\033[0;35m"; // PURPLE + public static final String CYAN = "\033[0;36m"; // CYAN + public static final String WHITE = "\033[0;37m"; // WHITE - private AnsiColor(char code, int intCode) { - this(code, intCode, false); - } + // Bold + public static final String BLACK_BOLD = "\033[1;30m"; // BLACK + public static final String RED_BOLD = "\033[1;31m"; // RED + public static final String GREEN_BOLD = "\033[1;32m"; // GREEN + public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW + public static final String BLUE_BOLD = "\033[1;34m"; // BLUE + public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE + public static final String CYAN_BOLD = "\033[1;36m"; // CYAN + public static final String WHITE_BOLD = "\033[1;37m"; // WHITE - private AnsiColor(char code, int intCode, boolean isFormat) { - this.code = code; - this.intCode = intCode; - this.isFormat = isFormat; - this.toString = new String(new char[] { COLOR_CHAR, code }); - } + // Underline + public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK + public static final String RED_UNDERLINED = "\033[4;31m"; // RED + public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN + public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW + public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE + public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE + public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN + public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE - /** - * * Gets the char value associated with this color * * @return A char value of - * this color code - */ - public char getChar() { - return code; - } + // Background + public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK + public static final String RED_BACKGROUND = "\033[41m"; // RED + public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN + public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW + public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE + public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE + public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN + public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE - @Override - public String toString() { - return toString; - } + // High Intensity + public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK + public static final String RED_BRIGHT = "\033[0;91m"; // RED + public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN + public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW + public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE + public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE + public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN + public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE - /** * Checks if this code is a format code as opposed to a color code. */ - public boolean isFormat() { - return isFormat; - } + // Bold High Intensity + public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK + public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED + public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN + public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW + public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE + public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE + public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN + public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE - /** * Checks if this code is a color code as opposed to a format code. */ - public boolean isColor() { - return !isFormat && this != RESET; - } - - /** * Get intCode * @return */ - public int getCode() { - return intCode; - } - - /** - * * Strips the given message of all color codes * * @param input String to - * strip of color * @return A copy of the input string, without any coloring - */ - public static String stripColor(final String input) { - if (input == null) { - return null; - } - return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); - } - - /** - * Translates a string using an alternate color code character into a string - * that uses the internal ChatColor.COLOR_CODE color code character. The - * alternate color code character will only be replaced if it is immediately - * followed by 0-9, A-F, a-f, K-O, k-o, R or r. - * - * @param altColorChar The alternate color code character to replace. Ex: & - * @param textToTranslate Text containing the alternate color code character. - * @return Text containing the ChatColor.COLOR_CODE color code character. - */ - public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) { - char[] b = textToTranslate.toCharArray(); - for (int i = 0; i < b.length - 1; i++) { - if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) { - b[i] = AnsiColor.COLOR_CHAR; - b[i + 1] = Character.toLowerCase(b[i + 1]); - } - } - return new String(b); - } -} + // High Intensity backgrounds + public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK + public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED + public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN + public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW + public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE + public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE + public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN + public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE +} \ No newline at end of file diff --git a/src/main/java/me/anthonyw/darkmatter/cli/ConsoleColors.java b/src/main/java/me/anthonyw/darkmatter/cli/ConsoleColors.java deleted file mode 100644 index 121db06..0000000 --- a/src/main/java/me/anthonyw/darkmatter/cli/ConsoleColors.java +++ /dev/null @@ -1,80 +0,0 @@ -package me.anthonyw.darkmatter.cli; - -public class ConsoleColors { - - // Prevent instantiation - private ConsoleColors() {} - - // Reset - public static final String RESET = "\033[0m"; // Text Reset - - // Regular Colors - public static final String BLACK = "\033[0;30m"; // BLACK - public static final String RED = "\033[0;31m"; // RED - public static final String GREEN = "\033[0;32m"; // GREEN - public static final String YELLOW = "\033[0;33m"; // YELLOW - public static final String BLUE = "\033[0;34m"; // BLUE - public static final String PURPLE = "\033[0;35m"; // PURPLE - public static final String CYAN = "\033[0;36m"; // CYAN - public static final String WHITE = "\033[0;37m"; // WHITE - - // Bold - public static final String BLACK_BOLD = "\033[1;30m"; // BLACK - public static final String RED_BOLD = "\033[1;31m"; // RED - public static final String GREEN_BOLD = "\033[1;32m"; // GREEN - public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW - public static final String BLUE_BOLD = "\033[1;34m"; // BLUE - public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE - public static final String CYAN_BOLD = "\033[1;36m"; // CYAN - public static final String WHITE_BOLD = "\033[1;37m"; // WHITE - - // Underline - public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK - public static final String RED_UNDERLINED = "\033[4;31m"; // RED - public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN - public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW - public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE - public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE - public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN - public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE - - // Background - public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK - public static final String RED_BACKGROUND = "\033[41m"; // RED - public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN - public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW - public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE - public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE - public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN - public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE - - // High Intensity - public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK - public static final String RED_BRIGHT = "\033[0;91m"; // RED - public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN - public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW - public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE - public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE - public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN - public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE - - // Bold High Intensity - public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK - public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED - public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN - public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW - public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE - public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE - public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN - public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE - - // High Intensity backgrounds - public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK - public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED - public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN - public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW - public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE - public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE - public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN - public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE -} \ No newline at end of file diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/Architecture.java b/src/main/java/me/anthonyw/darkmatter/compiler/Architecture.java new file mode 100644 index 0000000..f4b58b0 --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/Architecture.java @@ -0,0 +1,43 @@ +package me.anthonyw.darkmatter.compiler; + +public enum Architecture { + + // CISC + + /** + * Modern 64-bit desktop processors. + * + * Also known as "x86_64". + */ + AMD64("amd64"), + + /** + * Older generation 32-bit desktop processors. + */ + I386("i386"), + + // RISC + + /** + * Modern 64-bit embedded systems. + */ + ARM64("aarch64"), + + /** + * Older generation embedded systems. + * + * Also known as "armv7h". + */ + ARM32("armhf"); + + private final String identifier; + + private Architecture(final String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/Compilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/Compilable.java new file mode 100644 index 0000000..565f62d --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/Compilable.java @@ -0,0 +1,5 @@ +package me.anthonyw.darkmatter.compiler; + +public interface Compilable { + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java b/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java index feea2cc..6e8a8db 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java @@ -1,25 +1,32 @@ package me.anthonyw.darkmatter.compiler; +import me.anthonyw.darkmatter.cli.AnsiColor; + public enum CompilerSeverity { /** - * Debugging messages. - */ - DEBUG, - - /** - * Informational messages. + * Helpful suggestions. */ - INFO, + NOTE(AnsiColor.CYAN_BRIGHT), /** * Warning messages, including basic optimization checks. */ - WARN, + WARN(AnsiColor.YELLOW_BRIGHT), /** * Error messages, requires review, compilation halted. */ - ERROR; + ERROR(AnsiColor.RED_BRIGHT); + + private final String cliColor; + + private CompilerSeverity(final String redBright) { + this.cliColor = redBright; + } + + public String getAnsiColor() { + return cliColor; + } } diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java deleted file mode 100644 index 7e10bfc..0000000 --- a/src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java +++ /dev/null @@ -1,11 +0,0 @@ -package me.anthonyw.darkmatter.compiler; - -public abstract class JvmCompilable { - - /** - * This will generate JVM byte-code - * @return - */ - public abstract String jvmCodeGen(); - -} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/LlvmCompilable.java deleted file mode 100644 index 29d811a..0000000 --- a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmCompilable.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.anthonyw.darkmatter.compiler; - -public abstract class LlvmCompilable { - - public abstract String llvmCodeGen(); - -} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/Platform.java b/src/main/java/me/anthonyw/darkmatter/compiler/Platform.java new file mode 100644 index 0000000..30d3de6 --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/Platform.java @@ -0,0 +1,55 @@ +package me.anthonyw.darkmatter.compiler; + +public enum Platform { + + /** + * Universal OS + * + * https://github.com/universalos-io + */ + UNIVERSAL_OS("universalos"), + + /** + * GNU/Linux + * + * https://www.gnu.org/gnu/linux-and-gnu.en.html + * https://kernel.org/ + * https://en.wikipedia.org/wiki/Linux + */ + LINUX("linux"), + + /** + * Apple MacOS (XNU/Darwin) + * + * https://www.apple.com/macos/ + * https://en.wikipedia.org/wiki/MacOS + */ + MAC_OS("darwin"), + + /** + * Microsoft Windows + * + * https://www.microsoft.com/en-us/windows/ + * https://en.wikipedia.org/wiki/Microsoft_Windows + */ + WINDOWS("windows"), + + /** + * FreeBSD + * + * https://www.freebsd.org/ + * https://en.wikipedia.org/wiki/FreeBSD + */ + FREEBSD("freebsd"); + + private final String identifier; + + private Platform(final String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmCompilable.java new file mode 100644 index 0000000..5f41b1a --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmCompilable.java @@ -0,0 +1,13 @@ +package me.anthonyw.darkmatter.compiler.jvm; + +import me.anthonyw.darkmatter.compiler.Compilable; + +public abstract class JvmCompilable implements Compilable { + + /** + * This will generate JVM byte-code. + * @return + */ + public abstract String jvmCodeGen(); + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java index c2e5bbd..702afec 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java @@ -208,18 +208,18 @@ public enum JvmOpCode { IMPDEP2(0xFF, "impdep2"); private final int opcode; - private final String name; + private final String mnemonic; - JvmOpCode(int opcode, String name) { + private JvmOpCode(int opcode, String mnemonic) { this.opcode = opcode; - this.name = name; + this.mnemonic = mnemonic; } - public int getOpcode() { + public int getOpCode() { return opcode; } - public String getName() { - return name; + public String getMnemonic() { + return mnemonic; } } diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmAssembly.java similarity index 76% rename from src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java rename to src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmAssembly.java index 6f7d13a..a485964 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmAssembly.java @@ -1,6 +1,4 @@ -package me.anthonyw.darkmatter.compiler; - -import me.anthonyw.darkmatter.compiler.llvm.LlvmOpCode; +package me.anthonyw.darkmatter.compiler.llvm; public class LlvmAssembly { diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmCompilable.java new file mode 100644 index 0000000..8aa31c7 --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmCompilable.java @@ -0,0 +1,13 @@ +package me.anthonyw.darkmatter.compiler.llvm; + +import me.anthonyw.darkmatter.compiler.Compilable; + +public abstract class LlvmCompilable implements Compilable { + + /** + * This will generate LLVM-IR assembly. + * @return + */ + public abstract String llvmCodeGen(); + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java index c0a200c..8aa8482 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java @@ -2,92 +2,94 @@ public enum LlvmOpCode { - COMMENT(";"), - - RETURN("ret"), - - BR("br"), - - SWITCH("switch"), - - INVOKE("invoke"), - - UNWIND("unwind"), - - UNREACHABLE("unreachable"), - - - // BINARY - - - /** - * Addition - * - * Description: - *

- * The 'add' instruction returns the sum of its two operands. - *

- * - * Example: - *

- * {result} = add {type} {var1} {var2} - * {result} = add i32 4, %var - *

- */ - ADD("add"), - - /** - * Subtraction - * - *

- * The value produced is the integer or floating point difference of the two operands. - *

- * - * = sub - * = sub i32 4, %var - */ - SUBTRACT("sub"), - - /** - * Multiply - * - *

- * 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. - *

- * - * = mul i32 4, %var - */ - MULTIPLY("mul"), - - /** - * Unsigned division - * - * = udiv , - */ - DIVISION_UNSIGNED("udiv"), - - - /** - * Signed division - * - *

- * 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. - *

- * - * = sdiv , - */ - DIVISION_SIGNED("sdiv"); - - private final String opcode; - - private LlvmOpCode(final String opcode) { + // Terminator Instructions + RETURN(0x01, "ret"), // 01 + BR(0x02, "br"), // 02 + SWITCH(0x03, "switch"), // 03 + INDIRECT_BR(0x04, "indirectbr"), // 04 + INVOKE(0x05, "invoke"), // 05 + @Deprecated + UNWIND(0x06, "unwind"), // 06 + UNREACHABLE(0x07, "unreachable"), // 07 + ADD(0x08, "add"), // 08 + FADD(0x09, "fadd"), // 09 + SUB(0x0a, "sub"), // 10 + FSUB(0x0b, "fsub"), // 11 + MUL(0x0c, "mul"), // 12 + FMUL(0x0d, "fmul"), // 13 + UDIV(0x0e, "udiv"), // 14 + SDIV(0x0f, "sdiv"), // 15 + FDIV(0x10, "fdiv"), // 16 + UREM(0x11, "urem"), // 17 + SREM(0x12, "srem"), // 18 + FREM(0x13, "frem"), // 19 + + // Logical Operators + SHL(0x14, "shl"), // 20 + LSHR(0x15, "lshr"), // 21 + ASHR(0x16, "ashr"), // 22 + AND(0x17, "and"), // 23 + OR(0x18, "or"), // 24 + XOR(0x19, "xor"), // 25 + + // Memory Operators + ALLOCA(0x1a, "alloca"), // 26 + LOAD(0x1b, "load"), // 27 + STORE(0x1c, "store"), // 28 + GET_ELEMENT_PTR(0x1d, "getelementptr"), // 29 + + // Cast Operators + TRUNC(0x1e, "trunc"), // 30 + ZEXT(0x1f, "zext"), // 31 + SEXT(0x20, "sext"), // 32 + FP_TO_UI(0x21, "fptoui"), // 33 + FP_TO_SI(0x22, "fptosi"), // 34 + UI_TO_FP(0x23, "uitofp"), // 35 + SI_TO_FP(0x24, "sitofp"), // 36 + FP_TRUNC(0x25, "fptrunc"), // 37 + FPEXT(0x26, "fpext"), // 38 + PTRTOINT(0x27, "ptrtoint"), // 39 + INTTOPTR(0x28, "inttoptr"), // 40 + BITCAST(0x29, "bitcast"), // 41 + + // Other Operators + ICMP(0x2a, "icmp"), // 42 + FCMP(0x2b, "fcmp"), // 43 + PHI(0x2c, "phi"), // 44 + CALL(0x2d, "call"), // 45 + SELECT(0x2e, "select"), // 46 + USER_OP1(0x2f, "userop1"), // 47 + USER_OP2(0x30, "userop2"), // 48 + AARG(0x31, "aarg"), // 49 + EXTRACT_ELEMENT(0x32, "extractelement"), // 50 + INSERT_ELEMENT(0x33, "insertelement"), // 51 + SHUFFLE_VECTOR(0x34, "shufflevector"), // 52 + EXTRACT_VALUE(0x35, "extractvalue"), // 53 + INSERT_VALUE(0x36, "insertvalue"), // 54 + + // Atomic Operators + FENCE(0x37, "fence"), // 55 + ATOMIC_CMP_XCHG(0x38, "atomiccmpxchg"), // 56 + ATOMIC_RMW(0x39, "atomicrmw"), // 57 + + // Exception Handling Operators + RESUME(0x3a, "resume"), // 58 + LANDING_PAD(0x3b, "landingpad"); // 59 + + private final int opcode; + private final String mnemonic; + + private LlvmOpCode(final int opcode, final String mnemonic) { this.opcode = opcode; + this.mnemonic = mnemonic; } - public String getOpCode() { + public int getOpCode() { return opcode; } + public String getMnemonic() { + return mnemonic; + } + } diff --git a/src/main/java/me/anthonyw/darkmatter/jul/ConsoleHandler.java b/src/main/java/me/anthonyw/darkmatter/jul/ConsoleHandler.java index 4b4c577..cdca375 100644 --- a/src/main/java/me/anthonyw/darkmatter/jul/ConsoleHandler.java +++ b/src/main/java/me/anthonyw/darkmatter/jul/ConsoleHandler.java @@ -4,7 +4,7 @@ import java.util.logging.Handler; import java.util.logging.Level; -import me.anthonyw.darkmatter.cli.ConsoleColors; +import me.anthonyw.darkmatter.cli.AnsiColor; public class ConsoleHandler extends Handler { @@ -23,11 +23,11 @@ public void publish(java.util.logging.LogRecord record) { try { final String message = getFormatter().format(record); if (record.getLevel().intValue() == Level.WARNING.intValue()) { - System.err.printf("%s%s%s%n", ConsoleColors.YELLOW_BRIGHT, message.toString(), ConsoleColors.RESET); + System.err.printf("%s%s%s%n", AnsiColor.YELLOW_BRIGHT, message.toString(), AnsiColor.RESET); } else if (record.getLevel().intValue() > Level.WARNING.intValue()) { - System.err.printf("%s%s%s%n", ConsoleColors.RED_BRIGHT, message.toString(), ConsoleColors.RESET); + System.err.printf("%s%s%s%n", AnsiColor.RED_BRIGHT, message.toString(), AnsiColor.RESET); } else { - System.out.printf("%s%s%n", message.toString(), ConsoleColors.RESET); + System.out.printf("%s%s%n", message.toString(), AnsiColor.RESET); } } catch (Exception exception) { reportError(null, exception, ErrorManager.FORMAT_FAILURE); diff --git a/src/main/resources/META-INF/.gitkeep b/src/main/resources/META-INF/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/jni-config.json b/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/jni-config.json deleted file mode 100644 index 90b2612..0000000 --- a/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/jni-config.json +++ /dev/null @@ -1,131 +0,0 @@ -[ - { - "name":"java.lang.ClassLoader", - "methods":[ - {"name":"getPlatformClassLoader","parameterTypes":[] }, - {"name":"loadClass","parameterTypes":["java.lang.String"] } - ] - }, - { - "name":"java.lang.Throwable", - "allDeclaredFields":true, - "allDeclaredMethods":true - }, - { - "name":"java.util.ArrayList", - "methods":[ - {"name":"","parameterTypes":[] }, - {"name":"add","parameterTypes":["java.lang.Object"] } - ] - }, - { - "name":"java.util.HashMap", - "methods":[ - {"name":"","parameterTypes":[] }, - {"name":"put","parameterTypes":["java.lang.Object","java.lang.Object"] } - ] - }, - { - "name":"org.eclipse.swt.browser.WebKit", - "allDeclaredFields":true, - "allDeclaredMethods":true - }, - { - "name":"org.eclipse.swt.internal.cocoa.CGPoint", - "fields":[ - {"name":"x"}, - {"name":"y"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.CGRect", - "fields":[ - {"name":"origin"}, - {"name":"size"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.CGSize", - "fields":[ - {"name":"height"}, - {"name":"width"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.NSOperatingSystemVersion", - "fields":[ - {"name":"majorVersion"}, - {"name":"minorVersion"}, - {"name":"patchVersion"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.NSPoint", - "fields":[ - {"name":"x"}, - {"name":"y"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.NSRange", - "fields":[ - {"name":"length"}, - {"name":"location"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.NSRect", - "fields":[ - {"name":"height"}, - {"name":"width"}, - {"name":"x"}, - {"name":"y"} - ] - }, - { - "name":"org.eclipse.swt.internal.cocoa.NSSize", - "fields":[ - {"name":"height"}, - {"name":"width"} - ] - }, - { - "name": "org.eclipse.swt.internal.cocoa.objc_super", - "fields": [ - {"name": "receiver"}, - {"name": "super_class"} - ] - }, - { - "name": "org.eclipse.swt.internal.cocoa.OS", - "fields": [ - {"name": "sel_identity"} - ] - }, - { - "name":"org.eclipse.swt.widgets.Display", - "methods":[ - {"name":"applicationProc","parameterTypes":["long","long"] }, - {"name":"applicationProc","parameterTypes":["long","long","long"] }, - {"name":"applicationProc","parameterTypes":["long","long","long","long"] }, - {"name":"applicationProc","parameterTypes":["long","long","long","long","long","long"] }, - {"name":"cursorSetProc","parameterTypes":["long","long"] }, - {"name":"dialogProc","parameterTypes":["long","long","long"] }, - {"name":"dialogProc","parameterTypes":["long","long","long","long"] }, - {"name":"dialogProc","parameterTypes":["long","long","long","long","long"] }, - {"name":"observerProc","parameterTypes":["long","long","long"] }, - {"name":"windowProc","parameterTypes":["long","long"] }, - {"name":"windowProc","parameterTypes":["long","long","long"] }, - {"name":"windowProc","parameterTypes":["long","long","long","long"] }, - {"name":"windowProc","parameterTypes":["long","long","long","long","long"] }, - {"name":"windowProc","parameterTypes":["long","long","long","long","long","long"] } - ] - }, - { - "name":"org.eclipse.swt.widgets.FileDialog", - "methods":[ - {"name":"_completionHandler","parameterTypes":["long"] }, - {"name":"_overwriteExistingFileCheck","parameterTypes":["long","long","long"] } - ] - } -] diff --git a/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/native-image.properties b/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/native-image.properties deleted file mode 100644 index 0f6f07f..0000000 --- a/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/native-image.properties +++ /dev/null @@ -1,11 +0,0 @@ -Args=--no-fallback \ - -R:AnalysisSizeCutoff=-1 \ - -H:Optimize=2 \ - -H:+ReportExceptionStackTraces \ - --allow-incomplete-classpath \ - --initialize-at-build-time=org.eclipse.swt.widgets.Display \ - --initialize-at-build-time=org.eclipse.swt.internal.gtk.OS \ - --initialize-at-build-time=org.eclipse.swt.internal.Library \ - --initialize-at-build-time=com.oracle.svm.core.util.UserError$UserException \ - --initialize-at-build-time=org.eclipse.swt.internal.DPIUtil$AutoScaleMethod \ - --initialize-at-build-time=org.eclipse.swt.internal.DPIUtil \ No newline at end of file diff --git a/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/reflect-config.json b/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/reflect-config.json deleted file mode 100644 index c77676a..0000000 --- a/src/main/resources/META-INF/native-image/me.anthonyw/netromancy/reflect-config.json +++ /dev/null @@ -1,49 +0,0 @@ -[ - { - "name":"java.lang.Thread", - "methods":[{"name":"getContextClassLoader","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.widgets.Display", - "methods":[{"name":"fixedClassInitProc","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.graphics.Device", - "methods":[{"name":"getPrimaryScreen","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.graphics.GC", - "fields":[{"name":"handle"}] - }, - { - "name":"org.eclipse.swt.internal.cocoa.NSScreen", - "methods":[{"name":"backingScaleFactor","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.internal.cocoa.id", - "fields":[{"name":"id"}] - }, - { - "name":"org.eclipse.swt.internal.image.GIFFileFormat", - "methods":[{"name":"","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.internal.image.JPEGFileFormat", - "methods":[{"name":"","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.internal.image.PNGFileFormat", - "methods":[{"name":"","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.internal.image.WinBMPFileFormat", - "methods":[{"name":"","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.internal.image.WinICOFileFormat", - "methods":[{"name":"","parameterTypes":[] }] - }, - { - "name":"org.eclipse.swt.widgets.Display" - } -] diff --git a/src/main/resources/asm/.gitkeep b/src/main/resources/asm/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/i18n/.gitkeep b/src/main/resources/i18n/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/llvm/.gitkeep b/src/main/resources/llvm/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/llvm/test.ll b/src/main/resources/llvm/test.ll new file mode 100644 index 0000000..7f1962a --- /dev/null +++ b/src/main/resources/llvm/test.ll @@ -0,0 +1,6 @@ +; ModuleID = 'test.bc' +source_filename = "test.ll" + +define i32 @main() { + ret i32 42 +} diff --git a/src/main/resources/lua/.gitkeep b/src/main/resources/lua/.gitkeep new file mode 100644 index 0000000..e69de29