Skip to content

Commit

Permalink
now good?
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 8, 2025
1 parent c6581f1 commit c993c2b
Showing 1 changed file with 10 additions and 38 deletions.
48 changes: 10 additions & 38 deletions self_hosted/target.jou
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,27 @@ class Target:
global target: Target


# This function is a bit of a hack.
#
# Ideally, we would initialize all available targets, or perhaps only the
# native target, but information about supported targets is only in the C
# header files. If we initialize the wrong target, the code will compile, but
# it prints a warning message like this at runtime, every time you invoke the
# Jou compiler:
#
# 'x86-64' is not a recognized processor for this target (ignoring processor)
#
def running_on_arm64_mac() -> bool:
return MACOS and system("test `uname -m` = arm64") == 0


def init_target() -> None:
if running_on_arm64_mac():
LLVMInitializeX86TargetInfo()
LLVMInitializeX86Target()
LLVMInitializeX86TargetMC()
LLVMInitializeX86AsmParser()
LLVMInitializeX86AsmPrinter()

if MACOS:
# Support the new M1 macs. This will enable the target also on x86_64
# macs, but it doesn't matter.
LLVMInitializeAArch64TargetInfo()
LLVMInitializeAArch64Target()
LLVMInitializeAArch64TargetMC()
LLVMInitializeAArch64AsmParser()
LLVMInitializeAArch64AsmPrinter()
else:
LLVMInitializeX86TargetInfo()
LLVMInitializeX86Target()
LLVMInitializeX86TargetMC()
LLVMInitializeX86AsmParser()
LLVMInitializeX86AsmPrinter()

if WINDOWS:
# LLVM's default is x86_64-pc-windows-msvc
target.triple = "x86_64-pc-windows-gnu"
else:
system("uname -m")
fflush(stdout)
fflush(stderr)
printf("system(...) returns %d\n", system("test `uname -m` = arm64"))
fflush(stdout)
fflush(stderr)
triple = LLVMGetDefaultTargetTriple()
fflush(stdout)
fflush(stderr)
printf("triple=\"%s\"\n", triple)
fflush(stdout)
fflush(stderr)
assert strlen(triple) < sizeof target.triple
strcpy(target.triple, triple)
LLVMDisposeMessage(triple)
Expand All @@ -78,15 +55,10 @@ def init_target() -> None:
assert error == NULL
assert target.target != NULL

if running_on_arm64_mac():
arch = "arm64"
else:
arch = "x86-64"

target.target_machine = LLVMCreateTargetMachine(
target.target,
target.triple,
arch,
"",
"",
LLVMCodeGenOptLevel::Default,
LLVMRelocMode::PIC,
Expand Down

0 comments on commit c993c2b

Please sign in to comment.