WIP: dev #1
@@ -21,9 +21,9 @@ public class Generator
|
|||||||
private readonly ModuleGraph moduleGraph;
|
private readonly ModuleGraph moduleGraph;
|
||||||
private readonly string? entryPoint;
|
private readonly string? entryPoint;
|
||||||
private IndentedTextWriter writer = new();
|
private IndentedTextWriter writer = new();
|
||||||
private HashSet<NubType> referencedTypes = new();
|
private readonly HashSet<NubType> referencedTypes = [];
|
||||||
private readonly Dictionary<string, string> referencedStringLiterals = new();
|
private readonly Dictionary<string, string> referencedStringLiterals = [];
|
||||||
private readonly HashSet<NubType> emittedTypes = new();
|
private readonly HashSet<NubType> emittedTypes = [];
|
||||||
private readonly Stack<Scope> scopes = new();
|
private readonly Stack<Scope> scopes = new();
|
||||||
private int tmpNameIndex = 0;
|
private int tmpNameIndex = 0;
|
||||||
|
|
||||||
@@ -34,22 +34,7 @@ public class Generator
|
|||||||
writer.WriteLine($$"""
|
writer.WriteLine($$"""
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct nub_dynamic_array_52747d8c11b7118c args = (struct nub_dynamic_array_52747d8c11b7118c){0};
|
return {{entryPoint}}();
|
||||||
|
|
||||||
for (int i = 0; i < argc; ++i)
|
|
||||||
{
|
|
||||||
auto nubstring = nub_core_string_from_cstr(argv[i]);
|
|
||||||
da_append(&args, nubstring);
|
|
||||||
}
|
|
||||||
|
|
||||||
int returnValue = {{entryPoint}}(args);
|
|
||||||
|
|
||||||
for (int i = 0; i < args.count; ++i)
|
|
||||||
{
|
|
||||||
nub_core_string_rc_dec(args.items[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return returnValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
""");
|
""");
|
||||||
@@ -108,15 +93,32 @@ public class Generator
|
|||||||
|
|
||||||
writer = new IndentedTextWriter();
|
writer = new IndentedTextWriter();
|
||||||
|
|
||||||
writer.WriteLine("""
|
writer.WriteLine(
|
||||||
#include <float.h>
|
"""
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *data;
|
||||||
|
size_t length;
|
||||||
|
uint32_t ref;
|
||||||
|
uint32_t flags;
|
||||||
|
} string;
|
||||||
|
|
||||||
|
typedef uint8_t u8;
|
||||||
|
typedef int8_t i8;
|
||||||
|
|
||||||
|
typedef uint16_t u16;
|
||||||
|
typedef int16_t i16;
|
||||||
|
|
||||||
|
typedef uint32_t u32;
|
||||||
|
typedef int32_t i32;
|
||||||
|
|
||||||
|
typedef uint64_t u64;
|
||||||
|
typedef int64_t i64;
|
||||||
|
|
||||||
#define FLAG_STRING_LITERAL 1
|
#define FLAG_STRING_LITERAL 1
|
||||||
#define STRING_DEBUG 1
|
#define STRING_DEBUG 1
|
||||||
@@ -127,87 +129,71 @@ public class Generator
|
|||||||
#define STR_DBG(fmt, ...)
|
#define STR_DBG(fmt, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct nub_core_string
|
|
||||||
{
|
|
||||||
char *data;
|
|
||||||
size_t length;
|
|
||||||
uint32_t ref;
|
|
||||||
uint32_t flags;
|
|
||||||
};
|
|
||||||
|
|
||||||
#if STRING_DEBUG
|
#if STRING_DEBUG
|
||||||
static size_t nub_core_string_allocs = 0;
|
static size_t string_allocs = 0;
|
||||||
static size_t nub_core_string_frees = 0;
|
static size_t string_frees = 0;
|
||||||
|
|
||||||
__attribute__((destructor))
|
__attribute__((destructor))
|
||||||
static void string_debug_report(void)
|
static void string_debug_report(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[STR] REPORT allocs=%zu frees=%zu leaks=%zu\n", nub_core_string_allocs, nub_core_string_frees, nub_core_string_allocs - nub_core_string_frees);
|
fprintf(stderr, "[STR] REPORT allocs=%zu frees=%zu leaks=%zu\n", string_allocs, string_frees, string_allocs - string_frees);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static inline void nub_core_string_rc_inc(struct nub_core_string *string)
|
static inline void string_rc_inc(string *str)
|
||||||
{
|
|
||||||
if (string == NULL)
|
|
||||||
{
|
{
|
||||||
|
if (str == NULL) {
|
||||||
STR_DBG("INC null string");
|
STR_DBG("INC null string");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string->flags & FLAG_STRING_LITERAL)
|
if (str->flags & FLAG_STRING_LITERAL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
string->ref += 1;
|
str->ref += 1;
|
||||||
|
|
||||||
STR_DBG("INC: str=%p ref=%u \"%s\"", (void*)string, string->ref, string->data);
|
STR_DBG("INC: str=%p ref=%u \"%s\"", (void *)str, str->ref, str->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void nub_core_string_rc_dec(struct nub_core_string *string)
|
static inline void string_rc_dec(string *str)
|
||||||
{
|
{
|
||||||
if (string == NULL)
|
if (str == NULL)
|
||||||
{
|
{
|
||||||
STR_DBG("DEC null string");
|
STR_DBG("DEC null string");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string->flags & FLAG_STRING_LITERAL)
|
if (str->flags & FLAG_STRING_LITERAL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (string->ref == 0)
|
if (str->ref == 0)
|
||||||
{
|
{
|
||||||
STR_DBG("ERROR: DEC on zero refcount str=%p", (void*)string);
|
STR_DBG("ERROR: DEC on zero refcount str=%p", (void *)str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string->ref -= 1;
|
str->ref -= 1;
|
||||||
|
|
||||||
STR_DBG("DEC: str=%p ref=%u \"%s\"", (void*)string, string->ref, string->data);
|
STR_DBG("DEC: str=%p ref=%u \"%s\"", (void *)str, str->ref, str->data);
|
||||||
|
|
||||||
if (string->ref == 0)
|
if (str->ref == 0) {
|
||||||
{
|
STR_DBG("FREE: str=%p data=%p \"%s\"", (void *)str, (void *)str->data, str->data);
|
||||||
STR_DBG("FREE: str=%p data=%p \"%s\"", (void*)string, (void*)string->data, string->data);
|
|
||||||
|
|
||||||
char *data_ptr = string->data;
|
|
||||||
struct nub_core_string *str_ptr = string;
|
|
||||||
|
|
||||||
#if STRING_DEBUG
|
#if STRING_DEBUG
|
||||||
nub_core_string_frees++;
|
string_frees++;
|
||||||
|
|
||||||
memset(data_ptr, 0xDD, str_ptr->length);
|
|
||||||
memset(str_ptr, 0xDD, sizeof(*str_ptr));
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
free(data_ptr);
|
free(str->data);
|
||||||
free(str_ptr);
|
free(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline struct nub_core_string *nub_core_string_concat(struct nub_core_string *left, struct nub_core_string *right)
|
static inline string *string_concat(string *left, string *right)
|
||||||
{
|
{
|
||||||
size_t new_length = left->length + right->length;
|
size_t new_length = left->length + right->length;
|
||||||
|
|
||||||
struct nub_core_string *result = malloc(sizeof(struct nub_core_string));
|
string *result = (string *)malloc(sizeof(string));
|
||||||
result->data = malloc(new_length + 1);
|
result->data = (char *)malloc(new_length + 1);
|
||||||
|
|
||||||
memcpy(result->data, left->data, left->length);
|
memcpy(result->data, left->data, left->length);
|
||||||
memcpy(result->data + left->length, right->data, right->length);
|
memcpy(result->data + left->length, right->data, right->length);
|
||||||
@@ -218,19 +204,19 @@ public class Generator
|
|||||||
result->flags = 0;
|
result->flags = 0;
|
||||||
|
|
||||||
#if STRING_DEBUG
|
#if STRING_DEBUG
|
||||||
nub_core_string_allocs++;
|
string_allocs++;
|
||||||
STR_DBG("NEW concat: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data);
|
STR_DBG("NEW concat: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline struct nub_core_string *nub_core_string_from_cstr(char* cstr)
|
static inline string *string_from_cstr(char *cstr)
|
||||||
{
|
{
|
||||||
size_t len = strlen(cstr);
|
size_t len = strlen(cstr);
|
||||||
|
|
||||||
struct nub_core_string *result = malloc(sizeof(struct nub_core_string));
|
string *result = (string *)malloc(sizeof(string));
|
||||||
result->data = malloc(len + 1);
|
result->data = (char *)malloc(len + 1);
|
||||||
|
|
||||||
memcpy(result->data, cstr, len + 1);
|
memcpy(result->data, cstr, len + 1);
|
||||||
|
|
||||||
@@ -239,23 +225,15 @@ public class Generator
|
|||||||
result->flags = 0;
|
result->flags = 0;
|
||||||
|
|
||||||
#if STRING_DEBUG
|
#if STRING_DEBUG
|
||||||
nub_core_string_allocs++;
|
string_allocs++;
|
||||||
STR_DBG("NEW from_cstr: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data);
|
STR_DBG("NEW from_cstr: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define da_append(xs, x) \
|
"""
|
||||||
do { \
|
);
|
||||||
if ((xs)->count >= (xs)->capacity) { \
|
|
||||||
if ((xs)->capacity == 0) (xs)->capacity = 256; \
|
|
||||||
else (xs)->capacity *= 2; \
|
|
||||||
(xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \
|
|
||||||
} \
|
|
||||||
(xs)->items[(xs)->count++] = (x); \
|
|
||||||
} while (0)
|
|
||||||
""");
|
|
||||||
|
|
||||||
while (referencedTypes.Count != 0)
|
while (referencedTypes.Count != 0)
|
||||||
{
|
{
|
||||||
@@ -269,9 +247,9 @@ public class Generator
|
|||||||
writer.WriteLine
|
writer.WriteLine
|
||||||
(
|
(
|
||||||
$$"""
|
$$"""
|
||||||
static struct nub_core_string {{name}} = {
|
static string {{name}} = (string){
|
||||||
.data = "{{value}}",
|
.data = "{{value}}",
|
||||||
.length = {{value.Length}},
|
.length = {{Encoding.UTF8.GetByteCount(value)}},
|
||||||
.ref = 0,
|
.ref = 0,
|
||||||
.flags = FLAG_STRING_LITERAL
|
.flags = FLAG_STRING_LITERAL
|
||||||
};
|
};
|
||||||
@@ -296,19 +274,19 @@ public class Generator
|
|||||||
{
|
{
|
||||||
case NubTypeArray arrayType:
|
case NubTypeArray arrayType:
|
||||||
{
|
{
|
||||||
var name = NameMangler.Mangle("dynamic", "array", arrayType);
|
EmitTypeDefinitionIfNotEmitted(arrayType.ElementType);
|
||||||
|
|
||||||
writer.WriteLine
|
writer.WriteLine("typedef struct");
|
||||||
(
|
writer.WriteLine("{");
|
||||||
$$"""
|
using (writer.Indent())
|
||||||
struct {{name}}
|
|
||||||
{
|
{
|
||||||
size_t count;
|
writer.WriteLine("size_t count;");
|
||||||
size_t capacity;
|
writer.WriteLine("size_t capacity;");
|
||||||
{{CType(arrayType.ElementType)}} *items;
|
writer.WriteLine($"{CType(arrayType.ElementType)} *items;");
|
||||||
};
|
}
|
||||||
"""
|
writer.WriteLine($"}} {typeNames[arrayType]};");
|
||||||
);
|
writer.WriteLine();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NubTypeStruct structType:
|
case NubTypeStruct structType:
|
||||||
@@ -319,12 +297,10 @@ public class Generator
|
|||||||
foreach (var field in structInfo.Fields)
|
foreach (var field in structInfo.Fields)
|
||||||
EmitTypeDefinitionIfNotEmitted(field.Type);
|
EmitTypeDefinitionIfNotEmitted(field.Type);
|
||||||
|
|
||||||
writer.Write("struct ");
|
|
||||||
|
|
||||||
if (structInfo.Packed)
|
if (structInfo.Packed)
|
||||||
writer.Write("__attribute__((__packed__)) ");
|
writer.Write("__attribute__((__packed__)) ");
|
||||||
|
|
||||||
writer.WriteLine(NameMangler.Mangle(structType.Module, structType.Name, structType));
|
writer.WriteLine("typedef struct");
|
||||||
writer.WriteLine("{");
|
writer.WriteLine("{");
|
||||||
using (writer.Indent())
|
using (writer.Indent())
|
||||||
{
|
{
|
||||||
@@ -333,7 +309,7 @@ public class Generator
|
|||||||
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.WriteLine("};");
|
writer.WriteLine($"}} {typeNames[structType]};");
|
||||||
writer.WriteLine();
|
writer.WriteLine();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -343,7 +319,7 @@ public class Generator
|
|||||||
foreach (var field in anonymousStructType.Fields)
|
foreach (var field in anonymousStructType.Fields)
|
||||||
EmitTypeDefinitionIfNotEmitted(field.Type);
|
EmitTypeDefinitionIfNotEmitted(field.Type);
|
||||||
|
|
||||||
writer.WriteLine($"struct {NameMangler.Mangle("anonymous", "struct", anonymousStructType)}");
|
writer.WriteLine("typedef struct");
|
||||||
writer.WriteLine("{");
|
writer.WriteLine("{");
|
||||||
using (writer.Indent())
|
using (writer.Indent())
|
||||||
{
|
{
|
||||||
@@ -352,7 +328,7 @@ public class Generator
|
|||||||
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.WriteLine("};");
|
writer.WriteLine($"}} {typeNames[anonymousStructType]};");
|
||||||
writer.WriteLine();
|
writer.WriteLine();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -370,7 +346,7 @@ public class Generator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.WriteLine($"struct {NameMangler.Mangle(enumType.Module, enumType.Name, enumType)}");
|
writer.WriteLine("typedef struct");
|
||||||
writer.WriteLine("{");
|
writer.WriteLine("{");
|
||||||
using (writer.Indent())
|
using (writer.Indent())
|
||||||
{
|
{
|
||||||
@@ -389,7 +365,7 @@ public class Generator
|
|||||||
}
|
}
|
||||||
writer.WriteLine("};");
|
writer.WriteLine("};");
|
||||||
}
|
}
|
||||||
writer.WriteLine("};");
|
writer.WriteLine($"}} {typeNames[enumType]};");
|
||||||
writer.WriteLine();
|
writer.WriteLine();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -782,23 +758,31 @@ public class Generator
|
|||||||
{
|
{
|
||||||
NubTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
|
NubTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
|
||||||
NubTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
|
NubTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
|
||||||
NubTypeStruct type => $"struct {NameMangler.Mangle(type.Module, type.Name, type)}" + (varName != null ? $" {varName}" : ""),
|
NubTypeStruct type => CTypeNamed(type, varName),
|
||||||
NubTypeAnonymousStruct type => CTypeAnonymousStruct(type, varName),
|
NubTypeAnonymousStruct type => CTypeNamed(type, varName),
|
||||||
NubTypeEnum type => $"struct {NameMangler.Mangle(type.Module, type.Name, type)}" + (varName != null ? $" {varName}" : ""),
|
NubTypeEnum type => CTypeNamed(type, varName),
|
||||||
NubTypeEnumVariant type => CType(type.EnumType, varName),
|
NubTypeEnumVariant type => CType(type.EnumType, varName),
|
||||||
NubTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
|
NubTypeSInt type => $"i{type.Width}" + (varName != null ? $" {varName}" : ""),
|
||||||
NubTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""),
|
NubTypeUInt type => $"u{type.Width}" + (varName != null ? $" {varName}" : ""),
|
||||||
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
|
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
|
||||||
NubTypeString type => "struct nub_core_string" + (varName != null ? $" *{varName}" : "*"),
|
NubTypeString type => "string" + (varName != null ? $" *{varName}" : "*"),
|
||||||
NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
|
NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
|
||||||
NubTypeArray type => $"struct {NameMangler.Mangle("dynamic", "array", type)}" + (varName != null ? $" {varName}" : ""),
|
NubTypeArray type => CTypeNamed(type, varName),
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string CTypeAnonymousStruct(NubTypeAnonymousStruct type, string? varName)
|
private readonly Dictionary<NubType, string> typeNames = [];
|
||||||
|
|
||||||
|
private string CTypeNamed(NubType type, string? varName)
|
||||||
{
|
{
|
||||||
return $"struct {NameMangler.Mangle("anonymous", "struct", type)}{(varName != null ? $" {varName}" : "")}";
|
if (!typeNames.TryGetValue(type, out var name))
|
||||||
|
{
|
||||||
|
name = Tmp();
|
||||||
|
typeNames[type] = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $"{name}{(varName != null ? $" {varName}" : "")}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private string Tmp()
|
private string Tmp()
|
||||||
@@ -833,7 +817,7 @@ public class Generator
|
|||||||
{
|
{
|
||||||
case NubTypeString:
|
case NubTypeString:
|
||||||
{
|
{
|
||||||
writer.WriteLine($"nub_core_string_rc_inc({value});");
|
writer.WriteLine($"string_rc_inc({value});");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NubTypeStruct structType:
|
case NubTypeStruct structType:
|
||||||
@@ -904,7 +888,7 @@ public class Generator
|
|||||||
{
|
{
|
||||||
case NubTypeString:
|
case NubTypeString:
|
||||||
{
|
{
|
||||||
writer.WriteLine($"nub_core_string_rc_dec({value});");
|
writer.WriteLine($"string_rc_dec({value});");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NubTypeStruct structType:
|
case NubTypeStruct structType:
|
||||||
|
|||||||
@@ -144,18 +144,6 @@ if (!compileLib)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entryPointType.Parameters.Count != 1)
|
|
||||||
{
|
|
||||||
DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must take exaxtly one parameter").Build(), Console.Error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entryPointType.Parameters[0] is not NubTypeArray { ElementType: NubTypeString })
|
|
||||||
{
|
|
||||||
DiagnosticFormatter.Print(Diagnostic.Error($"First parameter of entrypoint must be a string array").Build(), Console.Error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
entryPoint = info.MangledName;
|
entryPoint = info.MangledName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,15 +10,19 @@ enum Message {
|
|||||||
Say: string
|
Say: string
|
||||||
}
|
}
|
||||||
|
|
||||||
func main(args: []string): i32 {
|
func main(): i32 {
|
||||||
let i = 0
|
let message = getMessage()
|
||||||
while i < args.count {
|
|
||||||
// core::print(args[i])
|
match message {
|
||||||
i = i + 1
|
Quit {}
|
||||||
|
Say msg {
|
||||||
|
core::println(msg)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMessage(): Message {
|
func getMessage(): Message {
|
||||||
return new Message::Say("test")
|
return new Message::Say("testæøå")
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user