Skip to content

Commit 193edc1

Browse files
committed
Add compiler interface to repl and various other fixes
1 parent 81448a5 commit 193edc1

File tree

9 files changed

+96
-59
lines changed

9 files changed

+96
-59
lines changed

codegen/metac_codegen.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,13 @@ static BCValue MetaCCodegen_doFunction(metac_bytecode_ctx_t* ctx,
533533
return imm32(f.FunctionIndex);
534534
}
535535

536+
#include "../compiler_intrinsics/metac_compiler_interface.h"
537+
538+
extern metac_compiler_t compiler;
539+
536540
static void InitCompilerInterface(metac_bytecode_ctx_t* ctx)
537541
{
538-
#ifdef METAC_COMPILER_INTERFACE
542+
// #ifdef METAC_COMPILER_INTERFACE
539543
void* c = ctx->c;
540544
const BackendInterface gen = *ctx->gen;
541545

@@ -549,8 +553,9 @@ static void InitCompilerInterface(metac_bytecode_ctx_t* ctx)
549553
ctx->CompilerInterfaceValue = gen.GenExternal(c, compilerInterfaceType, ".compiler");
550554
gen.MapExternal(c, &ctx->CompilerInterfaceValue, &compiler, sizeof(compiler));
551555
ctx->Externals[0].ExtValue = ctx->CompilerInterfaceValue;
556+
ctx->ExternalsCount += 1;
552557
}
553-
#endif
558+
// #endif
554559
}
555560

556561
bool IsExternal(metac_sema_expr_t* expr)
@@ -618,10 +623,7 @@ metac_bytecode_function_t MetaCCodegen_GenerateFunctionFromExp(metac_bytecode_ct
618623
func.FunctionIndex =
619624
gen.BeginFunction(c, 0, "dummy_eval_func");
620625

621-
if (!compilerInterfaceInited)
622-
{
623-
InitCompilerInterface(ctx);
624-
}
626+
InitCompilerInterface(ctx);
625627

626628
// we need to introduce all resolvable variables from the outer context here.
627629
// .compiler.help - .compiler
@@ -1160,7 +1162,9 @@ static void MetaCCodegen_doArrowExpr(metac_bytecode_ctx_t* ctx,
11601162
field = e2->Field;
11611163
offsetVal = imm32(field->Offset);
11621164

1163-
assert(e1Value.vType == BCValueType_StackValue);
1165+
assert(e1Value.vType == BCValueType_StackValue
1166+
|| e1Value.vType == BCValueType_HeapValue
1167+
|| e1Value.vType == BCValueType_External);
11641168
gen.Add3(c, &addr, &e1Value, &offsetVal);
11651169

11661170
MetaCCodegen_doDeref(ctx, &addr, field->Type, result);
@@ -1196,10 +1200,7 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
11961200
{
11971201
if (!result)
11981202
{
1199-
// XXX: this is super dangerous
1200-
// FIXME remove stackref as soon as possible!
1201-
BCValue tmp = {BCValueType_Unknown};
1202-
result = &tmp;
1203+
assert(!"result must be non null");
12031204
}
12041205

12051206
if (op == expr_signed_integer)
@@ -1242,6 +1243,7 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
12421243
}
12431244
else if (op == expr_addr)
12441245
{
1246+
assert(exp->E1->Kind == expr_variable);
12451247
}
12461248
else if (op == expr_dot)
12471249
{

compiler_intrinsics/metac_compiler_interface.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include "../os/os.h"
12
#include "metac_compiler_interface.h"
3+
#include <stdio.h>
24
extern metac_compiler_t compiler;
35

46
/*
@@ -82,23 +84,7 @@ void compiler_PrintInt(int32_t* value)
8284
fprintf(stderr, "-> %s\n", Buffer);
8385
}
8486

85-
metac_parser_t* GetCurrentParser(metac_compiler_t* compilerP)
86-
{
87-
88-
}
8987

90-
void compiler_RegisterIdentifierCallback (metac_compiler_t* compilerP,
91-
void (*IdentifierCb)(const char* idChars, uint32_t idKey, void* userCtx),
92-
void* userContext)
93-
{
94-
metac_parser_t* parser = GetCurrentParser(compilerP);
95-
identifier_callback_t idCallback;
96-
97-
idCallback.FuncP = IdentifierCb;
98-
idCallback.Ctx = userContext;
99-
100-
ARENA_ARRAY_ADD(parser->IdentifierCallbacks, idCallback);
101-
}
10288

10389
metac_compiler_t compiler = {
10490
0,
@@ -120,7 +106,7 @@ metac_compiler_t compiler = {
120106

121107
0, // register log callback
122108

123-
compiler_RegisterIdentifierCallback,
109+
0, // register identifier callback
124110

125111
compiler_PrintInt,
126112
};

compiler_intrinsics/metac_compiler_interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ typedef struct metac_compiler_t
5151
void* context);
5252
void (*PrintInt) (int32_t* value);
5353
} metac_compiler_t;
54+
5455
/* Proposed Interface:
5556
typedef struct {
5657
// Other members...

libinterpret/bc_interpreter_backend.c

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,17 +2644,50 @@ static inline void BCGen_emitArithInstruction(BCGen* self
26442644
, const BCValue* rhs
26452645
, BCTypeEnum* resultTypeEnum);
26462646

2647+
static inline void BCGen_Set(BCGen* self, BCValue* lhs, const BCValue* rhs)
2648+
{
2649+
assert(BCValue_isStackValueOrParameter(lhs));//, "Set lhs is has to be a StackValue. Not: " ~ enumToString(lhs.vType));
2650+
assert(rhs->vType == BCValueType_Immediate || rhs->vType == BCValueType_External || BCValue_isStackValueOrParameter(rhs));//, "Set rhs is has to be a StackValue or Imm not: " ~ rhs.vType.enumToString);
2651+
2652+
if (rhs->vType == BCValueType_Immediate)
2653+
{
2654+
if(rhs->type.type == BCTypeEnum_i64 || rhs->type.type == BCTypeEnum_u64 || rhs->type.type == BCTypeEnum_f52)
2655+
{
2656+
BCGen_emitLongInstSI(self, LongInst_SetImm32, lhs->stackAddr, (rhs->imm64.imm64 & UINT32_MAX));
2657+
BCGen_emitLongInstSI(self, LongInst_SetHighImm32, lhs->stackAddr, (rhs->imm64.imm64 >> 32));
2658+
}
2659+
else
2660+
{
2661+
BCGen_emitLongInstSI(self, LongInst_SetImm32, lhs->stackAddr, rhs->imm32.imm32);
2662+
}
2663+
}
2664+
else if (!BCValue_eq(lhs, rhs)) // do not emit self assignments;
2665+
{
2666+
BCValue tmp = *lhs;
2667+
BCGen_emitArithInstruction(self, LongInst_Set, lhs, rhs, 0);
2668+
}
2669+
}
2670+
26472671
static inline BCValue BCGen_castTo(BCGen* self, const BCValue* rhs, BCTypeEnum targetType)
26482672
{
2673+
bool rhsIsTemp = false;
2674+
BCValue rhsAsTemp = {0};
26492675
BCTypeEnum sourceType = rhs->type.type;
26502676

2677+
BCType type = {targetType};
2678+
BCValue lhs = BCGen_genTemporary(self, type);
2679+
26512680
if (sourceType == targetType)
26522681
return *rhs;
26532682

2654-
BCType type = {targetType};
2655-
BCValue lhs = BCGen_genTemporary(self, type);
26562683

2657-
assert(BCValue_isStackValueOrParameter(rhs));
2684+
if (!BCValue_isStackValueOrParameter(rhs))
2685+
{
2686+
rhsIsTemp = true;
2687+
rhsAsTemp = BCGen_genTemporary(self, rhs->type);
2688+
BCGen_Set(self, &rhsAsTemp, rhs);
2689+
rhs = &rhsAsTemp;
2690+
}
26582691

26592692
switch(targetType)
26602693
{
@@ -2694,31 +2727,12 @@ static inline BCValue BCGen_castTo(BCGen* self, const BCValue* rhs, BCTypeEnum t
26942727
//break;
26952728
}
26962729

2697-
return lhs;
2698-
}
2699-
2700-
static inline void BCGen_Set(BCGen* self, BCValue* lhs, const BCValue* rhs)
2701-
{
2702-
assert(BCValue_isStackValueOrParameter(lhs));//, "Set lhs is has to be a StackValue. Not: " ~ enumToString(lhs.vType));
2703-
assert(rhs->vType == BCValueType_Immediate || rhs->vType == BCValueType_External || BCValue_isStackValueOrParameter(rhs));//, "Set rhs is has to be a StackValue or Imm not: " ~ rhs.vType.enumToString);
2704-
2705-
if (rhs->vType == BCValueType_Immediate)
2706-
{
2707-
if(rhs->type.type == BCTypeEnum_i64 || rhs->type.type == BCTypeEnum_u64 || rhs->type.type == BCTypeEnum_f52)
2708-
{
2709-
BCGen_emitLongInstSI(self, LongInst_SetImm32, lhs->stackAddr, (rhs->imm64.imm64 & UINT32_MAX));
2710-
BCGen_emitLongInstSI(self, LongInst_SetHighImm32, lhs->stackAddr, (rhs->imm64.imm64 >> 32));
2711-
}
2712-
else
2713-
{
2714-
BCGen_emitLongInstSI(self, LongInst_SetImm32, lhs->stackAddr, rhs->imm32.imm32);
2715-
}
2716-
}
2717-
else if (!BCValue_eq(lhs, rhs)) // do not emit self assignments;
2730+
if (rhsIsTemp)
27182731
{
2719-
BCValue tmp = *lhs;
2720-
BCGen_emitArithInstruction(self, LongInst_Set, lhs, rhs, 0);
2732+
BCGen_destroyTemporary(self, &rhsAsTemp);
27212733
}
2734+
2735+
return lhs;
27222736
}
27232737

27242738
static inline BCValue BCGen_pushTemporary(BCGen* self, const BCValue* val)

libinterpret/printer_backend.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ static inline void Printer_Finalize(Printer* self)
568568
{
569569
Printer_PutStr(self, "Finalize()");
570570
Printer_PutNewline(self);
571+
*self->Buffer++ = '\0';
571572
}
572573

573574
static inline uint32_t Printer_BeginFunction(Printer* self, uint32_t fnIdx, const char* name)

parser/metac_expr_parser.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,9 @@ metac_expr_t* MetaCParser_ParsePrimaryExpr(metac_parser_t* self, parse_expr_flag
669669
metac_token_t* lParen = MetaCParser_Match(self, tok_lParen);
670670
metac_type_modifiers typeModifiers = ParseTypeModifiers(self);
671671

672-
if (IsTypeToken(MetaCParser_PeekToken(self, 1)->TokenType))
672+
metac_token_t* peek = MetaCParser_PeekToken(self, 1);
673+
674+
if (peek && IsTypeToken(peek->TokenType))
673675
{
674676
result->CastType = MetaCParser_ParseTypeDecl(self, 0, 0);
675677
result->CastType->TypeModifiers = typeModifiers;
@@ -855,14 +857,18 @@ metac_expr_t* MetaCParser_ParsePrimaryExpr(metac_parser_t* self, parse_expr_flag
855857
}
856858
}
857859

858-
//PushOperator(expr_paren);
860+
//PushOperator(expr_paren);[New Thread 0x7ffff76fb640 (LWP 2094)]
861+
859862
//PushOperand(result);
860863

861864
metac_token_t* endParen =
862865
MetaCParser_Match(self, tok_rParen);
863-
MetaCLocation_Expand(&loc, LocationFromToken(self, endParen));
866+
if (endParen)
867+
{
868+
MetaCLocation_Expand(&loc, LocationFromToken(self, endParen));
864869

865-
self->ExprParser.OpenParens--;
870+
self->ExprParser.OpenParens--;
871+
}
866872

867873
//PopOperator(expr_paren);
868874
}

repl/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $CC $CFLAGS linenoise_repl.c \
1717
-DACOSW_EXTERNAL_ASM ../3rd_party/libaco/acosw.S \
1818
-I.. -DACCEL=$ACCEL \
1919
-g3 -O0 \
20-
-mtune=core2 -march=core2 \
20+
-mtune=core2 -march=core2 -mstackrealign \
2121
-lm -lpthread \
2222
-o repl $@
2323
#-march=native -mtune=native \

repl/repl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "../parser/metac_parser_obj.c"
1010
#include "../semantic/metac_semantic_obj.c"
11+
#include "../compiler_intrinsics/metac_compiler_interface.c"
1112
#include "../driver/metac_driver.c"
1213
#include "../os/bsr.h"
1314
#include "../hash/crc32c.h"

semantic/metac_expr_semantic.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void ConvertTupleElementToExp(metac_sema_state_t* sema,
8282
}
8383

8484
EXTERN_C const BackendInterface BCGen_interface;
85+
EXTERN_C const BackendInterface Printer_interface;
86+
8587
metac_sema_expr_t
8688
EvaluateExpr(metac_sema_state_t* sema,
8789
metac_sema_expr_t* e,
@@ -98,6 +100,30 @@ EvaluateExpr(metac_sema_state_t* sema,
98100

99101
Allocator_Init(&interpAlloc, &sema->TempAlloc, 0);
100102

103+
if (false)
104+
{
105+
const char* result;
106+
107+
MetaCCodegen_SetDefaultInterface(&Printer_interface);
108+
109+
MetaCCodegen_Init(&ctx, &interpAlloc);
110+
ctx.Sema = sema;
111+
112+
for(uint32_t i = 0; i < sema->GlobalsCount; i++)
113+
{
114+
MetaCCodegen_doGlobal(&ctx, sema->Globals[i], i);
115+
}
116+
117+
MetaCCodegen_Begin(&ctx, sema->ParserIdentifierTable, sema->ParserStringTable, sema);
118+
119+
metac_bytecode_function_t fCode =
120+
MetaCCodegen_GenerateFunctionFromExp(&ctx, e);
121+
122+
MetaCCodegen_End(&ctx);
123+
124+
Printer_StreamToFile(ctx.c, stderr);
125+
}
126+
101127
MetaCCodegen_SetDefaultInterface(&BCGen_interface);
102128

103129
MetaCCodegen_Init(&ctx, 0);

0 commit comments

Comments
 (0)