Skip to content

Commit

Permalink
Fixed up some things I borked along the way with lists and loops.
Browse files Browse the repository at this point in the history
Minor edits.
  • Loading branch information
viega committed Jul 2, 2024
1 parent da5b998 commit fb95eda
Show file tree
Hide file tree
Showing 15 changed files with 310 additions and 152 deletions.
8 changes: 7 additions & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ function meson_build {
rm -rf ${1}
fi
log Creating meson target ${1}
meson setup ${1}

if [[ ${1} -ne "debug" ]] ; then
SETUP_ARGS=-Dbuildtype=release
else
SETUP_ARGS=-Dbuildtype=debugoptimized
fi
meson setup ${SETUP_ARGS} ${1}
fi
cd ${1}

Expand Down
23 changes: 20 additions & 3 deletions include/con4m.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,30 @@

// #define C4M_GC_ALL_OFF
// #define C4M_GC_ALL_ON
// #define C4M_VM_DEBUG
// #define C4M_TYPE_LOG

#ifndef C4M_NO_DEV_MODE
// When this is on, the `debug` instruction will run.
// Note that the debug instruction is not even generated unless
// C4M_DEV is on.

// #define C4M_VM_DEBUG
// #define C4M_VM_DEBUG_DEFAULT true

#ifdef C4M_NO_DEV_MODE
#undef C4M_DEV
#else
#define C4M_DEV
#endif

#if defined(C4M_VM_DEBUG)
#if !defined(C4M_DEV)
#error "Cannot debug VM when C4M_DEV_MODE is set."
#endif
#if !defined(C4M_VM_DEBUG_DEFAULT)
#define C4M_VM_DEBUG_DEFAULT false
#endif
#endif

#ifdef C4M_TRACE_GC
#ifndef C4M_GC_STATS
#define C4M_GC_STATS
Expand Down Expand Up @@ -49,7 +66,7 @@
#include "con4m/color.h"

// Basic "exclusive" (i.e., single threaded) list.
#include "con4m/xlist.h"
#include "con4m/list.h"

// Type system API.
#include "con4m/type.h"
Expand Down
1 change: 1 addition & 0 deletions include/con4m/datatypes/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ typedef enum : uint8_t {
C4M_ZNop = 0xFF,
#ifdef C4M_DEV
C4M_ZPrint = 0xFD,
C4M_ZDebug = 0xFC,
#endif
} c4m_zop_t;

Expand Down
23 changes: 23 additions & 0 deletions include/con4m/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "con4m.h"

extern void *c4m_list_get(c4m_list_t *, int64_t, bool *);
extern void c4m_list_append(c4m_list_t *list, void *item);
extern void c4m_list_add_if_unique(c4m_list_t *list,
void *item,
bool (*fn)(void *, void *));
extern void *c4m_list_pop(c4m_list_t *list);
extern void c4m_list_plus_eq(c4m_list_t *, c4m_list_t *);
extern c4m_list_t *c4m_list_plus(c4m_list_t *, c4m_list_t *);
extern bool c4m_list_set(c4m_list_t *, int64_t, void *);
extern c4m_list_t *c4m_list(c4m_type_t *);
extern int64_t c4m_list_len(const c4m_list_t *);
extern c4m_list_t *c4m_list_get_slice(c4m_list_t *, int64_t, int64_t);
extern void c4m_list_set_slice(c4m_list_t *,
int64_t,
int64_t,
c4m_list_t *);
extern bool c4m_list_contains(c4m_list_t *, c4m_obj_t);
extern c4m_list_t *c4m_list_copy(c4m_list_t *);
extern c4m_list_t *c4m_list_shallow_copy(c4m_list_t *);
8 changes: 4 additions & 4 deletions include/con4m/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern c4m_utf8_t *c4m_utf8_repeat(c4m_codepoint_t, int64_t);
extern c4m_utf32_t *c4m_utf32_repeat(c4m_codepoint_t, int64_t);
extern c4m_utf32_t *_c4m_str_strip(const c4m_str_t *s, ...);
extern c4m_str_t *_c4m_str_truncate(const c4m_str_t *s, int64_t, ...);
extern c4m_utf32_t *_c4m_str_join(const c4m_list_t *,
extern c4m_utf32_t *_c4m_str_join(c4m_list_t *,
const c4m_str_t *,
...);
extern c4m_utf8_t *c4m_str_from_int(int64_t n);
Expand All @@ -24,7 +24,7 @@ extern c4m_utf8_t *c4m_rich(c4m_utf8_t *, c4m_utf8_t *style);
extern c4m_codepoint_t c4m_index(const c4m_str_t *, int64_t);
extern bool c4m_str_can_coerce_to(c4m_type_t *, c4m_type_t *);
extern c4m_obj_t c4m_str_coerce_to(const c4m_str_t *, c4m_type_t *);
extern c4m_list_t *c4m_str_xsplit(c4m_str_t *, c4m_str_t *);
extern c4m_list_t *c4m_str_xsplit(c4m_str_t *, c4m_str_t *);
extern struct flexarray_t *c4m_str_fsplit(c4m_str_t *, c4m_str_t *);
extern bool c4m_str_starts_with(const c4m_str_t *,
const c4m_str_t *);
Expand Down Expand Up @@ -102,8 +102,8 @@ c4m_to_cstring(c4m_str_t *s)
return s->data;
}

extern c4m_list_t *c4m_u8_map(const c4m_list_t *);
extern bool c4m_str_eq(c4m_str_t *, c4m_str_t *);
extern c4m_list_t *c4m_u8_map(c4m_list_t *);
extern bool c4m_str_eq(c4m_str_t *, c4m_str_t *);

extern const uint64_t c4m_pmap_str[2];

Expand Down
53 changes: 0 additions & 53 deletions include/con4m/xlist.h

This file was deleted.

5 changes: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ else
using_osx = false
link_args = []
c_args = c_args + ['-D_GNU_SOURCE']

endif

exe_link_args = link_args + ['-flto', '-w']
Expand All @@ -54,6 +53,10 @@ if cc.links(fpty_code, name: 'forkpty_check')
add_project_arguments('-DHAVE_PTY_H', language: 'c')
endif

if (get_option('buildtype') == 'release')
add_project_arguments([ '-O2' ], language: 'c')
endif

c4m_src = ['src/con4m/style.c',
'src/con4m/colors.c',
'src/con4m/breaks.c',
Expand Down
4 changes: 2 additions & 2 deletions src/con4m/collect.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,10 @@ c4m_collect_arena(c4m_arena_t *from_space)
gstr = c4m_cstr_format("{}", c4m_box_double(u));
gstr = c4m_str_slice(gstr, 0, 5);

c4m_printf("[b]Collect utilization[/]: [em]{}[/]% garbage",
c4m_printf("[b]Collect utilization[/]: [em]{}%[/] [i]garbage",
gstr);

c4m_printf("[b]Average allocation size:[/] [em]{:,} bytes",
c4m_printf("[b]Average allocation size:[/] [em]{:,}[/] bytes",
c4m_box_u64((c4m_total_words * 8) / c4m_total_allocs));

#endif
Expand Down
62 changes: 41 additions & 21 deletions src/con4m/compiler/codegen.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#define C4M_USE_INTERNAL_API
#include "con4m.h"

#ifdef C4M_DEV
#define gen_debug(debug_arg) \
emit(ctx, C4M_ZDebug, c4m_kw("arg", c4m_ka(debug_arg)))
#define debug_label(cstr) \
gen_label(ctx, c4m_new_utf8(cstr))
#else
#define gen_debug(debug_arg)
#define debug_label(cstr)
#endif

// This is used in nested if/else only.
typedef struct {
c4m_jump_info_t *targets;
Expand Down Expand Up @@ -31,6 +41,7 @@ typedef struct {
int instruction_counter;
int current_stack_offset;
int max_stack_size;
int module_patch_loc;
bool lvalue;
assign_type_t assign_method;
c4m_symbol_t *retsym;
Expand Down Expand Up @@ -796,6 +807,8 @@ gen_module(gen_ctx *ctx)
int num_params = gen_parameter_checks(ctx);

emit(ctx, C4M_ZModuleEnter, c4m_kw("arg", c4m_ka(num_params)));
ctx->module_patch_loc = ctx->instruction_counter;
emit(ctx, C4M_ZMoveSp, c4m_kw("arg", c4m_ka(0)));
gen_kids(ctx);
}

Expand Down Expand Up @@ -1093,6 +1106,7 @@ gen_ranged_for(gen_ctx *ctx, c4m_loop_info_t *li)
// value. So if it's 0 .. 10, and we call subtract to calculate the
// length, we're going to be computing 0 - 10, not the opposite.
// So we first swap the two numbers.

emit(ctx, C4M_ZSwap);
// Subtract the 2 numbers now, but DO NOT POP; we need these.
emit(ctx, C4M_ZSubNoPop);
Expand Down Expand Up @@ -1137,6 +1151,7 @@ gen_ranged_for(gen_ctx *ctx, c4m_loop_info_t *li)
if (using_index) {
emit(ctx, C4M_ZPopToR3);
}

// pop the step for a second.
emit(ctx, C4M_ZPopToR1);
// If the two items are equal, we bail from the loop.
Expand All @@ -1147,13 +1162,14 @@ gen_ranged_for(gen_ctx *ctx, c4m_loop_info_t *li)
// Then push the step back on,
// And, if it's being used, the $i from R3.

GEN_JNZ(gen_sym_store(ctx, li->lvar_1, false);
emit(ctx, C4M_ZPushFromR1);
emit(ctx, C4M_ZAdd);
emit(ctx, C4M_ZPushFromR1);
possible_restore_from_r3(ctx, using_index);
gen_one_kid(ctx, ctx->cur_node->num_kids - 1);
gen_j(ctx, &ji_top));
GEN_JNZ(
gen_sym_store(ctx, li->lvar_1, false);
emit(ctx, C4M_ZPushFromR1);
emit(ctx, C4M_ZAdd);
emit(ctx, C4M_ZPushFromR1);
possible_restore_from_r3(ctx, using_index);
gen_one_kid(ctx, ctx->cur_node->num_kids - 1);
gen_j(ctx, &ji_top));
gen_apply_waiting_patches(ctx, &li->branch_info);
emit(ctx, C4M_ZMoveSp, c4m_kw("arg", c4m_ka(-3)));
}
Expand Down Expand Up @@ -1204,21 +1220,16 @@ gen_container_for(gen_ctx *ctx, c4m_loop_info_t *li)
//
// Also, note that the VIEW builtin doesn't need to copy objects,
// but it needs to give us a pointer to items, where we steal the
// lowest 2 bits in the pointer to represent the the log base 2 of
// lowest 3 bits in the pointer to represent the the log base 2 of
// the bytes in the item count. Currently, the only allowable
// view item sizes are therefore: 1 byte, 2 bytes, 4 bytes, 8
// bytes.
// view item sizes are 1 byte, 2 bytes, 4 bytes, 8 bytes. Anything
// else currently represents a bitfield (per below).
//
// Currently, we don't need more than 8bytes, since anything
// Currently, we don't need more than 8 bytes, since anything
// larger than 8 bytes is passed around as pointers. However, I
// expect to eventually add 128-bit ints, which would entail a
// double-word load.
//
// Therefore, views must be 8 byte aligned, because eventually we
// will steal a third bit. But that's no problem as long as view
// pointers start at the beginning of an allocation, since the
// allocator always spits out aligned objects.
//
// We have a bit of a special case for bitfield types (right now,
// just c4m_flags_t). There, the value of the "number of bytes"
// field will be set to 128; When we see that, we will use
Expand All @@ -1229,21 +1240,25 @@ gen_container_for(gen_ctx *ctx, c4m_loop_info_t *li)
// of first implementation, it will just always happen, even
// though in most cases it should be no problem to bind to the
// right approach statically.

gen_tcall(ctx, C4M_BI_VIEW, NULL);

// The length of the container is on top right now; the view is
// underneath. We want to store a copy to $len if appropriate,
// and then pop to a register 2, to work on the pointer. We'll
// push it back on top when we properly enter the loop.
gen_len_var_init(ctx, li);

// Move the container length out to a register for a bit.
emit(ctx, C4M_ZPopToR2);

emit(ctx, C4M_ZUnsteal);
// The bit length is actually encoded by taking log base 2; here
// were expand it back out before going into the loop.
emit(ctx, C4M_ZShlI, c4m_kw("arg", c4m_ka(0x1)));

// On top of this, put the iteration count, which at the start of
// each turn through the loop, will be the second item.

bool have_index_var = gen_index_var_init(ctx, li);
bool have_kv_pair = li->lvar_2 != NULL;

Expand Down Expand Up @@ -1276,18 +1291,17 @@ gen_container_for(gen_ctx *ctx, c4m_loop_info_t *li)
deal_with_iteration_count(ctx, li, have_index_var);
emit(ctx, C4M_ZPopToR1);
emit(ctx, C4M_ZLoadFromView, c4m_kw("arg", c4m_ka(have_kv_pair)));
// Store the item(s) to the appropriate loop variable(s).
store_view_item(ctx, li);
emit(ctx, C4M_ZPushFromR1); // Re-groom the stack; container length.
emit(ctx, C4M_ZPushFromR2); // Iteration count.
emit(ctx, C4M_ZPushFromR1); // Re-groom the stack; iter count
emit(ctx, C4M_ZPushFromR2); // container len
gen_one_kid(ctx, ctx->cur_node->num_kids - 1);
gen_j(ctx, &ji_top));
// After the loop:
// 1. backpatch.
// 2. Move the stack down four items, popping the count, len, item size,
// and container.
gen_apply_waiting_patches(ctx, &li->branch_info);
emit(ctx, C4M_ZMoveSp, c4m_kw("arg", c4m_ka(-4)));
emit(ctx, C4M_ZMoveSp, c4m_kw("arg", c4m_ka(-5)));
}

static inline void
Expand Down Expand Up @@ -2122,6 +2136,12 @@ gen_module_code(gen_ctx *ctx, c4m_vm_t *vm)
ctx->current_stack_offset = ctx->fctx->static_size;
ctx->max_stack_size = ctx->fctx->static_size;
gen_one_node(ctx);

c4m_zinstruction_t *sp_move = c4m_list_get(module->instructions,
ctx->module_patch_loc,
NULL);
sp_move->arg = ctx->max_stack_size;

emit(ctx, C4M_ZModuleRet);

module->module_var_size = ctx->max_stack_size;
Expand Down
6 changes: 6 additions & 0 deletions src/con4m/compiler/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ get_bool_label(c4m_zop_t op)
return c4m_new_utf8("currently unused");
case C4M_ZLoadFromView:
return c4m_new_utf8("load kv pair");
case C4M_ZDebug:
return c4m_new_utf8("set debug");
default:
c4m_unreachable();
}
Expand Down Expand Up @@ -334,6 +336,10 @@ const inst_info_t inst_info[256] = {
[C4M_ZPrint] = {
.name = "ZPrint",
},
[C4M_ZDebug] = {
.name = "ZDebug",
.arg_fmt = fmt_bool,
},
#endif
};

Expand Down
Loading

0 comments on commit fb95eda

Please sign in to comment.