Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analyser/conversion_checker.c2
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public type Checker struct {
public fn void Checker.init(Checker* c, diagnostics.Diags* diags, ast_builder.Builder* builder) {
c.diags = diags;
c.builder = builder;
c.native_kind = ast.getNativeKind();
c.native_kind = builder.getNativeKind();
// leave other fields uninitialized
}

Expand Down
5 changes: 1 addition & 4 deletions analyser/conversion_checker_expr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ fn ExprWidth getUnaryOpWidth(const UnaryOperator* u) {
// TODO adjust for integer promotion?
return getExprWidth(u.getInner());
case AddrOf:
w.width = (u8)(ast.getWordSize() * 8);
w.is_signed = false;
break;
case Deref:
Expr* e = (Expr*)u;
return getTypeWidth(e.getType());
Expand Down Expand Up @@ -228,7 +225,7 @@ fn ExprWidth getTypeWidth(QualType qt) {
return result;
}
// pointer or something
ExprWidth result = { .width = (u8)(ast.getWordSize() * 8), .is_signed = false }
ExprWidth result = { .width = (u8)(qt.getAlignment() * 8), .is_signed = false }
return result;
}

15 changes: 5 additions & 10 deletions analyser/size_analyser.c2
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,20 @@ public fn TypeSize sizeOfType(QualType qt) {

if (qt.isInvalid()) return result;

u32 pointerSize = getWordSize();

qt = qt.getCanonicalType();
Type* t = qt.getType();
switch (t.getKind()) {
case Builtin:
const BuiltinType* bi = (BuiltinType*)t;
// NOTE: alignment is also size
result.size = bi.getAlignment();
result.align = result.size;
result.align = bi.getAlignment();
result.size = result.align;
result.is_signed = bi.isSigned();
break;
case Pointer:
result.size = pointerSize;
result.align = result.size;
case Function:
result.align = t.getAlignment();
result.size = result.align;
break;
case Array:
ArrayType* arrayType = (ArrayType*)t;
Expand All @@ -218,10 +217,6 @@ public fn TypeSize sizeOfType(QualType qt) {
EnumType* et = (EnumType*)t;
EnumTypeDecl* etd = et.getDecl();
return sizeOfType(etd.getImplType());
case Function:
result.size = pointerSize;
result.align = pointerSize;
break;
case Void:
result.size = 0;
result.align = 0;
Expand Down
27 changes: 13 additions & 14 deletions ast/builtin_type.c2
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public type BuiltinKind enum u8 (public const char* const name,
const bool is_signed,
const bool is_unsigned,
const bool is_integer,
const u8 default_size,
const u8 default_width)
const u8 align,
const u8 width)
{
// Note: default_size/width is adjusted for 32-bit in globals
// Note: align/width is adjusted for 32-bit in globals
Char : { "char", true, false, true, true, 1, 8 },
Int8 : { "i8", true, true, false, true, 1, 8 },
Int16 : { "i16", true, true, false, true, 2, 16 },
Expand All @@ -39,34 +39,33 @@ public type BuiltinKind enum u8 (public const char* const name,
UInt64 : { "u64", false, false, true, true, 8, 64 },
Float32 : { "f32", false, true, false, false, 4, 0 },
Float64 : { "f64", false, true, false, false, 8, 0 },
ISize : { "isize", false, true, false, true, 8, 64 },
USize : { "usize", false, false, true, true, 8, 64 },
ISize : { "isize", false, true, false, true, 0, 0 },
USize : { "usize", false, false, true, true, 0, 0 },
Bool : { "bool", true, false, false, false, 1, 1 },
}

type BuiltinTypeBits struct {
u32 : NumTypeBits; // : 8
u32 : NumTypeBits; // : 16
BuiltinKind kind : 4;
BuiltinKind base_kind : 4;
u32 width : 8;
u32 size : 8;
}
static_assert(sizeof(BuiltinTypeBits), 4);

public type BuiltinType struct @(opaque) {
Type base;
}

fn BuiltinType* BuiltinType.create(ast_context.Context* c, BuiltinKind kind, u32 wordsize) {
fn BuiltinType* BuiltinType.create(ast_context.Context* c, BuiltinKind kind) {
BuiltinKind base_kind = kind;
if (kind == ISize) base_kind = (c.getWordSize() == 4) ? Int32 : Int64;
if (kind == USize) base_kind = (c.getWordSize() == 4) ? UInt32 : UInt64;
BuiltinType* b = c.alloc(sizeof(BuiltinType));
b.base.init(Builtin);
BuiltinKind base_kind = kind;
if (kind == ISize) base_kind = (wordsize == 4) ? Int32 : Int64;
if (kind == USize) base_kind = (wordsize == 4) ? UInt32 : UInt64;
b.base.builtinTypeBits.kind = kind;
b.base.builtinTypeBits.base_kind = base_kind;
b.base.builtinTypeBits.width = base_kind.default_width;
b.base.builtinTypeBits.size = base_kind.default_size;
b.base.builtinTypeBits.width = base_kind.width;
b.base.setAlignment(base_kind.align);
b.base.setCanonicalType(QualType.create(&b.base));
#if AstStatistics
Stats.addType(Builtin, sizeof(BuiltinType));
Expand Down Expand Up @@ -120,7 +119,7 @@ public fn bool BuiltinType.isUnsigned(const BuiltinType* b) {
}

public fn u32 BuiltinType.getAlignment(const BuiltinType* b) {
return b.base.builtinTypeBits.size;
return b.base.typeBits.align;
}

public fn u32 BuiltinType.getWidth(const BuiltinType* b) {
Expand Down
1 change: 1 addition & 0 deletions ast/function_type.c2
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public type FunctionType struct @(opaque) {
fn FunctionType* FunctionType.create(ast_context.Context* c, FunctionDecl* decl) {
FunctionType* t = c.alloc(sizeof(FunctionType));
t.base.init(Function);
t.base.setAlignment(c.getWordSize());
t.decl = decl;

t.base.setCanonicalType(QualType.create(&t.base));
Expand Down
3 changes: 2 additions & 1 deletion ast/pointer_pool.c2
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ type PointerPool struct {
}

fn void PointerPool.init(PointerPool* p, ast_context.Context* c) {
p.context = c;
p.count = 1; // skip first slot
p.capacity = 0;
p.slots = nil;
p.context = c;
p.resize(64);
memset(p.slots, 0, sizeof(p.slots[0]));
}

fn void PointerPool.clear(PointerPool* p) {
Expand Down
1 change: 1 addition & 0 deletions ast/pointer_type.c2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public type PointerType struct @(opaque) {
fn PointerType* PointerType.create(ast_context.Context* c, QualType inner) {
PointerType* t = c.alloc(sizeof(PointerType));
t.base.init(Pointer);
t.base.setAlignment(c.getWordSize());
t.inner = inner;
#if AstStatistics
Stats.addType(Pointer, sizeof(PointerType));
Expand Down
26 changes: 14 additions & 12 deletions ast/type.c2
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public type TypeKind enum u8 (const char* const name) {

type TypeBits struct {
TypeKind kind : 8;
u32 align : 8;
}
const u32 NumTypeBits = 8;
const u32 NumTypeBits = 16;

public type Type struct @(opaque) {
union {
Expand All @@ -48,6 +49,7 @@ public type Type struct @(opaque) {
fn void Type.init(Type* t, TypeKind k) {
t.bits = 0;
t.typeBits.kind = k;
t.typeBits.align = 0;
t.ptr_pool_idx = 0;
t.canonicalType.ptr = 0;
}
Expand Down Expand Up @@ -101,13 +103,16 @@ public fn void Type.dump(const Type* t) @(unused) {
ast.flushDumpBuf(out);
}

fn u32 Type.getAlignment(const Type* t) {
fn void Type.setAlignment(Type* t, u32 align) {
t.typeBits.align = align;
}

public fn u32 Type.getAlignment(const Type* t) {
switch (t.getKind()) {
case Builtin:
const BuiltinType* bi = (BuiltinType*)t;
return bi.getAlignment();
case Pointer:
break;
case Function:
return t.typeBits.align;
case Array:
const ArrayType* at = (ArrayType*)t;
QualType elem = at.getElemType();
Expand All @@ -121,8 +126,6 @@ fn u32 Type.getAlignment(const Type* t) {
const EnumTypeDecl* etd = e.getDecl();
QualType it = etd.getImplType();
return it.getTypeOrNil().getAlignment();
case Function:
break;
case Void:
return 0;
case Alias:
Expand All @@ -132,7 +135,7 @@ fn u32 Type.getAlignment(const Type* t) {
// should not happen
return 0;
}
return getWordSize();
return 0;
}

fn u32 Type.getElemSize(const Type* t) {
Expand All @@ -159,7 +162,8 @@ fn u32 Type.getSize(const Type* t) {
const BuiltinType* bi = (BuiltinType*)t;
return bi.getAlignment(); // note: alignment is size
case Pointer:
break;
case Function:
return t.getAlignment();
case Array:
const ArrayType* at = (ArrayType*)t;
QualType elem = at.getElemType();
Expand All @@ -173,8 +177,6 @@ fn u32 Type.getSize(const Type* t) {
const EnumTypeDecl* etd = e.getDecl();
QualType it = etd.getImplType();
return it.getSize();
case Function:
break;
case Void:
return 0;
case Alias:
Expand All @@ -184,7 +186,7 @@ fn u32 Type.getSize(const Type* t) {
// should not happen
return 0;
}
return getWordSize();
return 0;
}

fn void Type.print(const Type* t, string_buffer.Buf* out) {
Expand Down
14 changes: 2 additions & 12 deletions ast/utils.c2
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public type Globals struct @(opaque) {
string_pool.Pool* names_pool;
PointerPool pointers;
StringTypePool string_types;
u32 wordsize; // 4 or 8 (in bytes)
bool use_color;
// TODO just use AST vector (also in modules, but modules has ownership)
u32 ast_count; // Note: first index used to indicate nil
Expand All @@ -127,11 +126,10 @@ public fn Globals* getGlobals() { return globals; }
public fn void setGlobals(Globals* g) @(unused) { globals = g; }

// wordsize in bytes, must NOT be called from Plugin!
public fn void initialize(Context* c, string_pool.Pool* astPool, u32 wordsize, bool use_color) {
public fn void initialize(Context* c, string_pool.Pool* astPool, bool use_color) {
Globals* g = stdlib.calloc(sizeof(Globals), 1);
g.pointers.init(c);
g.string_types.init(c);
g.wordsize = wordsize;
g.use_color = use_color;
g.names_pool = astPool;
g.ast_count = 1;
Expand All @@ -145,7 +143,7 @@ public fn void initialize(Context* c, string_pool.Pool* astPool, u32 wordsize, b

// create all Qualtypes for builtin-types
for (BuiltinKind kind = BuiltinKind.min; kind <= BuiltinKind.max; kind++) {
g.builtins[kind].set((Type*)BuiltinType.create(c, kind, wordsize));
g.builtins[kind].set((Type*)BuiltinType.create(c, kind));
}

// create void type and void-ptr type
Expand Down Expand Up @@ -180,10 +178,6 @@ public fn void deinit() {
globals = nil;
}

public fn u32 getWordSize() {
return globals.wordsize;
}

public fn bool useColor() @(unused) {
return globals.use_color;
}
Expand Down Expand Up @@ -257,10 +251,6 @@ fn void flushDumpBuf(string_buffer.Buf* out) {
out.clear();
}

public fn BuiltinKind getNativeKind() {
return globals.wordsize == 8 ? UInt64 : UInt32;
}

Color col_Stmt = Color.Bmagenta;
Color col_Decl = Color.Bgreen;
Color col_Expr = Color.Bmagenta;
Expand Down
Loading
Loading