Skip to content

Commit

Permalink
'use' statements (#89)
Browse files Browse the repository at this point in the history
I did the work to get use statements working properly:

1. I redid all the module loading logic.
2. I did a basic wrapping of libcurl for http /  https support.  `GET` and `PUT` are supported (`c4m_http_get` and `c4m_http_upload`), including w/ AWS v4 signature support. It still needs wrapping of form posting and certificate pinning, and wrapping of the mime interface.

I also ripped out the module lock stack; though after talking to Miro, I'm going to re-implement it in the next week.
  • Loading branch information
viega authored Jul 22, 2024
1 parent ae7a185 commit 1da1ac5
Show file tree
Hide file tree
Showing 80 changed files with 3,172 additions and 2,331 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
echo "CC=${{ matrix.cc }}" >> "${GITHUB_ENV}"
clang --version
- name: On Linux, install curl
if: runner.os == 'Linux'
run: sudo apt-get install libcurl4-openssl-dev

- name: On macOS, allow Meson to find libcrypto and libssl
if: runner.os == 'macOS'
run: |
Expand Down
37 changes: 26 additions & 11 deletions dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

C4M_DEV_CFG="--buildtype=debug -Duse_memcheck=on -Ddev_mode=true"
C4M_RELEASE_CFG="--buildtype=release"
C4M_DEBUG_EXTRA="-Duse_ubsan=enabled -Duse_asan=enabled -Dshow_preprocessor_config=enabled -Dforkless_tests=enabled"
C4M_DEBUG_EXTRA="-Dshow_preprocessor_config=enabled -Dforkless_tests=enabled -Duse_memcheck=on -Dexception_traces=enabled"
C4M_SANITIZE_EXTRA="-Duse_ubsan=enabled -Duse_asan=enabled -Dshow_preprocessor_config=enabled -Dforkless_tests=enabled"
C4M_TEST_CFG=${C4M_DEV_CFG}


Expand Down Expand Up @@ -124,7 +125,9 @@ function ensure_project {
}

function configure_target {
meson configure ${C4M_BUILD_DIR} ${C4M_PASSTHROUGH_ARGS}
if [[ $# -ne 0 ]] ; then
meson configure ${C4M_BUILD_DIR} ${C4M_PASSTHROUGH_ARGS}
fi
}

function build_target {
Expand Down Expand Up @@ -155,19 +158,22 @@ function set_profile {
case ${C4M_BUILD_TARGET} in
dev)
export C4M_MESON_CONF="${C4M_DEV_CFG}"
;;
;;
sanitize)
export C4M_MESON_CONF="${C4M_DEV_CFG} ${C4M_SANITIZE_EXTRA}"
;;
debug)
export C4M_MESON_CONF="${C4M_DEV_CFG} ${C4M_DEBUG_EXTRA}"
;;
;;
release)
export C4M_MESON_CONF="${C4M_RELEASE_CFG}"
;;
;;
test)
export C4M_MESON_CONF="${C4M_TEST_CFG}"
;;
;;
*)
echo $(color RED "[-- libcon4m --] ") Invalid profile: $(color RED ${C4M_BUILD_TARGET})
echo $(color blue "Valid targets are: dev, debug, release, test")
echo $(color blue "Valid targets are: dev, debug, release, sanitize, test")
return -1
;;
esac
Expand Down Expand Up @@ -198,7 +204,7 @@ function clean_one_target {

function libcon4m_clean {
if [[ "$1" == "all" ]] ; then
for TARGET in dev debug release test ; do
for TARGET in dev sanitize debug release test ; do
clean_one_target ${TARGET}
done
if [[ -f ${C4M_MESON_STATE_FILE} ]]; then
Expand Down Expand Up @@ -236,9 +242,15 @@ function libcon4m_set_profile {
}

function libcon4m_prebuild {
CONFIGURE=1
if [[ ${1} -eq 0 ]] ; then
shift
CONFIGURE=0
fi

if [[ $# -ne 0 ]]; then
case ${1} in
dev | debug | release | test)
dev | debug | release | sanitize | test)
set_profile $@
shift
;;
Expand All @@ -253,6 +265,10 @@ function libcon4m_prebuild {

ensure_directory
ensure_project

if [[ CONFIGURE -ne 0 ]] ; then
configure_target
fi
}

function libcon4m_compile {
Expand Down Expand Up @@ -286,8 +302,7 @@ function libcon4m_run_tests {
}

function libcon4m_run {
libcon4m_prebuild $@

libcon4m_prebuild 0 $@
libcon4m_compile

set -e
Expand Down
6 changes: 6 additions & 0 deletions include/adts/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ c4m_str_crlf()
return c4m_str_copy(c4m_crlf_const);
}

static inline c4m_str_t *
c4m_str_replace(c4m_str_t *base, c4m_str_t *sub_old, c4m_str_t *sub_new)
{
return c4m_str_join(c4m_str_split(base, sub_old), sub_new);
}

#define c4m_new_utf8(to_copy) \
c4m_new(c4m_type_utf8(), c4m_kw("cstring", c4m_ka(to_copy)))

Expand Down
4 changes: 2 additions & 2 deletions include/compiler/ast_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ _show_pattern(c4m_tpat_node_t *pat)

extern bool c4m_tcmp(int64_t, c4m_tree_node_t *);
extern void c4m_setup_treematch_patterns();
extern c4m_type_t *c4m_node_to_type(c4m_file_compile_ctx *,
extern c4m_type_t *c4m_node_to_type(c4m_module_compile_ctx *,
c4m_tree_node_t *,
c4m_dict_t *);
extern c4m_obj_t
c4m_node_to_callback(c4m_file_compile_ctx *ctx, c4m_tree_node_t *n);
c4m_node_to_callback(c4m_module_compile_ctx *ctx, c4m_tree_node_t *n);

static inline bool
c4m_node_has_type(c4m_tree_node_t *node, c4m_node_kind_t expect)
Expand Down
2 changes: 1 addition & 1 deletion include/compiler/cfgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern c4m_cfg_node_t *c4m_cfg_add_use(c4m_cfg_node_t *,
c4m_tree_node_t *,
c4m_symbol_t *);
extern c4m_grid_t *c4m_cfg_repr(c4m_cfg_node_t *);
extern void c4m_cfg_analyze(c4m_file_compile_ctx *, c4m_dict_t *);
extern void c4m_cfg_analyze(c4m_module_compile_ctx *, c4m_dict_t *);

static inline c4m_cfg_node_t *
c4m_cfg_exit_node(c4m_cfg_node_t *block_entry)
Expand Down
2 changes: 1 addition & 1 deletion include/compiler/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#ifdef C4M_USE_INTERNAL_API
extern void c4m_layout_module_symbols(c4m_compile_ctx *,
c4m_file_compile_ctx *);
c4m_module_compile_ctx *);
extern int64_t c4m_layout_string_const(c4m_compile_ctx *, c4m_str_t *);
extern uint32_t _c4m_layout_const_obj(c4m_compile_ctx *, c4m_obj_t, ...);
extern c4m_grid_t *c4m_disasm(c4m_vm_t *, c4m_zmodule_info_t *m);
Expand Down
47 changes: 9 additions & 38 deletions include/compiler/compile.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
#pragma once
#include "con4m.h"

extern void _c4m_set_package_search_path(c4m_utf8_t *, ...);
extern c4m_compile_ctx *c4m_new_compile_context(c4m_str_t *);
extern bool c4m_validate_module_info(c4m_file_compile_ctx *);
extern c4m_compile_ctx *c4m_compile_from_entry_point(c4m_str_t *);
extern void c4m_perform_module_loads(c4m_compile_ctx *);
extern c4m_file_compile_ctx *c4m_init_module_from_loc(c4m_compile_ctx *,
c4m_str_t *);
extern c4m_type_t *c4m_str_to_type(c4m_utf8_t *);
extern c4m_vm_t *c4m_generate_code(c4m_compile_ctx *);
extern c4m_file_compile_ctx *c4m_new_file_compile_ctx();

#define c4m_set_package_search_path(x, ...) \
_c4m_set_package_search_path(x, C4M_VA(__VA_ARGS__))
extern c4m_compile_ctx *c4m_new_compile_context(c4m_str_t *);
extern c4m_compile_ctx *c4m_compile_from_entry_point(c4m_str_t *);
extern c4m_module_compile_ctx *c4m_init_module_from_loc(c4m_compile_ctx *,
c4m_str_t *);
extern c4m_type_t *c4m_str_to_type(c4m_utf8_t *);
extern c4m_vm_t *c4m_generate_code(c4m_compile_ctx *);

static inline bool
c4m_got_fatal_compiler_error(c4m_compile_ctx *ctx)
Expand All @@ -22,29 +15,7 @@ c4m_got_fatal_compiler_error(c4m_compile_ctx *ctx)
}

#ifdef C4M_USE_INTERNAL_API
extern void c4m_file_decl_pass(c4m_compile_ctx *,
c4m_file_compile_ctx *);
extern void c4m_check_pass(c4m_compile_ctx *);
extern c4m_file_compile_ctx *c4m_init_from_use(c4m_compile_ctx *,
c4m_str_t *,
c4m_str_t *,
c4m_str_t *);

#define C4M_INDEX_FN "$index"
#define C4M_SLICE_FN "$slice"
#define C4M_PLUS_FN "$plus"
#define C4M_MINUS_FN "$minus"
#define C4M_MUL_FN "$mul"
#define C4M_MOD_FN "$mod"
#define C4M_DIV_FN "$div"
#define C4M_FDIV_FN "$fdiv"
#define C4M_SHL_FN "$shl"
#define C4M_SHR_FN "$shr"
#define C4M_BAND_FN "$bit_and"
#define C4M_BOR_FN "$bit_or"
#define C4M_BXOR_FN "$bit_xor"
#define C4M_CMP_FN "$cmp"
#define C4M_SET_INDEX "$set_index"
#define C4M_SET_SLICE "$set_slice"

extern void c4m_module_decl_pass(c4m_compile_ctx *,
c4m_module_compile_ctx *);
extern void c4m_check_pass(c4m_compile_ctx *);
#endif
4 changes: 2 additions & 2 deletions include/compiler/dt_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ typedef struct {
c4m_scope_t *final_attrs;
c4m_scope_t *final_globals;
c4m_spec_t *final_spec;
c4m_file_compile_ctx *entry_point;
c4m_file_compile_ctx *sys_package;
c4m_module_compile_ctx *entry_point;
c4m_module_compile_ctx *sys_package;
c4m_dict_t *module_cache;
c4m_list_t *module_ordering;
c4m_set_t *backlog; // Modules we need to process.
Expand Down
5 changes: 3 additions & 2 deletions include/compiler/dt_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "con4m.h"

typedef enum {
c4m_err_open_file,
c4m_err_open_module,
c4m_err_location,
c4m_err_lex_stray_cr,
c4m_err_lex_eof_in_comment,
Expand Down Expand Up @@ -112,7 +112,7 @@ typedef enum {
c4m_err_malformed_url,
c4m_warn_no_tls,
c4m_err_search_path,
c4m_err_no_http,
c4m_err_invalid_path,
c4m_info_recursive_use,
c4m_err_self_recursive_use,
c4m_err_redecl_kind,
Expand Down Expand Up @@ -152,6 +152,7 @@ typedef enum {
c4m_err_switch_case_type,
c4m_err_concrete_typeof,
c4m_warn_type_overlap,
c4m_warn_empty_case,
c4m_err_dead_branch,
c4m_err_no_ret,
c4m_err_use_no_def,
Expand Down
2 changes: 1 addition & 1 deletion include/compiler/dt_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef enum {
} c4m_token_kind_t;

typedef struct {
struct c4m_file_compile_ctx *module;
struct c4m_module_compile_ctx *module;
c4m_codepoint_t *start_ptr;
c4m_codepoint_t *end_ptr;
c4m_utf8_t *literal_modifier;
Expand Down
15 changes: 6 additions & 9 deletions include/compiler/dt_files.h → include/compiler/dt_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ typedef enum {
c4m_compile_status_tree_typed, // full symbols and parsing.
c4m_compile_status_applied_folding, // Skippable and not done yet.
c4m_compile_status_generated_code
} c4m_file_compile_status;
} c4m_module_compile_status;

typedef struct c4m_file_compile_ctx {
typedef struct c4m_module_compile_ctx {
#ifdef C4M_DEV
// Cache all the print nodes to type check before running.
c4m_list_t *print_nodes;
Expand All @@ -31,10 +31,9 @@ typedef struct c4m_file_compile_ctx {
// pass raw source as long as you give at least a module name).

c4m_str_t *module; // Module name.
c4m_str_t *authority; // http/s only.
c4m_str_t *path; // Fully qualified path
c4m_str_t *provided_path; // Provided in use statement.
c4m_str_t *package; // Package name.
c4m_str_t *loaded_from; // Abs path / URL if found.
c4m_utf32_t *raw; // raw contents before lex pass.
c4m_list_t *tokens; // an xlist of x4m_token_t objects;
c4m_tree_node_t *parse_tree;
Expand All @@ -56,10 +55,8 @@ typedef struct c4m_file_compile_ctx {
uint64_t module_id; // Module hash.
int32_t static_size;
uint32_t num_params;
uint32_t local_module_id;
uint32_t local_module_id; // Index in object file.
unsigned int fatal_errors : 1;
unsigned int file : 1;
unsigned int secure : 1;
c4m_file_compile_status status;
c4m_module_compile_status status;

} c4m_file_compile_ctx;
} c4m_module_compile_ctx;
22 changes: 11 additions & 11 deletions include/compiler/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ extern c4m_compile_error *c4m_base_add_error(c4m_list_t *,
c4m_token_t *,
c4m_err_severity_t,
va_list);
extern c4m_compile_error *_c4m_add_error(c4m_file_compile_ctx *,
extern c4m_compile_error *_c4m_add_error(c4m_module_compile_ctx *,
c4m_compile_error_t,
c4m_tree_node_t *,
...);
extern c4m_compile_error *_c4m_add_warning(c4m_file_compile_ctx *,
extern c4m_compile_error *_c4m_add_warning(c4m_module_compile_ctx *,
c4m_compile_error_t,
c4m_tree_node_t *,
...);
extern c4m_compile_error *_c4m_add_info(c4m_file_compile_ctx *,
extern c4m_compile_error *_c4m_add_info(c4m_module_compile_ctx *,
c4m_compile_error_t,
c4m_tree_node_t *,
...);
Expand All @@ -35,16 +35,16 @@ extern c4m_compile_error *_c4m_add_spec_info(c4m_spec_t *,
c4m_compile_error_t,
c4m_tree_node_t *,
...);
extern c4m_compile_error *_c4m_error_from_token(c4m_file_compile_ctx *,
extern c4m_compile_error *_c4m_error_from_token(c4m_module_compile_ctx *,
c4m_compile_error_t,
c4m_token_t *,
...);

extern void _c4m_file_load_error(c4m_file_compile_ctx *,
extern void _c4m_module_load_error(c4m_module_compile_ctx *,
c4m_compile_error_t,
...);

extern void _c4m_file_load_warn(c4m_file_compile_ctx *,
extern void _c4m_module_load_warn(c4m_module_compile_ctx *,
c4m_compile_error_t,
...);

Expand All @@ -71,14 +71,14 @@ extern c4m_compile_error *c4m_new_error(int);
#define c4m_error_from_token(x, y, z, ...) \
_c4m_error_from_token(x, y, z, C4M_VA(__VA_ARGS__))

#define c4m_file_load_error(x, y, ...) \
_c4m_file_load_error(x, y, C4M_VA(__VA_ARGS__))
#define c4m_module_load_error(x, y, ...) \
_c4m_module_load_error(x, y, C4M_VA(__VA_ARGS__))

#define c4m_file_load_warn(x, y, ...) \
_c4m_file_load_warn(x, y, C4M_VA(__VA_ARGS__))
#define c4m_module_load_warn(x, y, ...) \
_c4m_module_load_warn(x, y, C4M_VA(__VA_ARGS__))

static inline bool
c4m_fatal_error_in_module(c4m_file_compile_ctx *ctx)
c4m_fatal_error_in_module(c4m_module_compile_ctx *ctx)
{
return ctx->fatal_errors != 0;
}
4 changes: 2 additions & 2 deletions include/compiler/lex.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include "con4m.h"

extern bool c4m_lex(c4m_file_compile_ctx *, c4m_stream_t *);
extern bool c4m_lex(c4m_module_compile_ctx *, c4m_stream_t *);
extern c4m_utf8_t *c4m_format_one_token(c4m_token_t *, c4m_str_t *);
extern c4m_grid_t *c4m_format_tokens(c4m_file_compile_ctx *);
extern c4m_grid_t *c4m_format_tokens(c4m_module_compile_ctx *);
extern c4m_utf8_t *c4m_token_type_to_string(c4m_token_kind_t);
extern c4m_utf8_t *c4m_token_raw_content(c4m_token_t *);
Loading

0 comments on commit 1da1ac5

Please sign in to comment.