core build system and array, it compiles!

This commit is contained in:
Krzosa Karol
2023-04-02 11:16:13 +02:00
parent 2be13cee30
commit 9bb355ed93
12 changed files with 45 additions and 192 deletions

View File

@@ -68,7 +68,9 @@
#if OS_WINDOWS
#define NOMINMAX
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <windows.h>
#define Breakpoint __debugbreak()
#define force_inline __forceinline

View File

@@ -131,7 +131,7 @@ arena_from_buffer(void *buffer, size_t size) {
struct Scoped_Arena {
Arena *arena;
int pos;
size_t pos;
Scoped_Arena(Arena *arena) {
this->arena = arena;
this->pos = arena->len;

View File

@@ -1,146 +1,8 @@
//-----------------------------------------------------------------------------
// Array
//-----------------------------------------------------------------------------
template <class T>
struct Array {
Allocator *allocator;
T *data;
S64 cap;
S64 len;
T *push_empty(S64 count = 1) {
grow(count);
T *result = data + len;
len += count;
return result;
}
T *push_empty_zero(S64 count = 1) {
T *result = push_empty(count);
memory_zero(result, count * sizeof(T));
return result;
}
void grow(S64 required_size) {
if (cap == 0) {
S64 new_cap = max(required_size * 2, (S64)16);
data = allocate_array(allocator, T, new_cap);
cap = new_cap;
}
else if (len + required_size > cap) {
U64 new_cap = max(cap * 2, len + required_size + 1);
T *new_data = allocate_array(allocator, T, new_cap);
memory_copy(new_data, data, cap * sizeof(T));
deallocate(allocator, data);
data = new_data;
cap = new_cap;
}
}
void dealloc() {
if (data) deallocate(allocator, data);
}
S64 get_index(T *item) {
assert((data <= item) && ((data + len) > item));
size_t offset = item - data;
return (S64)offset;
}
void add(Array<T> items) {
For(items) {
add(it);
}
}
#if 0
void insert(T item, int index) {
if (index == len) {
add(item);
return;
}
assert(index < len);
assert(index >= 0);
grow(1);
int right_len = len - index;
memmove(data + index + 1, data + index, sizeof(T) * right_len);
data[index] = item;
len += 1;
}
#endif
void add(T item) {
grow(1);
data[len++] = item;
}
S64 addi(T item) {
S64 result = len;
grow(1);
data[len++] = item;
return result;
}
void unordered_remove(T *item) {
assert(len > 0);
assert((data <= item) && ((data + len) > item));
*item = data[--len];
}
void init(Allocator *a, S64 size = 16) {
allocator = a;
data = allocate_array(a, T, size);
cap = size;
len = 0;
}
Array<T> copy(Allocator *a) {
Array<T> result = {};
result.len = len;
result.cap = len * 2;
result.allocator = a;
result.data = allocate_array(a, T, result.cap);
memory_copy(result.data, data, sizeof(T) * result.len);
return result;
}
Array<T> tight_copy(Allocator *a) {
Array<T> result = {};
result.len = len;
result.cap = len;
result.allocator = 0;
result.data = allocate_array(a, T, len);
memory_copy(result.data, data, sizeof(T) * len);
return result;
}
void ordered_remove(T &item) { // Dont use in loops !!!!
assert(len > 0);
assert(&item >= begin() && &item < end());
int index = get_index(&item);
assert(index >= 0 && index < len);
int right_len = len - index - 1;
memmove(data + index, data + index + 1, right_len * sizeof(T));
len -= 1;
}
force_inline B32 is_last(T *item) { return item == last(); }
force_inline B32 is_first(T *item) { return item == begin(); }
force_inline void clear() { len = 0; }
force_inline void reset() { len = 0; }
force_inline T pop() { return data[--len]; }
force_inline T *last() { return data + len - 1; }
force_inline T *begin() { return data; }
force_inline T *end() { return data + len; }
force_inline T &operator[](S64 i) {
assert(i >= 0 && i < cap);
return data[i];
}
};
#define ARRAY_ALLOCATOR_TYPE Allocator
#define ARRAY_ASSERT assert
#define ARRAY_ALLOCATE(allocator, size) allocate_size(allocator, size)
#define ARRAY_DEALLOCATE(allocator, p) deallocate(allocator, p)
#include "core/array.hpp"
//-----------------------------------------------------------------------------
// Map

View File

@@ -1,20 +1,9 @@
@echo off
if exist "..\misc\compile_setup.bat" call "..\misc\compile_setup.bat"
bld --dont_compile_core
rem if exist "..\misc\compile_setup.bat" call "..\misc\compile_setup.bat"
set clang-flags=-O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib
rem set clang-flags=-O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib
pushd %~dp0
cl core_main.cpp -Zi -nologo -W3 -wd4200 -wd4267 -wd4244 -diagnostics:column -Fe:main.exe user32.lib
rem clang core_main.cpp %clang-flags%
rem ubuntu run clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o core.out
rem clang core_arena.cpp %clang-flags%
rem clang test.cpp
rem main.exe -testing
rem echo Building arms race
rem call examples/arms_race/build_arms_race.bat
rem main examples/arms_race/arms_race.core
popd
rem pushd %~dp0
rem cl core_main.cpp -Zi -nologo -W3 -wd4200 -wd4267 -wd4244 -diagnostics:column -Fe:main.exe user32.lib
rem popd

View File

@@ -1437,7 +1437,7 @@ void bigint_shl_int(BigInt *dest, const BigInt *op1, uint64_t shift) {
uint64_t leftover_shift_count = shift % 64;
dest->digits = ALLOC_DIGITS(op1->digit_count + digit_shift_count + 1);
dest->digit_count = digit_shift_count;
dest->digit_count = (unsigned)digit_shift_count;
uint64_t carry = 0;
for (size_t i = 0; i < op1->digit_count; i += 1) {
uint64_t digit = op1_digits[i];
@@ -1495,7 +1495,7 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) {
return bigint_init_unsigned(dest, 0);
}
dest->digit_count = op1->digit_count - digit_shift_count;
dest->digit_count = (unsigned)(op1->digit_count - digit_shift_count);
uint64_t *digits;
if (dest->digit_count == 1) {
digits = &dest->digit;

View File

@@ -477,7 +477,7 @@ gen_expr(Ast_Expr *ast) {
gen("%Q(", node->resolved_decl->unique_name);
For(node->exprs) {
gen_try_any_or_slice(it->item, it->resolved_type);
if (!node->exprs.is_last(&it)) gen(", ");
if (!node->exprs.is_last(it)) gen(", ");
}
gen(")");
BREAK();
@@ -507,7 +507,7 @@ gen_expr(Ast_Expr *ast) {
gen("[%d] = ", (int)it->resolved_index);
gen_try_any_or_slice(it->item, it->resolved_type);
if (!node->exprs.is_last(&it)) gen(", ");
if (!node->exprs.is_last(it)) gen(", ");
}
if (is_slice(node->resolved_type)) gen("}");
@@ -612,7 +612,7 @@ gen_ast(Ast *ast) {
gen(";");
genln("");
}
if (node->ifs.is_first(&it)) {
if (node->ifs.is_first(it)) {
gen("if(");
gen_expr(it->expr);
gen(")");
@@ -971,7 +971,7 @@ compile_to_c_code() {
For(type->agg.members) {
genln("");
// @todo remove intern from gen
Intern_String name = pctx->internf("m%llu", type->agg.members.get_index(&it));
Intern_String name = pctx->internf("m%llu", type->agg.members.get_index(it));
gen_simple_decl(it.type, name);
gen(";");
}

View File

@@ -196,7 +196,7 @@ token_make(Core_Ctx *lexer) {
CORE_Static Token *
lex_last_indent_token(Lex_Stream *s) {
if (s->indent_stack.len > 0) {
return *s->indent_stack.last();
return *s->indent_stack.back();
}
return &pctx->same_scope_token;
}
@@ -326,7 +326,7 @@ lex__stream(Core_Ctx *lexer) {
semi.kind = OPEN_SCOPE;
semi.indent = last->indent + 2; // @todo: proper detection of indentation
lex_add_token(lexer, &semi);
s->indent_stack.add(lexer->tokens.last());
s->indent_stack.add(lexer->tokens.back());
}
else {
semi.kind = SAME_SCOPE;
@@ -347,7 +347,7 @@ lex__stream(Core_Ctx *lexer) {
if (t.indent > last->indent) {
t.kind = OPEN_SCOPE;
lex_add_token(lexer, &t);
s->indent_stack.add(lexer->tokens.last());
s->indent_stack.add(lexer->tokens.back());
}
else if (t.indent < last->indent) {

View File

@@ -58,7 +58,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
For(*replace) {
assert(it->type == pctx->type_type);
if (it->name == dst->intern_val) {
int it_index = replace->get_index(&it);
S64 it_index = replace->get_index(it);
Ast_Call_Item *replacement = with[0][it_index];
Ast *replacement_v = replacement->item;
dst = (Ast_Atom *)ast_copy(replacement_v, parent_scope, 0, 0);

View File

@@ -35,7 +35,7 @@ core_type_to_string(Ast_Type *type) {
For(args) {
String t = core_type_to_string(it);
b->addf("%Q", t);
if (!args.is_last(&it)) b->addf(", ");
if (!args.is_last(it)) b->addf(", ");
}
b->addf(")");
if (type->func.ret) {
@@ -160,7 +160,7 @@ void core__stringify(Ast *ast) {
gen("(");
For(n->exprs) {
core__stringify(it);
if (!n->exprs.is_last(&it)) gen(",");
if (!n->exprs.is_last(it)) gen(",");
}
gen(")");
} break;
@@ -228,7 +228,7 @@ void core__stringify(Ast *ast) {
Ast_Var_Unpack *n = (Ast_Var_Unpack *)ast;
For(n->vars) {
core__stringify(it);
if (!n->vars.is_last(&it)) gen(",");
if (!n->vars.is_last(it)) gen(",");
}
gen(" = ");
core__stringify(n->expr);
@@ -328,7 +328,7 @@ void core__stringify(Ast *ast) {
gen("return ");
For(n->expr) {
core__stringify(it);
if (!n->expr.is_last(&it)) gen(", ");
if (!n->expr.is_last(it)) gen(", ");
}
} break;
@@ -338,14 +338,14 @@ void core__stringify(Ast *ast) {
gen("(");
For(n->args) {
core__stringify(it);
if (!n->args.is_last(&it)) gen(", ");
if (!n->args.is_last(it)) gen(", ");
}
gen(")");
if (n->ret.len) gen(": ");
For(n->ret) {
core__stringify(it);
if (!n->ret.is_last(&it)) gen(", ");
if (!n->ret.is_last(it)) gen(", ");
}
core__stringify(n->scope);

View File

@@ -884,7 +884,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret) {
node->resolved_type = expr_op.type;
For(node->vars) {
S64 index = node->vars.get_index(&it);
S64 index = node->vars.get_index(it);
Ast_Resolved_Member *type = expr_op.type->agg.members.data + index;
it->type = type->type;
resolve_decl(it);
@@ -1044,7 +1044,7 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type) {
else {
if (default_counter == -1)
compiler_error(it->pos, "Mixing expicit indexes and default is illegal");
it->resolved_index = default_counter;
it->resolved_index = (S32)default_counter;
}
Operand item = resolve_expr(it->item, AST_CANT_BE_NULL, item_type, 0);
@@ -1069,7 +1069,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type) {
For_Named(type->agg.members, m) {
if (it->name->intern_val == m.name) {
it->resolved_name = m.name;
it->resolved_index = type->agg.members.get_index(&m);
it->resolved_index = (S32)type->agg.members.get_index(m);
// @copy_paste
if (m.type == pctx->type_type)
item_type = m.type_val;
@@ -1095,7 +1095,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type) {
Ast_Resolved_Member *m = &type->agg.members[default_counter];
it->resolved_name = m->name;
it->resolved_index = default_counter;
it->resolved_index = (S32)default_counter;
m->visited = true;
// @copy_paste
@@ -1187,7 +1187,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
if (node->expr) {
type_complete(type.type_val);
type.type_val = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val));
type.type_val = type_array(type.type_val, (S32)bigint_as_unsigned(&expr.big_int_val));
}
else {
type.type_val = type_slice(type.type_val, node);
@@ -1651,7 +1651,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Ast_Call_Item *item = 0;
For(node->exprs) {
int it_index = node->exprs.get_index(&it);
S64 it_index = node->exprs.get_index(it);
bool named_argument = is_flag_set(it->call_flags, CALL_NAME);
bool named_argument_already_appeared = it_index > default_iter;

View File

@@ -59,7 +59,7 @@ force_inline B32 is_numeric(Ast_Type *type) {
// Hash consed types
//-----------------------------------------------------------------------------
CORE_Static Ast_Type *
type_new(Allocator *allocator, Ast_Type_Kind kind, size_t size, size_t align) {
type_new(Allocator *allocator, Ast_Type_Kind kind, int32_t size, int32_t align) {
Ast_Type *result = allocate_struct(allocator, Ast_Type, true);
result->kind = kind;
result->size = size;
@@ -147,7 +147,7 @@ type_try_tupling(Array<Ast_Type *> types, Ast *ast) {
}
CORE_Static Ast_Type *
type_array(Ast_Type *base, S64 size) {
type_array(Ast_Type *base, S32 size) {
U64 hash_base = hash_ptr(base);
U64 hash = hash_mix(hash_base, hash_u64(size));
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);
@@ -237,7 +237,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node) {
//
Array<Ast_Resolved_Member> members = {scratch.arena};
type->kind = TYPE_COMPLETING;
size_t members_size = 0;
S32 members_size = 0;
For(node->scope->decls) {
resolve_decl(it);
assert(it->type->kind != TYPE_INCOMPLETE);
@@ -247,7 +247,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node) {
m.offset = type->size;
members_size += it->type->size;
type->align = max(type->align, it->type->align);
type->size = it->type->size + align_up(type->size, it->type->align);
type->size = it->type->size + (S32)align_up(type->size, it->type->align);
m.name = it->name;
m.value = it->value;
@@ -257,7 +257,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node) {
//
// Then compute size of struct itself
//
type->size = align_up(type->size, type->align);
type->size = (S32)align_up(type->size, type->align);
type->padding = type->size - members_size;
type->agg.members = members.tight_copy(pctx->perm);
type->kind = TYPE_STRUCT;
@@ -285,7 +285,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node) {
//
// Then compute size of union itself
//
type->size = align_up(type->size, type->align);
type->size = (S32)align_up(type->size, type->align);
type->agg.members = members.tight_copy(pctx->perm);
type->kind = TYPE_UNION;
}
@@ -321,7 +321,7 @@ typename_base(String_Builder *sb, Ast_Type *type) {
sb->addf("(");
For(type->func.args) {
typename_base(sb, it);
if (!type->func.args.is_last(&it)) sb->addf(", ");
if (!type->func.args.is_last(it)) sb->addf(", ");
}
sb->addf("):");

View File

@@ -593,7 +593,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
str = va_arg(va, String);
if (str.str == 0 && str.len != 0)
str = string_null;
pr = str.len;
pr = (int)str.len;
s = (char *)str.str;
l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u);
lead[0] = 0;