Skip to content

Commit

Permalink
update TB with fixes; std changes
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Feb 28, 2024
1 parent 4f68798 commit 4391c1c
Show file tree
Hide file tree
Showing 17 changed files with 7,284 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Language: C
Language: Cpp
AlignAfterOpenBracket: BlockIndent
ColumnLimit: 0
IndentCaseBlocks: false
Expand Down
33 changes: 33 additions & 0 deletions main/env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

local function more(tab, indent, path, done)
for i=1, #done do
if done[i] == tab then
return
end
end
done[#done + 1] = tab
local prefix = indent .. path
if type(tab) == 'table' then
print(prefix .. ': ' .. 'table {')
for k,v in pairs(tab) do
if type(k) == 'string' then
more(v, indent .. ' ', k, done)
elseif type(k) == 'number' then
more(v, indent .. ' ', '[' .. tostring(k) .. ']', done)
else
print(k)
end
end
elseif type(tab) == 'function' then
print(prefix .. ': ' .. 'function')
elseif type(tab) == 'string' then
print(prefix .. ': ' .. 'string')
elseif type(tab) == 'number' then
print(prefix .. ': ' .. tostring(tab))
else
print(tab)
end
done[#done] = nil
end

more(_G, '', '_ENV', '_G', {})
4 changes: 2 additions & 2 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ typedef struct {

EMSCRIPTEN_KEEPALIVE vm_main_t *vm_main_new(void) {
vm_main_t *ret = vm_malloc(sizeof(vm_main_t));
ret->std = vm_std_new();
ret->config = (vm_config_t){
.use_tb_opt = false,
.use_num = VM_USE_NUM_I64,
.target = VM_TARGET_TB_EMCC,
};
ret->std = vm_std_new(&ret->config);
ret->blocks = (vm_blocks_t) {0};
return ret;
}
Expand Down Expand Up @@ -63,7 +63,7 @@ int main(int argc, char **argv) {
vm_config_t *config = &val_config;
bool echo = false;
bool isrepl = true;
vm_table_t *std = vm_std_new();
vm_table_t *std = vm_std_new(config);
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
if (!strcmp(arg, "--")) {
Expand Down
4 changes: 3 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ TREES_SRCS = vendor/trees/alloc.c vendor/trees/get_changed_ranges.c vendor/trees

STD_SRCS = vm/std/io.c vm/std/std.c
ISOCLINE_SRCS = vendor/isocline/isocline.c
VM_SRCS = vm/ir/ir.c vm/lib.c vm/ir/type.c vm/ast/build.c vm/ast/ast.c vm/ast/comp.c vm/ast/print.c vm/obj.c vm/backend/tb.c vm/backend/exec.c vm/ir/check.c vm/ir/rblock.c vm/lua/parser.c vm/lua/scan.c vm/lua/ast.c vm/lua/repl.c $(ISOCLINE_SRCS)
XXH_SRCS = vendor/xxhash/xxhash.c
VM_SRCS = vm/ir/ir.c vm/lib.c vm/ir/type.c vm/ast/build.c vm/ast/ast.c vm/ast/comp.c vm/ast/print.c vm/obj.c vm/backend/tb.c vm/backend/exec.c vm/ir/check.c vm/ir/rblock.c vm/lua/parser.c vm/lua/scan.c vm/lua/ast.c vm/lua/repl.c $(ISOCLINE_SRCS) $(XXH_SRCS)


ALL_SRCS = $(VM_SRCS) $(STD_SRCS) $(EXTRA_SRCS) $(TREES_SRCS)
ALL_OBJS = $(ALL_SRCS:%.c=$(OBJ_DIR)/%.o)
Expand Down
2 changes: 1 addition & 1 deletion vendor/tb/src/opt/branches.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ static TB_Node* ideal_branch(TB_Passes* restrict opt, TB_Function* f, TB_Node* n

// flip successors
if (cmp_type == TB_CMP_EQ) {
SWAP(uint64_t, br->keys[0].taken, br->keys[1].taken);
br->keys[0].taken = br->total_hits - br->keys[0].taken;
FOR_USERS(u, n) {
TB_NodeProj* p = TB_NODE_GET_EXTRA(u->n);
p->index = !p->index;
Expand Down
15 changes: 7 additions & 8 deletions vendor/tb/src/opt/print_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ typedef struct {
nl_buffer_t *globals;
nl_buffer_t *pre;
nl_buffer_t *buf;
TB_Node *return_block;
void *arena;
ptrdiff_t loop_goes_to;
int depth;
Expand Down Expand Up @@ -652,10 +651,10 @@ static void c_fmt_bb(CFmtState* ctx, TB_Node* bb_start) {
TB_BasicBlock* bb = ctx->opt->scheduled[bb_start->gvn];
Worklist* ws = &ctx->opt->worklist;

#ifndef NDEBUG
TB_BasicBlock* expected = &nl_map_get_checked(ctx->cfg.node_to_block, bb_start);
assert(expected == bb);
#endif
// #ifndef NDEBUG
// TB_BasicBlock* expected = &nl_map_get_checked(ctx->cfg.node_to_block, bb_start);
// assert(expected == bb);
// #endif

CFmtBlockRange *range = c_fmt_get_block_range(ctx, bb_start);

Expand Down Expand Up @@ -911,6 +910,8 @@ static void c_fmt_bb(CFmtState* ctx, TB_Node* bb_start) {
break;
}

case TB_NATURAL_LOOP:
case TB_AFFINE_LOOP:
case TB_CALLGRAPH: {
break;
}
Expand Down Expand Up @@ -1389,7 +1390,7 @@ static void c_fmt_bb(CFmtState* ctx, TB_Node* bb_start) {
FOREACH_N(i, 2, 4) {
if (projs[i] == NULL) break;
c_fmt_output(ctx, projs[i]);
nl_buffer_format(ctx->buf, "ret.v%u;\n", projs[i]->gvn, projs[i]->gvn);
nl_buffer_format(ctx->buf, "ret.v%u;\n", projs[i]->gvn);
}
}
ctx->depth -= 1;
Expand Down Expand Up @@ -1611,8 +1612,6 @@ TB_API char *tb_pass_c_fmt(TB_Passes* opt) {
tb_pass_schedule(opt, ctx.cfg, false);
worklist_clear_visited(&opt->worklist);

ctx.return_block = opt->worklist.items[ctx.cfg.block_count - 1];

// TB_Node* end_bb = NULL;
FOREACH_N(i, 0, ctx.cfg.block_count) {
ctx.depth += 1;
Expand Down
6 changes: 3 additions & 3 deletions vendor/tb/src/tb_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ TB_Node* tb_inst_if(TB_Function* f, TB_Node* cond, TB_Node* if_true, TB_Node* if
TB_Node* mem_state = peek_mem(f);

// generate control projections
TB_Node* n = tb_alloc_node(f, TB_BRANCH, TB_TYPE_TUPLE, 2, sizeof(TB_NodeBranch) + sizeof(int64_t));
TB_Node* n = tb_alloc_node(f, TB_BRANCH, TB_TYPE_TUPLE, 2, sizeof(TB_NodeBranch) + sizeof(TB_BranchKey));
set_input(f, n, transfer_ctrl(f, NULL), 0);
set_input(f, n, cond, 1);

Expand All @@ -969,7 +969,7 @@ TB_Node* tb_inst_if2(TB_Function* f, TB_Node* cond, TB_Node* projs[2]) {
TB_Node* mem_state = peek_mem(f);

// generate control projections
TB_Node* n = tb_alloc_node(f, TB_BRANCH, TB_TYPE_TUPLE, 2, sizeof(TB_NodeBranch) + sizeof(int64_t));
TB_Node* n = tb_alloc_node(f, TB_BRANCH, TB_TYPE_TUPLE, 2, sizeof(TB_NodeBranch) + sizeof(TB_BranchKey));
set_input(f, n, transfer_ctrl(f, NULL), 0);
set_input(f, n, cond, 1);

Expand Down Expand Up @@ -997,7 +997,7 @@ TB_Node* tb_inst_branch(TB_Function* f, TB_DataType dt, TB_Node* key, TB_Node* d
TB_Node* mem_state = peek_mem(f);

// generate control projections
TB_Node* n = tb_alloc_node(f, TB_BRANCH, TB_TYPE_TUPLE, 2, sizeof(TB_NodeBranch) + (sizeof(int64_t) * entry_count));
TB_Node* n = tb_alloc_node(f, TB_BRANCH, TB_TYPE_TUPLE, 2, sizeof(TB_NodeBranch) + (sizeof(TB_BranchKey) * entry_count));
set_input(f, n, transfer_ctrl(f, NULL), 0);
set_input(f, n, key, 1);

Expand Down
42 changes: 42 additions & 0 deletions vendor/xxhash/xxhash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* xxHash - Extremely Fast Hash algorithm
* Copyright (C) 2012-2023 Yann Collet
*
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/

/*
* xxhash.c instantiates functions defined in xxhash.h
*/

#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */

#include "xxhash.h"
Loading

0 comments on commit 4391c1c

Please sign in to comment.