switch target to x86-64

main
Clyne 6 months ago
parent 0d8932e969
commit 69d136281d
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -9,9 +9,9 @@ main: $(subst .cpp,.o,$(CXXFILES))
prog: main test.fp prog: main test.fp
./main < test.fp > forsp.ir ./main < test.fp > forsp.ir
llc -march=x86 -filetype=obj -relocation-model=pic forsp.ir -O2 llc -march=x86-64 -filetype=obj -relocation-model=pic forsp.ir -O2
$(CC) -c support.c -m32 -O2 $(CC) -c support.c -O2
$(CC) support.o forsp.ir.o -m32 -O2 $(CC) support.o forsp.ir.o -O2
clean: clean:
rm -f a.out main *.ir *.o rm -f a.out main *.ir *.o

@ -36,7 +36,7 @@ static inline auto storeEnv(LLVMState& llvmState, llvm::Value *index, llvm::Valu
{ {
auto ptrty = llvmState.inttype->getPointerTo(); auto ptrty = llvmState.inttype->getPointerTo();
auto var = llvmState.builder.CreateGEP(ptrty, scope.back().env, {index}); auto var = llvmState.builder.CreateGEP(ptrty, scope.back().env, {index});
return llvmState.builder.CreateStore(val, var, false); return llvmState.builder.CreateStore(val, var);
} }
NumberAST::NumberAST(const std::string& n): BaseAST(n) {} NumberAST::NumberAST(const std::string& n): BaseAST(n) {}
@ -132,7 +132,7 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
if (!v.native) { if (!v.native) {
auto index = llvm::ConstantInt::get(llvmState.inttype, i++); auto index = llvm::ConstantInt::get(llvmState.inttype, i++);
auto m = llvmState.builder.CreateGEP(ptrty, mem, {index}); auto m = llvmState.builder.CreateGEP(ptrty, mem, {index});
llvmState.builder.CreateStore(loadEnv(llvmState, v.value), m, false); llvmState.builder.CreateStore(loadEnv(llvmState, v.value), m);
} }
} }

@ -21,7 +21,7 @@ LLVMState::LLVMState():
ctx(), ctx(),
modul("forsp", ctx), modul("forsp", ctx),
builder(ctx), builder(ctx),
inttype(llvm::Type::getInt32Ty(ctx)), inttype(llvm::Type::getInt64Ty(ctx)),
stacktype(llvm::VectorType::get(inttype, 16, false)), stacktype(llvm::VectorType::get(inttype, 16, false)),
ftype(llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), llvm::ArrayRef<llvm::Type *> {inttype->getPointerTo()}, false)), ftype(llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), llvm::ArrayRef<llvm::Type *> {inttype->getPointerTo()}, false)),
one(llvm::ConstantInt::get(inttype, 1)), one(llvm::ConstantInt::get(inttype, 1)),
@ -38,7 +38,7 @@ llvm::Value *LLVMState::createPush(llvm::Value *var)
{ {
auto dspval = builder.CreateLoad(inttype, llvmSp); auto dspval = builder.CreateLoad(inttype, llvmSp);
auto inc = builder.CreateAdd(dspval, one); auto inc = builder.CreateAdd(dspval, one);
builder.CreateStore(inc, llvmSp, false); builder.CreateStore(inc, llvmSp);
auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dspval}); auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dspval});
return builder.CreateStore(var, gep); return builder.CreateStore(var, gep);
@ -48,7 +48,7 @@ llvm::Value *LLVMState::createPop()
{ {
auto dspval = builder.CreateLoad(inttype, llvmSp); auto dspval = builder.CreateLoad(inttype, llvmSp);
auto dec = builder.CreateSub(dspval, one); auto dec = builder.CreateSub(dspval, one);
builder.CreateStore(dec, llvmSp, false); builder.CreateStore(dec, llvmSp);
auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dec}); auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dec});
return builder.CreateLoad(inttype, gep); return builder.CreateLoad(inttype, gep);
@ -75,7 +75,7 @@ llvm::Value *LLVMState::createVariable(const std::string& name)
llvm::Constant *LLVMState::createInt(int n) llvm::Constant *LLVMState::createInt(int n)
{ {
return llvm::ConstantInt::get(ctx, llvm::APInt(32, n, true)); return llvm::ConstantInt::get(ctx, llvm::APInt(64, n, true));
} }
void LLVMState::output() void LLVMState::output()

@ -2,8 +2,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
extern int32_t sp; #define cell_t int64_t
extern int32_t stack;
extern cell_t sp;
extern cell_t stack;
void emit() void emit()
{ {
@ -12,22 +14,22 @@ void emit()
void _print() void _print()
{ {
printf("%d\n", *(&stack + --sp)); printf("%ld\n", *(&stack + --sp));
} }
void sub() void sub()
{ {
int32_t *st = &stack; cell_t *st = &stack;
st[sp - 2] -= st[sp - 1]; st[sp - 2] -= st[sp - 1];
--sp; --sp;
} }
void cswap() void cswap()
{ {
int32_t *st = &stack; cell_t *st = &stack;
--sp; --sp;
if (st[sp]) { if (st[sp]) {
int32_t tmp = st[sp - 1]; cell_t tmp = st[sp - 1];
st[sp - 1] = st[sp - 2]; st[sp - 1] = st[sp - 2];
st[sp - 2] = tmp; st[sp - 2] = tmp;
} }
@ -35,44 +37,44 @@ void cswap()
void eq() void eq()
{ {
int32_t *st = &stack; cell_t *st = &stack;
--sp; --sp;
st[sp - 1] = st[sp - 1] == st[sp]; st[sp - 1] = st[sp - 1] == st[sp];
} }
void alloc() void alloc()
{ {
int32_t *st = &stack; cell_t *st = &stack;
st[sp - 1] = (int32_t)malloc(st[sp - 1] * sizeof(int32_t)); st[sp - 1] = (cell_t)malloc(st[sp - 1] * sizeof(cell_t));
} }
void dealloc() void dealloc()
{ {
int32_t *st = &stack; cell_t *st = &stack;
--sp; --sp;
free((int32_t *)st[sp]); free((cell_t *)st[sp]);
} }
void peek() void peek()
{ {
int32_t *st = &stack; cell_t *st = &stack;
st[sp - 1] = *(int32_t *)st[sp - 1]; st[sp - 1] = *(cell_t *)st[sp - 1];
} }
void poke() void poke()
{ {
int32_t *st = &stack; cell_t *st = &stack;
sp -= 2; sp -= 2;
int32_t *ptr = (int32_t *)st[sp + 1]; cell_t *ptr = (cell_t *)st[sp + 1];
*ptr = st[sp]; *ptr = st[sp];
} }
void dumpstack() void dumpstack()
{ {
int32_t *st = &stack; cell_t *st = &stack;
printf("<stack> "); printf("<stack> ");
for (int i = 0; i < sp; ++i) for (int i = 0; i < sp; ++i)
printf("%d ", st[i]); printf("%ld ", st[i]);
putchar('\n'); putchar('\n');
} }

@ -10,7 +10,7 @@
(10 emit) $cr (10 emit) $cr
; if-stmt ; if-stmt
($c $t $f c ^f ^t rot cswap $_ force) $if (force cswap drop force) $if
($f $t $c $fn ^f ^t ^c fn) $endif ($f $t $c $fn ^f ^t ^c fn) $endif
; range ; range

Loading…
Cancel
Save