Skip to content

Commit 95c661b

Browse files
committed
A couple fixes and a better printpit
1 parent 5ffdb66 commit 95c661b

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

codegen/metac_codegen.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ BCType MetaCCodegen_GetBCType(metac_bytecode_ctx_t* ctx, metac_type_index_t type
6868
result.type = BCTypeEnum_i32;
6969
break;
7070
case type_long:
71+
case type_long_long:
7172
result.type = BCTypeEnum_i64;
7273
break;
7374
case type_size_t:
@@ -82,9 +83,6 @@ BCType MetaCCodegen_GetBCType(metac_bytecode_ctx_t* ctx, metac_type_index_t type
8283
result.type = BCTypeEnum_f52;
8384
break;
8485

85-
case type_long_long:
86-
result.type = BCTypeEnum_i64;
87-
break;
8886
case type_long_double:
8987
result.type = BCTypeEnum_f106;
9088
break;
@@ -99,8 +97,6 @@ BCType MetaCCodegen_GetBCType(metac_bytecode_ctx_t* ctx, metac_type_index_t type
9997
result.type = BCTypeEnum_u32;
10098
break;
10199
case type_unsigned_long:
102-
result.type = BCTypeEnum_u64;
103-
break;
104100
case type_unsigned_long_long:
105101
result.type = BCTypeEnum_u64;
106102
break;
@@ -622,7 +618,7 @@ metac_bytecode_function_t MetaCCodegen_GenerateFunctionFromExp(metac_bytecode_ct
622618
#ifdef PRINT_BYTECODE
623619
if (ctx->gen == &BCGen_interface)
624620
{
625-
BCGen_PrintCode((BCGen*)c, 0, 48);
621+
BCGen_PrintCode((BCGen*)c, 0, 128);
626622
}
627623
#endif
628624

@@ -887,7 +883,21 @@ void MetaCCodegen_doDeref(metac_bytecode_ctx_t* ctx,
887883
BackendInterface gen = *ctx->gen;
888884
void* c = ctx->c;
889885
// this only works for pointers to 32bit basic types
890-
gen.Load32(c, result, addr);
886+
if (addr->vType == BCValueType_External)
887+
{
888+
if (sizeof(void*) == 4)
889+
{
890+
gen.Load32(c, result, addr);
891+
}
892+
else if (sizeof(void*) == 8)
893+
{
894+
gen.Load64(c, result, addr);
895+
}
896+
}
897+
else
898+
{
899+
gen.Load32(c, result, addr);
900+
}
891901
}
892902

893903
static void MetaCCodegen_doCastExpr(metac_bytecode_ctx_t* ctx,
@@ -961,6 +971,8 @@ static void MetaCCodegen_doDotExpr(metac_bytecode_ctx_t* ctx,
961971
BCValue addr;
962972
BCValue e1Value = {BCValueType_Unknown};
963973
BCValue offsetVal = {BCValueType_Unknown};
974+
BCValue addr_u32;
975+
BCValue e1Value_u32;
964976

965977
assert(exp->Kind == expr_dot);
966978
assert(idxKind == type_index_struct);
@@ -980,7 +992,9 @@ static void MetaCCodegen_doDotExpr(metac_bytecode_ctx_t* ctx,
980992

981993
assert(e1Value.vType == BCValueType_StackValue);
982994

983-
gen.Add3(c, &addr, &e1Value, &offsetVal);
995+
addr_u32 = BCValue_u32(&addr);
996+
e1Value_u32 = BCValue_u32(&e1Value);
997+
gen.Add3(c, &addr_u32, &e1Value_u32, &offsetVal);
984998

985999
MetaCCodegen_doDeref(ctx, &addr, field->Type, result);
9861000
}
@@ -1005,6 +1019,8 @@ static void MetaCCodegen_doArrowExpr(metac_bytecode_ctx_t* ctx,
10051019
BCValue e1Value = {BCValueType_Unknown};
10061020
BCValue offsetVal = {BCValueType_Unknown};
10071021
bool isExternal = false;
1022+
BCValue e1Value_u32;
1023+
BCValue addr_u32;
10081024

10091025
assert(idxKind == type_index_ptr);
10101026
assert(exp->Kind == expr_arrow || exp->Kind == expr_dot);
@@ -1037,7 +1053,10 @@ static void MetaCCodegen_doArrowExpr(metac_bytecode_ctx_t* ctx,
10371053
|| e1Value.vType == BCValueType_HeapValue
10381054
|| e1Value.vType == BCValueType_External);
10391055

1040-
gen.Add3(c, &addr, &e1Value, &offsetVal);
1056+
addr_u32 = BCValue_u32(&addr);
1057+
e1Value_u32 = BCValue_u32(&e1Value);
1058+
gen.Add3(c, &addr_u32, &e1Value_u32, &offsetVal);
1059+
10411060
gen.Comment(c, "Address pointer was just generated as the result of an Add3");
10421061
MetaCCodegen_doDeref(ctx, &addr, field->Type, result);
10431062
gen.Comment(c, "doDref was executed ... ");
@@ -1169,7 +1188,8 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
11691188
if (exp->E2->Kind == expr_signed_integer
11701189
&& (exp->E2->ValueI64 >= INT32_MIN && exp->E2->ValueI64 <= INT32_MAX))
11711190
{
1172-
rhs = imm32_(cast(int32_t)exp->E2->ValueI64, true);
1191+
bool isSigned = TypeIndex_isSigned(exp->E2->TypeIndex);
1192+
rhs = imm32_(cast(int32_t)exp->E2->ValueI64, isSigned);
11731193
}
11741194
else
11751195
{

libinterpret/bc_common.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,13 @@ static const BCType BCType_i32 = {BCTypeEnum_i32};
958958
static const BCType BCType_u32 = {BCTypeEnum_u32};
959959

960960

961+
static inline BCValue BCValue_u32(const BCValue* v)
962+
{
963+
BCValue result = *v;
964+
result.type = BCType_u32;
965+
return result;
966+
}
967+
961968
#undef offsetof
962969

963970
#endif

libinterpret/bc_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ static const BCType BCType_u32;
429429

430430

431431
EXTERN_C BCTypeEnum BCTypeEnum_commonTypeEnum(BCTypeEnum lhs, BCTypeEnum rhs);
432+
static inline BCValue BCValue_u32(const BCValue* v);
433+
432434
static inline void AllocDefaultHeap(BCHeap* newHeap);
433435

434436
#undef offsetof

libinterpret/bc_interpreter_backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ void BCGen_PrintCode(BCGen* self, uint32_t start, uint32_t end)
10211021
{
10221022
printf("LongInst_Comment [length:%d]\n", hi);
10231023
uint32_t nWords = ((hi + 7) & ~7) / 4;
1024-
printf(" %.*s", (int) hi, (const char*) (codeP + ip));
1024+
printf(" %.*s\n", (int) hi, (const char*) (codeP + ip));
10251025
ip += nWords;
10261026
}
10271027
break;

0 commit comments

Comments
 (0)