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

Delete compiler/output.jou and move its contents to other files #591

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
30 changes: 29 additions & 1 deletion compiler/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "stdlib/io.jou"
import "stdlib/mem.jou"
import "stdlib/errno.jou"
import "stdlib/str.jou"
import "stdlib/process.jou"

import "./cf_graph.jou"
import "./command_line_args.jou"
Expand All @@ -10,7 +11,6 @@ import "./evaluate.jou"
import "./run.jou"
import "./codegen.jou"
import "./llvm.jou"
import "./output.jou"
import "./typecheck/common.jou"
import "./typecheck/step1_create_types.jou"
import "./typecheck/step2_populate_types.jou"
Expand Down Expand Up @@ -350,6 +350,34 @@ def optimize(module: LLVMModule*, level: int) -> None:
LLVMDisposePassManager(pm)


def compile_to_object_file(module: LLVMModule*) -> byte*:
Akuli marked this conversation as resolved.
Show resolved Hide resolved
len = 0L
objname = get_filename_without_jou_suffix(LLVMGetSourceFileName(module, &len))

objname = realloc(objname, strlen(objname) + 10)
if WINDOWS:
strcat(objname, ".obj")
else:
strcat(objname, ".o")

path = get_path_to_file_in_jou_compiled(objname)
free(objname)

if command_line_args.verbosity >= 1:
printf("Emitting object file: %s\n", path)

tmppath = strdup(path)
error: byte* = NULL
if LLVMTargetMachineEmitToFile(target.target_machine, module, tmppath, LLVMCodeGenFileType::ObjectFile, &error) != 0:
assert error != NULL
fprintf(stderr, "failed to emit object file \"%s\": %s\n", path, error)
exit(1)
free(tmppath)

assert error == NULL
return path


def tokenize_only(path: byte*) -> None:
tokens = tokenize(command_line_args.infile, NULL)
print_tokens(tokens)
Expand Down
95 changes: 0 additions & 95 deletions compiler/output.jou

This file was deleted.

61 changes: 61 additions & 0 deletions compiler/run.jou
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,67 @@ import "stdlib/mem.jou"
import "stdlib/io.jou"
import "stdlib/process.jou"

import "../config.jou"
import "./command_line_args.jou"
import "./paths.jou"


def run_linker(objpaths: byte**, exepath: byte*) -> None:
jou_exe = find_current_executable()
instdir = dirname(jou_exe)

if command_line_args.linker_flags != NULL:
linker_flags = malloc(strlen(command_line_args.linker_flags) + 50)
assert linker_flags != NULL
strcpy(linker_flags, "-lm ")
strcat(linker_flags, command_line_args.linker_flags)
else:
linker_flags = strdup("-lm")
assert linker_flags != NULL

size = 10L
for i = 0; objpaths[i] != NULL; i++:
size += strlen(objpaths[i]) + 10

quoted_object_files: byte* = malloc(size)
assert quoted_object_files != NULL
quoted_object_files[0] = '\0'

for i = 0; objpaths[i] != NULL; i++:
if i != 0:
strcat(quoted_object_files, " ")
strcat(quoted_object_files, "\"")
strcat(quoted_object_files, objpaths[i]) # TODO: escape properly?
strcat(quoted_object_files, "\"")

size = strlen(instdir) + strlen(quoted_object_files) + strlen(exepath) + strlen(linker_flags) + 100
if get_jou_clang_path() != NULL:
size += strlen(get_jou_clang_path())
command: byte* = malloc(size)

if WINDOWS:
# Assume mingw with clang has been downloaded with windows_setup.sh.
# Could also use clang, but gcc has less dependencies so we can make the Windows zips smaller.
# Windows quoting is weird. The outermost quotes get stripped here.
snprintf(command, size, "\"\"%s\\mingw64\\bin\\gcc.exe\" %s -o \"%s\" %s\"", instdir, quoted_object_files, exepath, linker_flags)
else:
# Assume clang is installed and use it to link. Could use lld, but clang is needed anyway.
# instdir is not used in this case.
snprintf(command, size, "'%s' %s -o '%s' %s", get_jou_clang_path(), quoted_object_files, exepath, linker_flags)

free(quoted_object_files)
free(jou_exe)
free(linker_flags)

if command_line_args.verbosity >= 2:
printf("Running linker: %s\n", command)
elif command_line_args.verbosity >= 1:
printf("Running linker\n")

if system(command) != 0:
exit(1)
free(command)


def run_exe(exepath: byte*, valgrind: bool) -> int:
command = malloc(strlen(exepath) + 1000)
Expand Down
Loading