From 7152a710ccadfb31807737d82180b054c2014653 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 16 Apr 2023 15:02:25 +0200 Subject: [PATCH] Add c types, int is default type --- build/examples/game2d.core | 4 +-- core_codegen_c_language.cpp | 12 +++++++- core_compiler.cpp | 34 +++++++++++++++++++-- core_compiler.h | 59 ++++++++++++++++++++++++------------- core_compiler_interface.hpp | 15 ++++++++-- core_printer.cpp | 16 ++++++++-- core_typechecking.cpp | 8 ++--- core_types.cpp | 14 +++++++-- 8 files changed, 125 insertions(+), 37 deletions(-) diff --git a/build/examples/game2d.core b/build/examples/game2d.core index 458b51a..73de16c 100644 --- a/build/examples/game2d.core +++ b/build/examples/game2d.core @@ -46,8 +46,8 @@ Add(&guys, {100, 100}) */ Array :: struct($T: Type) data: *T - len: S64 - cap: S64 + len: int + cap: int Guy :: struct pos: Vector2 diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 9d27c2c..695fa34 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -48,10 +48,20 @@ get_ctype_name_for_type(Ast_Type *type) { case TYPE_VOID: return "void"; case TYPE_BOOL: return "bool"; case TYPE_STRING: return (char *)prefixed_string_type.str; - case TYPE_CHAR: return "char"; case TYPE_F32: return "float"; case TYPE_F64: return "double"; + + case TYPE_CHAR: return "char"; + case TYPE_UCHAR: return "unsigned char"; case TYPE_INT: return "int"; + case TYPE_UINT: return "unsigned int"; + case TYPE_LONG: return "long"; + case TYPE_ULONG: return "unsigned long"; + case TYPE_LLONG: return "long long"; + case TYPE_ULLONG: return "unsigned long long"; + case TYPE_SHORT: return "short"; + case TYPE_USHORT: return "unsigned short"; + case TYPE_S8: return "int8_t"; case TYPE_S16: return "int16_t"; case TYPE_S32: return "int32_t"; diff --git a/core_compiler.cpp b/core_compiler.cpp index 7dfc647..ed2158c 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -125,17 +125,34 @@ for i in meta.token_simple_expr: pctx->type__untyped_float = {TYPE_UNTYPED_FLOAT, sizeof(double), __alignof(double)}; pctx->type__char = {TYPE_CHAR, sizeof(char), __alignof(char)}; + pctx->type__uchar = {TYPE_UCHAR, sizeof(unsigned char), __alignof(unsigned char), true}; pctx->type__int = {TYPE_INT, sizeof(int), __alignof(int)}; + pctx->type__uint = {TYPE_UINT, sizeof(unsigned), __alignof(unsigned), true}; + pctx->type__long = {TYPE_LONG, sizeof(long), __alignof(long)}; + pctx->type__ulong = {TYPE_ULONG, sizeof(unsigned long), __alignof(unsigned long), true}; + pctx->type__llong = {TYPE_LLONG, sizeof(long long), __alignof(long long)}; + pctx->type__ullong = {TYPE_ULLONG, sizeof(unsigned long long), __alignof(unsigned long long), true}; + pctx->type__short = {TYPE_SHORT, sizeof(short), __alignof(short)}; + pctx->type__ushort = {TYPE_USHORT, sizeof(unsigned short), __alignof(unsigned short), true}; pctx->type_char = &pctx->type__char; + pctx->type_uchar = &pctx->type__uchar; pctx->type_int = &pctx->type__int; + pctx->type_uint = &pctx->type__uint; + pctx->type_long = &pctx->type__long; + pctx->type_ulong = &pctx->type__ulong; + pctx->type_llong = &pctx->type__llong; + pctx->type_ullong = &pctx->type__ullong; + pctx->type_short = &pctx->type__short; + pctx->type_ushort = &pctx->type__ushort; + pctx->type_void = &pctx->type__void; // pctx->type__string = {TYPE_STRING, sizeof(String), __alignof(String)}; // pctx->type_any; // Needs to be inited at runtime + // pctx->type_string = &pctx->type__string; pctx->type_type = &pctx->type__type; - // pctx->type_string = &pctx->type__string; pctx->type_bool = &pctx->type__bool; pctx->type_f32 = &pctx->type__f32; @@ -373,12 +390,23 @@ GetTypeInfo :: (type: Type): *Type_Info )"_s; { + + insert_builtin_type_into_scope(module, "char"_s, pctx->type_char); + insert_builtin_type_into_scope(module, "uchar"_s, pctx->type_uchar); + insert_builtin_type_into_scope(module, "int"_s, pctx->type_int); + insert_builtin_type_into_scope(module, "uint"_s, pctx->type_uint); + insert_builtin_type_into_scope(module, "long"_s, pctx->type_long); + insert_builtin_type_into_scope(module, "ulong"_s, pctx->type_ulong); + insert_builtin_type_into_scope(module, "llong"_s, pctx->type_llong); + insert_builtin_type_into_scope(module, "ullong"_s, pctx->type_ullong); + insert_builtin_type_into_scope(module, "short"_s, pctx->type_short); + insert_builtin_type_into_scope(module, "ushort"_s, pctx->type_ushort); + insert_builtin_type_into_scope(module, "S64"_s, pctx->type_s64); insert_builtin_type_into_scope(module, "S32"_s, pctx->type_s32); insert_builtin_type_into_scope(module, "S16"_s, pctx->type_s16); insert_builtin_type_into_scope(module, "S8"_s, pctx->type_s8); - insert_builtin_type_into_scope(module, "int"_s, pctx->type_int); - insert_builtin_type_into_scope(module, "char"_s, pctx->type_char); + insert_builtin_type_into_scope(module, "U64"_s, pctx->type_u64); insert_builtin_type_into_scope(module, "U32"_s, pctx->type_u32); insert_builtin_type_into_scope(module, "U16"_s, pctx->type_u16); diff --git a/core_compiler.h b/core_compiler.h index cc51196..b9b8c8a 100644 --- a/core_compiler.h +++ b/core_compiler.h @@ -154,40 +154,57 @@ print(f"Ast_Operator_Info op_info_table[{size}];") Ast_Type type__untyped_float; Ast_Type type__char; + Ast_Type type__uchar; Ast_Type type__int; + Ast_Type type__uint; + Ast_Type type__long; + Ast_Type type__ulong; + Ast_Type type__llong; + Ast_Type type__ullong; + Ast_Type type__short; + Ast_Type type__ushort; - Ast_Type *type_char = &type__char; - Ast_Type *type_int = &type__int; - Ast_Type *type_void = &type__void; + Ast_Type *type_char; + Ast_Type *type_uchar; + Ast_Type *type_int; + Ast_Type *type_uint; + Ast_Type *type_long; + Ast_Type *type_ulong; + Ast_Type *type_llong; + Ast_Type *type_ullong; + Ast_Type *type_short; + Ast_Type *type_ushort; + + Ast_Type *type_void; Ast_Type *type_pointer_to_char; Ast_Type *type_pointer_to_void; Ast_Type *type_any; // Needs to be inited at runtime - Ast_Type *type_type = &type__type; - Ast_Type *type_string = &type__string; - Ast_Type *type_bool = &type__bool; + Ast_Type *type_type; + Ast_Type *type_string; + Ast_Type *type_bool; - Ast_Type *type_f32 = &type__f32; - Ast_Type *type_f64 = &type__f64; + Ast_Type *type_f32; + Ast_Type *type_f64; - Ast_Type *type_s8 = &type__s8; - Ast_Type *type_s16 = &type__s16; - Ast_Type *type_s32 = &type__s32; - Ast_Type *type_s64 = &type__s64; + Ast_Type *type_s8; + Ast_Type *type_s16; + Ast_Type *type_s32; + Ast_Type *type_s64; - Ast_Type *type_u8 = &type__u8; - Ast_Type *type_u16 = &type__u16; - Ast_Type *type_u32 = &type__u32; - Ast_Type *type_u64 = &type__u64; + Ast_Type *type_u8; + Ast_Type *type_u16; + Ast_Type *type_u32; + Ast_Type *type_u64; - Ast_Type *untyped_string = &type__untyped_string; - Ast_Type *untyped_bool = &type__untyped_bool; - Ast_Type *untyped_int = &type__untyped_int; - Ast_Type *untyped_float = &type__untyped_float; + Ast_Type *untyped_string; + Ast_Type *untyped_bool; + Ast_Type *untyped_int; + Ast_Type *untyped_float; Ast_Type type__vargs; - Ast_Type *type_vargs = &type__vargs; + Ast_Type *type_vargs; Intern_String intern(String string) { assert(string.len > 0); diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 3dcbe9f..6e34b4f 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -184,16 +184,27 @@ struct Token { enum Ast_Type_Kind { TYPE_NONE, - TYPE_S64, // FIRST_NUMERIC + TYPE_S64, // FIRST_NUMERIC is_int start TYPE_S32, TYPE_S16, TYPE_S8, + TYPE_INT, + TYPE_UINT, + TYPE_LONG, + TYPE_ULONG, + TYPE_LLONG, + TYPE_ULLONG, + TYPE_SHORT, + TYPE_USHORT, TYPE_CHAR, + TYPE_UCHAR, + TYPE_U64, TYPE_U32, TYPE_U16, - TYPE_U8, + TYPE_U8, //is_int end + TYPE_F32, TYPE_F64, TYPE_POINTER, diff --git a/core_printer.cpp b/core_printer.cpp index f5fe979..f736a7d 100644 --- a/core_printer.cpp +++ b/core_printer.cpp @@ -6,14 +6,26 @@ CORE_Static String core_type_to_string(Ast_Type *type) { + + // @todo: use get_name_of_type instead of duplicating the typename dispatch table switch (type->kind) { case TYPE_NONE: return ""_s; break; case TYPE_S64: return "S64"_s; break; case TYPE_S32: return "S32"_s; break; case TYPE_S16: return "S16"_s; break; case TYPE_S8: return "S8"_s; break; - case TYPE_INT: return "int"_s; break; - case TYPE_CHAR: return "char"_s; break; + + case TYPE_CHAR: return "char"_s; + case TYPE_UCHAR: return "uchar"_s; + case TYPE_INT: return "int"_s; + case TYPE_UINT: return "uint"_s; + case TYPE_LONG: return "long"_s; + case TYPE_ULONG: return "ulong"_s; + case TYPE_LLONG: return "llong"_s; + case TYPE_ULLONG: return "ullong"_s; + case TYPE_SHORT: return "short"_s; + case TYPE_USHORT: return "ushort"_s; + case TYPE_U64: return "U64"_s; break; case TYPE_U32: return "U32"_s; break; case TYPE_U16: return "U16"_s; break; diff --git a/core_typechecking.cpp b/core_typechecking.cpp index e0ff3fa..b2498ef 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -89,7 +89,7 @@ operand_rvalue(Ast_Type *type) { CORE_Static Ast_Type * get_default_type_from_untyped(Ast_Type *type) { switch (type->kind) { - case TYPE_UNTYPED_INT: return pctx->type_s64; break; + case TYPE_UNTYPED_INT: return pctx->type_int; break; case TYPE_UNTYPED_BOOL: return pctx->type_bool; break; case TYPE_UNTYPED_STRING: return pctx->type_string; break; case TYPE_UNTYPED_FLOAT: return pctx->type_f32; break; @@ -1268,7 +1268,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str // @todo: type_architecture? // we only try to convert the index cause array can't be const // right now - try_propagating_resolved_type_to_untyped_literals(node->index, pctx->type_s64); + try_propagating_resolved_type_to_untyped_literals(node->index, pctx->type_int); try_propagating_resolved_type_to_untyped_literals(node->expr, left.type); if (!left.type) @@ -1566,7 +1566,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope); node->kind = AST_LENGTH_OF; - node->resolved_type = pctx->type_s64; + node->resolved_type = pctx->type_int; if (is_array(name.type)) { Value value = value_int(name.type->arr.size); rewrite_into_const(node, Ast_Builtin, value); @@ -1578,7 +1578,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str return operand_const_rvalue(value); } else if (is_array(name.type) || is_slice(name.type) || is_string(name.type)) { - return operand_rvalue(pctx->type_s64); + return operand_rvalue(pctx->type_int); } else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type)); } diff --git a/core_types.cpp b/core_types.cpp index 4342243..a3a1bd8 100644 --- a/core_types.cpp +++ b/core_types.cpp @@ -4,11 +4,21 @@ get_name_of_type(Ast_Type *type) { switch (type->kind) { case TYPE_VOID: return "void"; case TYPE_BOOL: return "Bool"; - case TYPE_CHAR: return "char"; case TYPE_F32: return "F32"; case TYPE_F64: return "F64"; case TYPE_S8: return "S8"; + + case TYPE_CHAR: return "char"; + case TYPE_UCHAR: return "uchar"; case TYPE_INT: return "int"; + case TYPE_UINT: return "uint"; + case TYPE_LONG: return "long"; + case TYPE_ULONG: return "ulong"; + case TYPE_LLONG: return "llong"; + case TYPE_ULLONG: return "ullong"; + case TYPE_SHORT: return "short"; + case TYPE_USHORT: return "ushort"; + case TYPE_S16: return "S16"; case TYPE_S32: return "S32"; case TYPE_S64: return "S64"; @@ -210,7 +220,7 @@ type_lambda(Ast *ast, Array return_vals, Array args) { CORE_Static Ast_Type * type_enum(Ast_Decl *ast, Ast_Type *type) { if (!type) { - type = pctx->type_s64; + type = pctx->type_int; } Ast_Type *result = type_new(pctx->perm, TYPE_ENUM, type->size, type->align);