Skip to content

Commit eeeb0d7

Browse files
committed
Implement return values
1 parent b48424e commit eeeb0d7

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ UMKA_LIB_DYNAMIC = $(BUILD_PATH)/libumka.$(LIBEXT)
3939
UMKA_EXE = $(BUILD_PATH)/umka
4040

4141
#CFLAGS = -s -fPIC -O3 -Wall -Wno-format-security -malign-double -fno-strict-aliasing -DUMKA_EXT_LIBS
42-
CFLAGS = -fPIC -O3 -ggdb -Wall -Wno-format-security -malign-double -fno-strict-aliasing -DUMKA_EXT_LIBS $(LIBS) $(INCLUDE)
42+
CFLAGS = -fPIC -ggdb -Wall -Wno-format-security -malign-double -fno-strict-aliasing -DUMKA_EXT_LIBS $(LIBS) $(INCLUDE)
4343
STATIC_CFLAGS = $(CFLAGS) -DUMKA_STATIC
4444
DYNAMIC_CFLAGS = $(CFLAGS) -DUMKA_BUILD $(DYNAMIC_CFLAGS_EXTRA)
4545

src/umka_gen.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ typedef struct
4747
typedef struct
4848
{
4949
void *entry;
50-
int numArgs;
5150
ffi_cif cif;
5251
} DynamicCall;
5352

src/umka_stmt.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "umka_common.h"
12
#include "umka_gen.h"
23
#include "umka_lexer.h"
34
#include "umka_types.h"
@@ -124,7 +125,7 @@ ffi_type *mapToFfiType(Umka *umka,const struct tagType *type) {
124125
case TYPE_PTR:
125126
return &ffi_type_uint64;
126127
case TYPE_BOOL:
127-
return &ffi_type_uchar;
128+
return &ffi_type_uint8;
128129
case TYPE_CHAR:
129130
return &ffi_type_uchar;
130131

@@ -133,7 +134,6 @@ ffi_type *mapToFfiType(Umka *umka,const struct tagType *type) {
133134
// probabbly should be only created once for each struct, then looked up here
134135
return mapToFfiStruct(umka, type->typeIdent->type);
135136

136-
137137
// float types
138138
case TYPE_REAL32:
139139
return &ffi_type_float;
@@ -171,7 +171,10 @@ int assignFfiTypes(Umka *umka, ffi_type **types, const Signature *sig)
171171
for(int i = 0; i < sig->numParams && i < 16; i++) {
172172
const Param *param = sig->param[i];
173173
ffi_type *type = mapToFfiType(umka, param->type);
174-
if (type == NULL) continue;
174+
175+
if (strcmp(param->name, "#upvalues") == 0) continue;
176+
if (strcmp(param->name, "#result") == 0) continue;
177+
175178
types[numArgs++] = type;
176179
}
177180
return numArgs;
@@ -225,13 +228,13 @@ void doResolveExtern(Umka *umka)
225228

226229
// todo: free?
227230
ffi_type **types = malloc(sizeof(ffi_type)*16);
228-
dynamicCall->numArgs = assignFfiTypes(umka, types, &ident->type->sig);
229-
230231
ffi_type *retType = mapToFfiType(umka, ident->type->sig.resultType);
232+
int numArgs = assignFfiTypes(umka, types, &ident->type->sig);
233+
231234
if (retType == NULL)
232235
umka->error.handler(umka->error.context, "Unsupported return type for ffi function: %d", ident->type->sig.resultType->typeIdent->name);
233236

234-
ffi_status status = ffi_prep_cif(&dynamicCall->cif, FFI_DEFAULT_ABI, dynamicCall->numArgs, retType, types);
237+
ffi_status status = ffi_prep_cif(&dynamicCall->cif, FFI_DEFAULT_ABI, numArgs, retType, types);
235238
if (status != FFI_OK)
236239
umka->error.handler(umka->error.context, "Error creating ffi_cif: %d", status);
237240

src/umka_vm.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,18 +3569,25 @@ static FORCE_INLINE void doCallExtern(Fiber *fiber, Error *error)
35693569
fiber->ip++;
35703570
}
35713571

3572-
3573-
static void ffiEntry(UmkaStackSlot *params, UmkaStackSlot *result, const DynamicCall *dynamicCall)
3572+
static FORCE_INLINE void ffiEntry(UmkaStackSlot *params, UmkaStackSlot *result, const DynamicCall *dynamicCall)
35743573
{
35753574
void* args[16] = {0};
3576-
void *retVal = NULL;
3577-
3578-
for (int i = 0; i < dynamicCall->numArgs; i++) {
3575+
for (unsigned int i = 0; i < dynamicCall->cif.nargs; i++) {
35793576
args[i] = &umkaGetParam(params, i)->ptrVal;
35803577
}
35813578

3582-
ffi_call((ffi_cif *)&dynamicCall->cif, dynamicCall->entry, retVal, args);
3583-
// todo retval
3579+
UmkaStackSlot *resultSlot = umkaGetResult(params, result);
3580+
void* retPtr = resultSlot;
3581+
if (dynamicCall->cif.rtype->type == FFI_TYPE_STRUCT) {
3582+
retPtr = resultSlot->ptrVal;
3583+
}
3584+
3585+
ffi_call((ffi_cif *)&dynamicCall->cif, dynamicCall->entry, retPtr, args);
3586+
// workaround for real32 -> cast float to double
3587+
if (dynamicCall->cif.rtype->type == FFI_TYPE_FLOAT) {
3588+
resultSlot->realVal = resultSlot->real32Val;
3589+
}
3590+
35843591
}
35853592

35863593

0 commit comments

Comments
 (0)