Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp migration #8

Open
wants to merge 23 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cea7c7e
reimplemented lexer and parser
Vipul-Cariappa Jan 29, 2024
18a6765
added support for expressions
Vipul-Cariappa Jan 30, 2024
69aeb45
implemented function call as an expression
Vipul-Cariappa Jan 31, 2024
d86c6f9
implemented AST class for valdef
Vipul-Cariappa Jan 31, 2024
bb8e6d1
implemented AST generation for funcdefs
Vipul-Cariappa Jan 31, 2024
4846915
file interpretation
Vipul-Cariappa Feb 1, 2024
779cda4
messy clean up
Vipul-Cariappa Feb 1, 2024
1911d38
extracting out AST and storing it in a hashmap
Vipul-Cariappa Feb 2, 2024
c930345
implemented semantics verification
Vipul-Cariappa Feb 4, 2024
a895777
implemented interpreter
Vipul-Cariappa Feb 4, 2024
8f072b2
basic cli interpretation
Vipul-Cariappa Feb 4, 2024
9542dc7
changes to work with llvm i.e. removal of try/catch
Vipul-Cariappa Feb 4, 2024
01d8fcb
minor fix for prompt. Prompt should not be displayed for file interpr…
Vipul-Cariappa Feb 5, 2024
05add10
basic semi-complete compiler
Vipul-Cariappa Feb 5, 2024
2d9d44d
Fix Compiler: if operator now used labels/jumps and branches
Vipul-Cariappa Feb 8, 2024
04119ff
Parser: does not terminate at syntax error while interpreting
Vipul-Cariappa Feb 12, 2024
67944e3
initial implementation of JIT
Vipul-Cariappa Feb 13, 2024
76eb67c
slightly better semantic error messages
Vipul-Cariappa Feb 13, 2024
6c74317
better boolean compatibility with llvm
Vipul-Cariappa Feb 13, 2024
62780e8
JIT: added support for variables are no arg func with return type as …
Vipul-Cariappa Feb 13, 2024
8e6d49a
cleaned Parser.yy
Vipul-Cariappa Feb 13, 2024
d604216
cmake update with LLVM
Vipul-Cariappa Mar 23, 2024
08f1c56
speed up compilation by using lld and pre-compiled headers
Vipul-Cariappa Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
# Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
Expand All @@ -85,3 +85,4 @@ _deps

*tmp*
KariLang
bin/
82 changes: 70 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,79 @@
cmake_minimum_required(VERSION 3.20)
project(KariLang C)
project(KariLang)

set(CMAKE_C_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_definitions(-Wall)

find_package(BISON 3.0 REQUIRED)

bison_target(PARSER "src/parser.yy" "src/parser.tab.c" DEFINES_FILE "src/parser.tab.h" COMPILE_FLAGS "-Wall -Wcounterexamples")
bison_target(PARSER "src/Parser.yy" "${CMAKE_CURRENT_BINARY_DIR}/Parser.tab.cc" DEFINES_FILE "${CMAKE_CURRENT_BINARY_DIR}/Parser.tab.hh" COMPILE_FLAGS "-Wall -Wcounterexamples")

find_package(FLEX 2.0 REQUIRED)

flex_target(LEXER "src/lexer.l" "src/lex.yy.c")
flex_target(LEXER "src/Lexer.l" "${CMAKE_CURRENT_BINARY_DIR}/Lex.yy.cc")

find_package(LLVM 17 REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

include_directories(
${CMAKE_CURRENT_BINARY_DIR}
src/
${LLVM_INCLUDE_DIRS}
)

add_definitions(${LLVM_DEFINITIONS_LIST})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})

add_executable(KariLang src/Main.cc
src/AST.cc
src/AST.hh
src/Compile.cc
src/Compile.hh
src/JIT.cc
src/JIT.hh
src/Parser.hh
src/PCH.hh
src/Utils.hh
${BISON_PARSER_OUTPUTS}
${FLEX_LEXER_OUTPUTS})

target_precompile_headers(KariLang PUBLIC src/PCH.hh)

# getting LLVM libraries to link with
set(KARILANG_LLVM_COMPONENTS core support mcjit orcjit native asmparser asmprinter irreader)


# Enable the native target
if ("${LLVM_NATIVE_ARCH}" STREQUAL "AArch64")
set(WITH_TARGET_AARCH64 yes)
endif()

if ("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
set(WITH_TARGET_X86 yes)
endif()

if (WITH_TARGET_AARCH64)
if (NOT ("${LLVM_TARGETS_TO_BUILD}" MATCHES "AArch64"))
message(FATAL_ERROR "The selected LLVM library doesn't have support for AArch64 targets")
endif()

list(APPEND KARILANG_LLVM_COMPONENTS aarch64info aarch64utils aarch64desc aarch64asmparser aarch64codegen aarch64disassembler)
add_definitions("-DHAVE_TARGET_AARCH64=1")
endif()

if (WITH_TARGET_X86)
if (NOT ("${LLVM_TARGETS_TO_BUILD}" MATCHES "X86"))
message(FATAL_ERROR "The selected LLVM library doesn't have support for X86 targets")
endif()

list(APPEND KARILANG_LLVM_COMPONENTS x86info x86desc x86codegen x86asmparser x86disassembler)
add_definitions("-DHAVE_TARGET_X86=1")
endif()

llvm_map_components_to_libnames(llvm_libs ${KARILANG_LLVM_COMPONENTS})

add_executable(KariLang src/main.c
src/semantics.c
src/interpreter.c
src/parser.tab.c
src/lex.yy.c
src/DS.h
src/common.h
src/cli_interpreter.h)
target_link_libraries(KariLang
"${llvm_libs}")
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
BUILD_DIR=bin
SRC_DIR=src

LEXER_FILE = $(SRC_DIR)/Lexer.l
LEXER_OUTPUT = $(BUILD_DIR)/Lexer.yy.cc

PARSER_FILE = $(SRC_DIR)/Parser.yy
PARSER_OUTPUT = $(BUILD_DIR)/Parser.tab.cc

SOURCE_FILES = \
$(SRC_DIR)/Main.cc \
$(SRC_DIR)/AST.cc \
$(SRC_DIR)/Compile.cc \
$(SRC_DIR)/JIT.cc

HEADER_FILES = \
$(SRC_DIR)/Utils.hh \
$(SRC_DIR)/AST.hh \
$(SRC_DIR)/JIT.hh \
$(SRC_DIR)/Parser.hh

INCLUDE_OPTIONS = \
-I$(SRC_DIR) \
-I$(BUILD_DIR)

C = clang
CXX = clang++
BUILD_OPTIONS = \
-Wall \
-g \
`llvm-config --cxxflags --ldflags --system-libs --libs all`
# -fsanitize=address \
# -fno-omit-frame-pointer

$(BUILD_DIR)/KariLang: $(SOURCE_FILES) $(LEXER_OUTPUT) $(PARSER_OUTPUT) $(HEADER_FILES)
$(CXX) $(INCLUDE_OPTIONS) $(SOURCE_FILES) $(LEXER_OUTPUT) $(PARSER_OUTPUT) $(BUILD_OPTIONS) -o $@

$(LEXER_OUTPUT): $(LEXER_FILE) $(PARSER_OUTPUT)
flex -o $@ $(LEXER_FILE)

$(PARSER_OUTPUT): $(PARSER_FILE)
bison -H $^ -o $@

.PHONY: clean
clean:
rm bin/* && rm tmp/*.o
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,14 @@ from the [releases](https://github.com/Vipul-Cariappa/KariLang/releases) page.

If you want to compile from source:

Go to src directory
```bash
cd src/
mkdir bin && cd bin
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j4
```

Compile the parser
```bash
bison -Wall -Wcounterexamples -H ./parser.y
```

Compiler the lexer
```bash
flex ./lexer.l
```
While development to speed up compilation time use `lld` linker

Compiler the language
```bash
cc -Wall -g ./main.c ./semantics.c ./interpreter.c ./lex.yy.c ./parser.tab.c -o ./KariLang
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-fuse-ld=lld"
```
21 changes: 21 additions & 0 deletions runtime/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>

#define RED "\x1B[31m"
#define RESET "\x1B[0m"

int ____karilang_main(int);

int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "KariLang programs take one integer input and outputs "
"an integer value.\n" RED "Error: " RESET
"Integer input missing.\n");
return 1;
}

int input = atoi(argv[1]);
printf("Input: %d\nOutput: %d\n", input, ____karilang_main(input));

return 0;
}
Loading