Skip to content

Commit 32a24d4

Browse files
committed
fix: more jit fixes, still incorrect result
1 parent 5d9a363 commit 32a24d4

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

stdlib/jit/jitcodegen.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,16 @@ void JITCodegen::init() {
139139
wrapperFunc = LLVMAddFunction(module, "__next_jit_wrapper", functionType);
140140
}
141141

142-
LLVMValueRef JITCodegen::getConstantInt(uint64_t val) {
143-
return LLVMConstInt(LLVMIntType(64), val, 0);
142+
LLVMValueRef JITCodegen::getConstantInt(uint64_t val, int width) {
143+
return LLVMConstInt(LLVMIntType(width), val, 0);
144+
}
145+
146+
LLVMValueRef JITCodegen::getConstantSInt(int64_t val) {
147+
return LLVMConstInt(LLVMIntType(64), val, 1);
148+
}
149+
150+
LLVMValueRef JITCodegen::getConstantDouble(double val) {
151+
return LLVMConstReal(LLVMDoubleType(), val);
144152
}
145153

146154
LLVMValueRef JITCodegen::defineIsNumber(LLVMValueRef func) {
@@ -397,7 +405,7 @@ LLVMValueRef JITCodegen::generateCastOrReturn(LLVMTypeRef targetType,
397405
}
398406

399407
LLVMTypeRef JITCodegen::getTypeFromString(String *type) {
400-
if(type == String::const_i1) {
408+
if(type == String::const_bool_) {
401409
return LLVMInt1Type();
402410
}
403411
if(type == String::const_i64) {
@@ -456,6 +464,7 @@ bool JITCodegen::hasVariable(Token name) {
456464
}
457465

458466
LLVMValueRef JITCodegen::getVariable(Token name) {
467+
assert(hasVariable(name) && "Variable doesn't exist!");
459468
String2 s = String::from(name.start, name.length);
460469
return variableMap[s];
461470
}
@@ -545,6 +554,19 @@ void JITCodegen::visit(FnStatement *s) {
545554
auto retTyped =
546555
LLVMBuildCall2(builder, typedFnType, compiledFuncTyped,
547556
argValuesTyped.data(), s->arity, "__typed_func_ret");
557+
558+
// wrap the typed result back to a Next value
559+
// before returning
560+
auto retType = LLVMTypeOf(retTyped);
561+
if(retType == LLVMIntType(64) || retType == LLVMDoubleType()) {
562+
retTyped =
563+
callSetNumber(generateCastOrReturn(LLVMDoubleType(), retTyped));
564+
} else if(retType == LLVMIntType(1)) {
565+
retTyped = callSetBoolean(retTyped);
566+
} else {
567+
assert("Unknown return type!");
568+
}
569+
548570
LLVMBuildBr(builder, continueBlock);
549571

550572
// create the call to the untyped function
@@ -860,11 +882,25 @@ LLVMValueRef JITCodegen::generateBinOp(LLVMValueRef left, LLVMValueRef right,
860882

861883
LLVMValueRef JITCodegen::visit(VariableExpression *v) {
862884
auto valptr = getVariable(v->token);
863-
return LLVMBuildLoad2(builder, nextType, valptr, "__var_load");
885+
return LLVMBuildLoad2(
886+
builder, compileSpecialized ? LLVMGetAllocatedType(valptr) : nextType,
887+
valptr, "__var_load");
864888
}
865889

866890
LLVMValueRef JITCodegen::visit(LiteralExpression *e) {
867-
return getConstantInt(e->value.val.value);
891+
if(!compileSpecialized) {
892+
return getConstantInt(e->value.val.value);
893+
} else {
894+
if(e->value.isInteger()) {
895+
return getConstantSInt(e->value.toInteger());
896+
} else if(e->value.isNumber()) {
897+
return getConstantDouble(e->value.toNumber());
898+
} else if(e->value.isBoolean()) {
899+
return getConstantInt(e->value.toBoolean(), 1);
900+
} else {
901+
panic("Unknown literal: ", e->value, "!");
902+
}
903+
}
868904
}
869905

870906
LLVMValueRef JITCodegen::visit(GroupingExpression *e) {

stdlib/jit/jitcodegen.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ struct JITCodegen : LLVMCodegenBase {
5151

5252
void positionBuilderAtEnd(LLVMBasicBlockRef block);
5353

54-
static LLVMValueRef getConstantInt(uint64_t val);
54+
static LLVMValueRef getConstantInt(uint64_t val, int width = 64);
55+
static LLVMValueRef getConstantSInt(int64_t val);
56+
static LLVMValueRef getConstantDouble(double val);
5557

5658
void init();
5759
void initEngine();

stringvalues.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SCONSTANT(str, "str")
2323
SCONSTANT(nil, "nil")
2424
SCONSTANT(i64, "i64")
2525
SCONSTANT(f64, "f64")
26-
SCONSTANT(i1, "i1")
26+
SCONSTANT(bool_, "bool")
2727
#define TYPE(r, n) SCONSTANT(type_##n, #n)
2828
#include "valuetypes.h"
2929

0 commit comments

Comments
 (0)