WIP: dev #1

Draft
nub31 wants to merge 103 commits from dev into master
2 changed files with 269 additions and 303 deletions
Showing only changes of commit 918d25f8c8 - Show all commits

View File

@@ -17,6 +17,106 @@ public class Generator
this.entryPoint = entryPoint; this.entryPoint = entryPoint;
} }
const string NUB_H_CONTENTS =
"""
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char *data;
size_t length;
size_t ref;
uint32_t flags;
} string;
typedef string *string_ptr;
#define FLAG_STRING_LITERAL 1
static inline void string_rc_inc(string_ptr str)
{
if (str == NULL)
return;
if (str->flags & FLAG_STRING_LITERAL)
return;
str->ref += 1;
}
static inline void string_rc_dec(string_ptr str)
{
if (str == NULL)
return;
if (str->flags & FLAG_STRING_LITERAL)
return;
if (str->ref == 0)
return;
str->ref -= 1;
if (str->ref == 0)
{
free(str->data);
free(str);
}
}
static inline string_ptr string_concat(string_ptr left, string_ptr right)
{
size_t new_length = left->length + right->length;
string_ptr result = (string_ptr)malloc(sizeof(string));
result->data = (char*)malloc(new_length + 1);
memcpy(result->data, left->data, left->length);
memcpy(result->data + left->length, right->data, right->length);
result->data[new_length] = '\0';
result->length = new_length;
result->ref = 1;
result->flags = 0;
return result;
}
static inline string_ptr string_from_cstr(char *cstr)
{
size_t len = strlen(cstr);
string_ptr result = (string_ptr)malloc(sizeof(string));
result->data = (char*)malloc(len + 1);
memcpy(result->data, cstr, len + 1);
result->length = len;
result->ref = 1;
result->flags = 0;
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)
""";
private readonly List<TypedNodeDefinitionFunc> functions; private readonly List<TypedNodeDefinitionFunc> functions;
private readonly ModuleGraph moduleGraph; private readonly ModuleGraph moduleGraph;
private readonly string? entryPoint; private readonly string? entryPoint;
@@ -29,16 +129,18 @@ public class Generator
private string Emit() private string Emit()
{ {
var outPath = ".build";
var fileName = "out.c";
if (entryPoint != null) if (entryPoint != null)
{ {
writer.WriteLine($$""" writer.WriteLine("int main(int argc, char *argv[])");
int main(int argc, char *argv[]) writer.WriteLine("{");
{ using (writer.Indent())
return {{entryPoint}}(); {
} writer.WriteLine($"return {entryPoint}();");
}
"""); writer.WriteLine("}");
writer.WriteLine(); writer.WriteLine();
} }
@@ -47,9 +149,7 @@ public class Generator
if (!moduleGraph.TryResolveIdentifier(function.Module, function.Name.Ident, true, out var info)) if (!moduleGraph.TryResolveIdentifier(function.Module, function.Name.Ident, true, out var info))
throw new UnreachableException($"Module graph does not have info about the function {function.Module}::{function.Name.Ident}. This should have been caught earlier"); throw new UnreachableException($"Module graph does not have info about the function {function.Module}::{function.Name.Ident}. This should have been caught earlier");
if (info.Source == Module.DefinitionSource.Imported || info.Extern) if (info.Source == Module.DefinitionSource.Internal && !info.Extern && !info.Exported)
writer.Write("extern ");
else if (info.Source == Module.DefinitionSource.Internal && !info.Extern && !info.Exported)
writer.Write("static "); writer.Write("static ");
var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident)); var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident));
@@ -66,7 +166,7 @@ public class Generator
writer.WriteLine(); writer.WriteLine();
} }
var implementations = writer.ToString(); var definitions = writer.ToString();
writer = new IndentedTextWriter(); writer = new IndentedTextWriter();
@@ -85,163 +185,12 @@ public class Generator
writer.WriteLine($"{CType(fn.ReturnType, info.MangledName)}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});"); writer.WriteLine($"{CType(fn.ReturnType, info.MangledName)}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});");
else else
writer.WriteLine($"{CType(info.Type, info.MangledName)};"); writer.WriteLine($"{CType(info.Type, info.MangledName)};");
writer.WriteLine();
} }
} }
} }
var declarations = writer.ToString();
writer = new IndentedTextWriter();
writer.WriteLine("""
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 RC_DBG(fmt, ...) fprintf(stderr, "[RC] " fmt "\n", ##__VA_ARGS__)
static size_t rc_allocs = 0;
static size_t rc_frees = 0;
__attribute__((destructor))
static void string_debug_report(void)
{
fprintf(stderr, "[RC] REPORT allocs=%zu frees=%zu leaks=%zu\n", rc_allocs, rc_frees, rc_allocs - rc_frees);
}
static inline void string_rc_inc(string *str)
{
if (str == NULL)
{
RC_DBG("INC null string");
return;
}
if (str->flags & FLAG_STRING_LITERAL)
return;
str->ref += 1;
RC_DBG("INC: str=%p ref=%u \"%s\"", (void *)str, str->ref, str->data);
}
static inline void string_rc_dec(string *str)
{
if (str == NULL)
{
RC_DBG("WRN: DEC null string");
return;
}
if (str->flags & FLAG_STRING_LITERAL)
return;
if (str->ref == 0)
{
RC_DBG("ERR: DEC on zero refcount str=%p", (void *)str);
return;
}
str->ref -= 1;
RC_DBG("DEC: str=%p ref=%u \"%s\"", (void *)str, str->ref, str->data);
if (str->ref == 0)
{
rc_frees++;
RC_DBG("FREE: str=%p data=%p \"%s\"", (void *)str, (void *)str->data, str->data);
free(str->data);
free(str);
}
}
static inline string *string_concat(string *left, string *right)
{
size_t new_length = left->length + right->length;
string *result = (string *)malloc(sizeof(string));
result->data = (char *)malloc(new_length + 1);
memcpy(result->data, left->data, left->length);
memcpy(result->data + left->length, right->data, right->length);
result->data[new_length] = '\0';
result->length = new_length;
result->ref = 1;
result->flags = 0;
rc_allocs++;
RC_DBG("NEW string_concat: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data);
return result;
}
static inline string *string_from_cstr(char *cstr)
{
size_t len = strlen(cstr);
string *result = (string *)malloc(sizeof(string));
result->data = (char *)malloc(len + 1);
memcpy(result->data, cstr, len + 1);
result->length = len;
result->ref = 1;
result->flags = 0;
rc_allocs++;
RC_DBG("NEW string_from_cstr: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data);
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)
{
var type = referencedTypes.ElementAt(0);
EmitTypeDefinitionIfNotEmitted(type);
referencedTypes.Remove(type);
}
foreach (var (name, value) in referencedStringLiterals) foreach (var (name, value) in referencedStringLiterals)
{ {
writer.WriteLine writer.WriteLine
@@ -258,9 +207,35 @@ static inline string *string_from_cstr(char *cstr)
); );
} }
var header = writer.ToString(); var declarations = writer.ToString();
return $"{header}\n{declarations}\n{implementations}"; writer = new IndentedTextWriter();
while (referencedTypes.Count != 0)
{
var type = referencedTypes.ElementAt(0);
EmitTypeDefinitionIfNotEmitted(type);
referencedTypes.Remove(type);
}
var types = writer.ToString();
var sb = new StringBuilder();
sb.AppendLine("#include \"nub.h\"");
sb.AppendLine();
sb.AppendLine(types);
sb.AppendLine(declarations);
sb.AppendLine(definitions);
Directory.CreateDirectory(outPath);
var filePath = Path.Combine(outPath, fileName);
File.WriteAllText(Path.Combine(outPath, "nub.h"), NUB_H_CONTENTS);
File.WriteAllText(filePath, sb.ToString());
return filePath;
} }
private void EmitTypeDefinitionIfNotEmitted(NubType type) private void EmitTypeDefinitionIfNotEmitted(NubType type)
@@ -270,109 +245,10 @@ static inline string *string_from_cstr(char *cstr)
emittedTypes.Add(type); emittedTypes.Add(type);
var name = TypeName(type);
switch (type) switch (type)
{ {
case NubTypeArray arrayType:
{
EmitTypeDefinitionIfNotEmitted(arrayType.ElementType);
var name = typeNames[arrayType];
writer.WriteLine("typedef struct");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("size_t count;");
writer.WriteLine("size_t capacity;");
writer.WriteLine($"{CType(arrayType.ElementType)} *items;");
writer.WriteLine("uint32_t ref;");
}
writer.WriteLine($"}} {name};");
writer.WriteLine();
writer.WriteLine($$"""
static inline void {{name}}_rc_inc({{name}} *array)
{
if (array == NULL)
{
RC_DBG("WRN: INC null array");
return;
}
array->ref += 1;
RC_DBG("INC: array=%p ref=%u", (void *)array, array->ref);
}
""");
writer.WriteLine($"static inline void {name}_rc_dec({name} *array)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("if (array == NULL)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("RC_DBG(\"WRN: DEC null array\");");
writer.WriteLine("return;");
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine("if (array->ref == 0)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("RC_DBG(\"ERR: DEC on zero refcount\");");
writer.WriteLine("return;");
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine("array->ref -= 1;");
writer.WriteLine();
writer.WriteLine("RC_DBG(\"DEC: array=%p ref=%u\", (void*)array, array->ref);");
writer.WriteLine();
writer.WriteLine("if (array->ref == 0)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("for (size_t i = 0; i < array->count; ++i)");
writer.WriteLine("{");
using (writer.Indent())
{
EmitCopyDestructor("array->items[i]", arrayType.ElementType);
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine("rc_frees++;");
writer.WriteLine("RC_DBG(\"FREE: array=%p data=%p\", (void *)array, (void *)array->items);");
writer.WriteLine();
writer.WriteLine("free(array);");
}
writer.WriteLine("}");
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine($"static inline {name} *{name}_make()");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine($"{name} *array = ({name} *)malloc(sizeof({name}));");
writer.WriteLine("array->ref = 1;");
writer.WriteLine("array->count = 0;");
writer.WriteLine("array->capacity = 0;");
writer.WriteLine("array->items = NULL;");
writer.WriteLine();
writer.WriteLine("rc_allocs++;");
writer.WriteLine($"RC_DBG(\"NEW {name}_make: array=%p ref=%u\", (void *)array, array->ref);");
writer.WriteLine("return array;");
}
writer.WriteLine("}");
writer.WriteLine();
break;
}
case NubTypeStruct structType: case NubTypeStruct structType:
{ {
if (!moduleGraph.TryResolveType(structType.Module, structType.Name, true, out var info) || info is not Module.TypeInfoStruct structInfo) if (!moduleGraph.TryResolveType(structType.Module, structType.Name, true, out var info) || info is not Module.TypeInfoStruct structInfo)
@@ -393,7 +269,7 @@ static inline void {{name}}_rc_inc({{name}} *array)
writer.WriteLine($"{CType(field.Type, field.Name)};"); writer.WriteLine($"{CType(field.Type, field.Name)};");
} }
} }
writer.WriteLine($"}} {typeNames[structType]};"); writer.WriteLine($"}} {name};");
writer.WriteLine(); writer.WriteLine();
break; break;
@@ -412,7 +288,7 @@ static inline void {{name}}_rc_inc({{name}} *array)
writer.WriteLine($"{CType(field.Type, field.Name)};"); writer.WriteLine($"{CType(field.Type, field.Name)};");
} }
} }
writer.WriteLine($"}} {typeNames[anonymousStructType]};"); writer.WriteLine($"}} {name};");
writer.WriteLine(); writer.WriteLine();
break; break;
@@ -449,7 +325,7 @@ static inline void {{name}}_rc_inc({{name}} *array)
} }
writer.WriteLine("};"); writer.WriteLine("};");
} }
writer.WriteLine($"}} {typeNames[enumType]};"); writer.WriteLine($"}} {name};");
writer.WriteLine(); writer.WriteLine();
break; break;
@@ -457,6 +333,97 @@ static inline void {{name}}_rc_inc({{name}} *array)
case NubTypeEnumVariant variantType: case NubTypeEnumVariant variantType:
{ {
EmitTypeDefinitionIfNotEmitted(variantType.EnumType); EmitTypeDefinitionIfNotEmitted(variantType.EnumType);
break;
}
case NubTypePointer pointerType:
{
EmitTypeDefinitionIfNotEmitted(pointerType.To);
writer.WriteLine($"typedef {TypeName(pointerType.To)} *{name};");
break;
}
case NubTypeFunc funcType:
{
EmitTypeDefinitionIfNotEmitted(funcType.ReturnType);
foreach (var parameterType in funcType.Parameters)
EmitTypeDefinitionIfNotEmitted(parameterType);
writer.WriteLine($"typedef {TypeName(funcType.ReturnType)} (*)({string.Join(", ", funcType.Parameters.Select(TypeName))}) {name};");
break;
}
case NubTypeArray arrayType:
{
EmitTypeDefinitionIfNotEmitted(arrayType.ElementType);
var backingName = Tmp();
writer.WriteLine("typedef struct");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("size_t count;");
writer.WriteLine("size_t capacity;");
writer.WriteLine($"{CType(arrayType.ElementType)} *items;");
writer.WriteLine("uint32_t ref;");
}
writer.WriteLine($"}} {backingName};");
writer.WriteLine();
writer.WriteLine($"typedef {backingName} *{name};");
writer.WriteLine();
writer.WriteLine($"static inline void {name}_rc_inc({name} array)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("if (array == NULL) return;");
writer.WriteLine("array->ref += 1;");
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine($"static inline void {name}_rc_dec({name} array)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("if (array == NULL) return;");
writer.WriteLine("if (array->ref == 0) return;");
writer.WriteLine();
writer.WriteLine("array->ref -= 1;");
writer.WriteLine();
writer.WriteLine("if (array->ref == 0)");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine("for (size_t i = 0; i < array->count; ++i)");
writer.WriteLine("{");
using (writer.Indent())
{
EmitCopyDestructor("array->items[i]", arrayType.ElementType);
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine("free(array);");
}
writer.WriteLine("}");
}
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine($"static inline {name} {name}_make()");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine($"{name} array = ({name})malloc(sizeof({backingName}));");
writer.WriteLine("array->ref = 1;");
writer.WriteLine("array->count = 0;");
writer.WriteLine("array->capacity = 0;");
writer.WriteLine("array->items = NULL;");
writer.WriteLine("return array;");
}
writer.WriteLine("}");
writer.WriteLine();
break; break;
} }
} }
@@ -801,7 +768,7 @@ static inline void {{name}}_rc_inc({{name}} *array)
var name = Tmp(); var name = Tmp();
scopes.Peek().DeconstructableNames.Add((name, expression.Type)); scopes.Peek().DeconstructableNames.Add((name, expression.Type));
writer.WriteLine($"{CType(expression.Type, name)} = {typeNames[expression.Type]}_make();"); writer.WriteLine($"{CType(expression.Type, name)} = {TypeName(expression.Type)}_make();");
foreach (var value in expression.Values) foreach (var value in expression.Values)
{ {
@@ -869,39 +836,39 @@ static inline void {{name}}_rc_inc({{name}} *array)
return tmp; return tmp;
} }
public string CType(NubType node, string? varName = null)
{
referencedTypes.Add(node);
return node switch
{
NubTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
NubTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
NubTypeStruct type => CTypeNamed(type, varName),
NubTypeAnonymousStruct type => CTypeNamed(type, varName),
NubTypeEnum type => CTypeNamed(type, varName),
NubTypeEnumVariant type => CType(type.EnumType, varName),
NubTypeSInt type => $"i{type.Width}" + (varName != null ? $" {varName}" : ""),
NubTypeUInt type => $"u{type.Width}" + (varName != null ? $" {varName}" : ""),
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
NubTypeString type => "string" + (varName != null ? $" *{varName}" : "*"),
NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
NubTypeArray type => $"{CTypeNamed(type)}" + (varName != null ? $" *{varName}" : "*"),
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
};
}
private readonly Dictionary<NubType, string> typeNames = []; private readonly Dictionary<NubType, string> typeNames = [];
private string CTypeNamed(NubType type, string? varName = null) private string TypeName(NubType type)
{ {
if (!typeNames.TryGetValue(type, out var name)) if (!typeNames.TryGetValue(type, out var name))
{ {
name = Tmp(); name = type switch
{
NubTypeVoid => "void",
NubTypeBool => "bool",
NubTypeStruct => Tmp(),
NubTypeAnonymousStruct => Tmp(),
NubTypeEnum => Tmp(),
NubTypeEnumVariant t => TypeName(t.EnumType),
NubTypeSInt t => $"int{t.Width}_t",
NubTypeUInt t => $"uint{t.Width}_t",
NubTypePointer => Tmp(),
NubTypeString => "string_ptr",
NubTypeFunc => Tmp(),
NubTypeArray => Tmp(),
_ => throw new NotImplementedException(),
};
typeNames[type] = name; typeNames[type] = name;
} }
return $"{name}{(varName != null ? $" {varName}" : "")}"; return name;
}
private string CType(NubType node, string? varName = null)
{
referencedTypes.Add(node);
return $"{TypeName(node)}" + (varName != null ? $" {varName}" : "");
} }
private string Tmp() private string Tmp()
@@ -936,7 +903,7 @@ static inline void {{name}}_rc_inc({{name}} *array)
{ {
case NubTypeArray arrayType: case NubTypeArray arrayType:
{ {
writer.WriteLine($"{typeNames[arrayType]}_rc_inc({value});"); writer.WriteLine($"{TypeName(type)}_rc_inc({value});");
break; break;
} }
case NubTypeString: case NubTypeString:
@@ -1012,7 +979,7 @@ static inline void {{name}}_rc_inc({{name}} *array)
{ {
case NubTypeArray arrayType: case NubTypeArray arrayType:
{ {
writer.WriteLine($"{typeNames[arrayType]}_rc_dec({value});"); writer.WriteLine($"{TypeName(type)}_rc_dec({value});");
break; break;
} }
case NubTypeString: case NubTypeString:

View File

@@ -147,18 +147,17 @@ if (!compileLib)
entryPoint = info.MangledName; entryPoint = info.MangledName;
} }
var output = Generator.Emit(functions, moduleGraph, entryPoint); var outFile = Generator.Emit(functions, moduleGraph, entryPoint);
File.WriteAllText(".build/out.c", output);
if (compileLib) if (compileLib)
{ {
Process.Start("gcc", ["-Og", "-g", "-fno-builtin", "-c", "-o", ".build/out.o", ".build/out.c", .. archivePaths]).WaitForExit(); Process.Start("gcc", ["-Og", "-g", "-fno-builtin", "-c", "-o", ".build/out.o", outFile, .. archivePaths]).WaitForExit();
Process.Start("ar", ["rcs", ".build/out.a", ".build/out.o"]).WaitForExit(); Process.Start("ar", ["rcs", ".build/out.a", ".build/out.o"]).WaitForExit();
NubLib.Pack(".build/out.nublib", ".build/out.a", Manifest.Create(moduleGraph)); NubLib.Pack(".build/out.nublib", ".build/out.a", Manifest.Create(moduleGraph));
} }
else else
{ {
Process.Start("gcc", ["-Og", "-g", "-fno-builtin", "-o", ".build/out", ".build/out.c", .. archivePaths]).WaitForExit(); Process.Start("gcc", ["-Og", "-g", "-fno-builtin", "-o", ".build/out", outFile, .. archivePaths]).WaitForExit();
} }
return 0; return 0;