@@ -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
146154LLVMValueRef JITCodegen::defineIsNumber (LLVMValueRef func) {
@@ -397,7 +405,7 @@ LLVMValueRef JITCodegen::generateCastOrReturn(LLVMTypeRef targetType,
397405}
398406
399407LLVMTypeRef 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
458466LLVMValueRef 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
861883LLVMValueRef 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
866890LLVMValueRef 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
870906LLVMValueRef JITCodegen::visit (GroupingExpression *e) {
0 commit comments