commit f6e8bf748e04812989773403d86d8d3536614375
parent 4e954d398a4ac44ed59ad4fbc8b3ed26f454bd6a
Author: Brian Swetland <swetland@frotz.net>
Date: Sun, 14 May 2023 14:41:38 -0700
change how types, variables, fields, etc are represented in the generated C code
This makes the output a little easier on the eyes.
Diffstat:
6 files changed, 41 insertions(+), 35 deletions(-)
diff --git a/compiler0.c b/compiler0.c
@@ -45,6 +45,7 @@ enum {
SYMBOL_VAR,
SYMBOL_FLD, // struct field
SYMBOL_PTR, // struct *field
+ SYMBOL_DEF, // enum
};
struct Scope {
@@ -357,6 +358,7 @@ void emit(FILE* fp, const char *fmt, ...) {
va_end(ap);
}
+#if 0
void emit_type(FILE *fp, Type *type) {
if ((type->kind == TYPE_SLICE) || (type->kind == TYPE_ARRAY)) {
emit(fp, "t$s$");
@@ -377,7 +379,7 @@ void emit_vref(FILE *fp, Symbol *sym) {
emit(fp, "v$%s", sym->name->text);
}
}
-
+#endif
void ctx_open_source(const char* filename) {
ctx.filename = filename;
@@ -833,7 +835,7 @@ void parse_ident(void) {
if (ctx.tok == tOPAREN) { // function call
next();
- emit(IMPL,"f$%s(", name->text);
+ emit(IMPL,"fn_%s(", name->text);
while (ctx.tok != tCPAREN) {
parse_expr();
if (ctx.tok != tCPAREN) {
@@ -847,24 +849,28 @@ void parse_ident(void) {
next();
String *fieldname = parse_name("field name");
Symbol *field = type_find_field(sym->type, fieldname);
- emit(IMPL,"(v$%s->f$%s)", name->text, fieldname->text);
+ emit(IMPL,"($%s->%s)", name->text, fieldname->text);
} else if (ctx.tok == tOBRACK) { // array access
next();
// XXX handle slices
if (sym->type->kind != TYPE_ARRAY) {
error("cannot access '%s' as an array", name->text);
}
- emit(IMPL,"(v$%s[", name->text);
+ emit(IMPL,"($%s[", name->text);
parse_expr();
emit(IMPL,"])");
} else { // variable access
- emit(IMPL,"v$%s", sym->name->text);
+ if (sym->kind == SYMBOL_DEF) {
+ emit(IMPL,"c$%s", sym->name->text);
+ } else {
+ emit(IMPL,"$%s", sym->name->text);
+ }
}
}
void parse_primary_expr(void) {
if (ctx.tok == tNUM) {
- emit(IMPL,"0x%08x", ctx.num);
+ emit(IMPL,"0x%x", ctx.num);
} else if (ctx.tok == tSTR) {
error("<TODO> string const");
} else if (ctx.tok == tTRUE) {
@@ -985,8 +991,8 @@ Type *parse_struct_type(String *name) {
Type *rectype = type_make(name, TYPE_STRUCT, nil, nil, 0);
scope_push(SCOPE_STRUCT);
require(tOBRACE);
- emit(TYPE,"typedef struct t$%s t$%s;\n", name->text, name->text);
- emit(TYPE,"struct t$%s {\n", name->text);
+ emit(TYPE,"typedef struct %s_t %s_t;\n", name->text, name->text);
+ emit(TYPE,"struct %s_t {\n", name->text);
while (true) {
if (ctx.tok == tCBRACE) {
next();
@@ -996,7 +1002,7 @@ Type *parse_struct_type(String *name) {
bool ptr = (ctx.tok == tSTAR);
if (ptr) next();
Type *type = parse_type(false);
- emit(TYPE," t$%s %sf$%s;\n", type->name->text, ptr ? "*" : "", fname->text);
+ emit(TYPE," %s_t %s%s;\n", type->name->text, ptr ? "*" : "", fname->text);
Symbol *sym = symbol_make(fname, type);
sym->kind = ptr ? SYMBOL_PTR : SYMBOL_FLD;
if (ctx.tok != tCBRACE) {
@@ -1208,19 +1214,19 @@ void parse_var(void) {
next();
if (ctx.tok == tOBRACE) {
next();
- emit(IMPL,"t$%s v$$%s = {\n", type->name->text, name->text);
+ emit(IMPL,"%s_t $$%s = {\n", type->name->text, name->text);
parse_struct_init(var);
- emit(IMPL,"\n};\nt$%s *v$%s = &v$$%s;\n",
+ emit(IMPL,"\n};\n%s_t *$%s = &$$%s;\n",
type->name->text, name->text, name->text);
} else {
- emit(IMPL,"t$%s %sv$%s = ", type->name->text,
+ emit(IMPL,"%s_t %s$%s = ", type->name->text,
(type->kind == TYPE_STRUCT) ? "*" : "",
name->text);
parse_expr();
emit(IMPL,";\n");
}
} else {
- emit(IMPL,"t$%s %sv$%s = 0;", type->name->text,
+ emit(IMPL,"%s_t %s$%s = 0;", type->name->text,
(type->kind == TYPE_STRUCT) ? "*" : "",
name->text);
}
@@ -1319,17 +1325,17 @@ void parse_function(void) {
rtype = parse_type(false);
}
- emit(DECL,"t$%s f$%s(", rtype->name->text, fname->text);
- emit(IMPL,"t$%s f$%s(", rtype->name->text, fname->text);
+ emit(DECL,"%s_t fn_%s(", rtype->name->text, fname->text);
+ emit(IMPL,"%s_t fn_%s(", rtype->name->text, fname->text);
for (Symbol *s = ctx.scope->first; s != nil; s = s->next) {
- emit(DECL,"t$%s %sv$%s%s",
+ emit(DECL,"%s_t %s$%s%s",
s->type->name->text,
s->type->kind == TYPE_STRUCT ? "*" : "",
- s->name->text, s->next ? "," : "");
- emit(IMPL,"t$%s %sv$%s%s",
+ s->name->text, s->next ? ", " : "");
+ emit(IMPL,"%s_t %s$%s%s",
s->type->name->text,
s->type->kind == TYPE_STRUCT ? "*" : "",
- s->name->text, s->next ? "," : "");
+ s->name->text, s->next ? ", " : "");
}
emit(DECL,"%s);\n", ctx.scope->first ? "" : "void");
emit(IMPL,"%s) {\n", ctx.scope->first ? "" : "void");
@@ -1362,8 +1368,8 @@ void parse_enum_def(void) {
val = ctx.num;
}
require(tCOMMA);
- symbol_make_global(name, ctx.type_u32);
- emit(DECL,"static const t$u32 v$%s = 0x%08x;\n", name->text, val);
+ symbol_make_global(name, ctx.type_u32)->kind = SYMBOL_DEF;
+ emit(DECL,"#define c$%s = 0x%x;\n", name->text, val);
val++;
}
require(tCBRACE);
diff --git a/inc/builtin.type.h b/inc/builtin.type.h
@@ -1,11 +1,11 @@
#include <stdint.h>
-typedef void t$void;
+typedef void void_t;
-typedef uint32_t t$u32;
-typedef int32_t t$i32;
-typedef uint16_t t$u16;
-typedef int16_t t$i16;
-typedef uint8_t t$u8;
-typedef int8_t t$i8;
+typedef uint32_t u32_t;
+typedef int32_t i32_t;
+typedef uint16_t u16_t;
+typedef int16_t i16_t;
+typedef uint8_t u8_t;
+typedef int8_t i8_t;
diff --git a/inc/library.impl.c b/inc/library.impl.c
@@ -1,10 +1,10 @@
-void f$_hexout_(int x) {
+void fn__hexout_(int x) {
printf("D %08x\n", x);
}
int main(int argc, char** argv) {
- int x = f$start();
+ int x = fn_start();
printf("X %08x\n", x);
return 0;
}
diff --git a/inc/library.impl.h b/inc/library.impl.h
@@ -1,3 +1,3 @@
#include <stdio.h>
-t$void f$_hexout_(t$i32 x);
+void fn__hexout_(i32_t x);
diff --git a/test/1041-arrays.src b/test/1041-arrays.src
@@ -1,10 +1,10 @@
-var numbers [8]byte = { 1, 2, 4, 8, 16, 32, 64, 128 };
+var numbers [8]u8 = { 1, 2, 4, 8, 16, 32, 64, 128 };
-var data [8]byte = { 0x10, 0x20, 0x30, 0x40,
+var data [8]u8 = { 0x10, 0x20, 0x30, 0x40,
0x50, 0x60, 0x70, 0x80 };
-func dump(x [8]byte) {
+func dump(x [8]u8) {
var n i32 = 0;
while (n < 8) {
_hexout_(x[n]);
diff --git a/test/1042-arrays2.src b/test/1042-arrays2.src
@@ -1,5 +1,5 @@
-var data [8]byte;
+var data [8]u8;
func start() i32 {
var n i32 = 0;