Skip to content

Commit

Permalink
compiler: Fix more issues around external C func signatures
Browse files Browse the repository at this point in the history
DCO-1.1-Signed-off-by: Ellie <[email protected]>
  • Loading branch information
ell1e committed Oct 12, 2024
1 parent 36af9bc commit 0b90ef8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 22 deletions.
46 changes: 33 additions & 13 deletions src/compiler/builtin_syms.h64
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ type BuiltinSymsStorageItem {
var is_c_builtin_type = no
var hidden = no
var is_csymbol = no
var func_signature = none
var func_signature_expr = none
var func_signature_str = none
}

func BuiltinSymsStorageItem.init(
Expand All @@ -65,7 +66,12 @@ func BuiltinSymsStorageItem.init(
self.module_path = module_path.copy()
self.package_name = package_name
self.kind = item_kind
self.func_signature = func_signature
if func_signature != none and
typename(func_signature) == "str" {
self.func_signature_str = func_signature + ""
} else {
self.func_signature_expr = func_signature
}
}

func BuiltinSymsStorageItem.as_str {
Expand All @@ -83,32 +89,38 @@ func BuiltinSymsStorageItem.as_str {
mod_path_str +
"symbol_storage_ref=" +
self.symbol_storage_ref.as_str() + ""
) + "}"
)
if self.func_signature_expr != none {
t += ", func_signature_expr=" +
self.func_signature_expr.as_str()
}
t += "}"
return t
}

func BuiltinSymsStorageItem.resolve_func_signature(
project=none, is_moose64=no, debug=no
func resolve_func_signature_of_item(
item, project=none, is_moose64=no, debug=no
) {
var program_name = if is_moose64 ("moosec") else ("horsec")
if typename(self.func_signature) != "str" {
if item.func_signature_expr != none or
item.func_signature_str == none {
return later
}
if debug {
print(program_name + ": debug: " +
"BuiltinSymsStorageItem.resolve_func_signature: "
"builtin_syms.resolve_func_signature_of_item: "
"Resolving func signature: " +
self.func_signature)
item.func_signature_str)
}
var result = token.tokenize_str(
self.func_signature, keep_whitespace=no,
item.func_signature_str, keep_whitespace=no,
is_isolated_type=yes,
is_moose64=is_moose64,
)

if debug {
print(program_name + ": debug: " +
"BuiltinSymsStorageItem.resolve_func_signature: "
"builtin_syms.resolve_func_signature_of_item: "
"Tokenized func signature: " + result.tokens.as_str())
}
var tokens = result.tokens
Expand Down Expand Up @@ -136,7 +148,15 @@ func BuiltinSymsStorageItem.resolve_func_signature(
}
}
assert(result_typeref != none)
self.func_signature = result_typeref
if item.is_csymbol {
result_typeref.func_storage_ref =
new st_ref.StorageRef(st_ref.ST_MCREF,
item.storage_id)
} else {
result_typeref.func_storage_ref =
item.symbol_storage_ref.copy()
}
item.func_signature_expr = result_typeref

return later
}
Expand Down Expand Up @@ -363,8 +383,8 @@ func register_symbol(project, symbol_name, symbol_ref,
return_result = st_item.symbol_storage_ref.copy()
}

st_item.resolve_func_signature(
project=project, is_moose64=is_moose64
resolve_func_signature_of_item(
st_item, project=project, is_moose64=is_moose64
) later:

return later return_result
Expand Down
36 changes: 33 additions & 3 deletions src/compiler/moose64/ast/analyze/analyze.h64
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ func _get_c_or_m64_type_of_node_do(
}

if sym_info == none {
if node.ref.kind == st_ref.ST_MCREF and
if node.ref != none and
node.ref.kind == st_ref.ST_MCREF and
(node.ref.id == builtin_syms.get_item_by_name(
project_file.project, "size_of",
in_package_name="m64.horse64.org",
Expand Down Expand Up @@ -641,10 +642,39 @@ func _get_c_or_m64_type_of_node_do(
))
}
return later none
} elseif node.ref.kind == st_ref.ST_MCREF {
} elseif node.ref != none and
node.ref.kind == st_ref.ST_MCREF {
# FIXME: Obtain C func signature. For now,
# we don't support this.
return later none
var builtin_item = builtin_syms.get_item_by_storage_ref(
project_file.project, node.ref
)
if debug {
print(dbg_prefix + " Obtained built-in symbol item "
"for this node.ref: builtin_item=" +
builtin_item.as_str())
}
if builtin_item == none or
builtin_item.func_signature_expr == none {
return later none
}
var func_result = new typeinfo.TypeInfo(
typeinfo.TI_FUNCREF
)
func_result.orig_type_name = builtin_item.symbol_name
if builtin_item.is_csymbol {
func_result.user_type_ref =
new st_ref.StorageRef(st_ref.ST_MCREF,
builtin_item.storage_id)
} else {
func_result.user_type_ref =
builtin_item.symbol_storage_ref.copy()
}
func_result.func_typeexpr =
builtin_item.func_signature_expr.copy()
func_result._orig_typeexpr =
func_result.func_typeexpr
return func_result
}

if debug {
Expand Down
30 changes: 27 additions & 3 deletions src/compiler/moose64/c_importer.h64
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import path from core.horse64.org

import compiler.builtin_syms as builtin_syms
import compiler.moose64.c_symbol as c_symbol
import compiler.project as project

Expand All @@ -53,6 +54,7 @@ func get_mcref_id(project) {
}

func CImportInfo._process_c_include(project, ipath) {
const is_moose64 = yes
ipath = ipath.replace("/", path.sep)
var new_items = {->}
if ipath == "stdlib.h" {
Expand All @@ -69,13 +71,13 @@ func CImportInfo._process_c_include(project, ipath) {
"void *memcpy(void *dest, const void *src, size_t n);",
_override_func_signature=
"(<-any ref, <-readonly any ref, "
"size)->any ref"
"<-size)->any ref"
)
new_items["memmove"] = new c_symbol.CFuncSymbol(
"void *memmove(void *dest, const void *src, size_t n);",
_override_func_signature=
"(<-any ref, <-readonly any ref, "
"size)->any ref"
"<-size)->any ref"
)
new_items["strlen"] = new c_symbol.CFuncSymbol(
"size_t strlen(const char *);",
Expand All @@ -85,16 +87,38 @@ func CImportInfo._process_c_include(project, ipath) {
} elseif ipath == "stdio.h" {
new_items["printf"] = new c_symbol.CFuncSymbol(
"int printf(const char *fmt, ...);",
_override_func_signature=
"(<-readonly byte ref, <-readonly byte ref)->empty"
# FIXME: The Moose64 signature is incorrect here.
)
}
var queue = []
for entry in new_items {
if not self.known_items.has(entry) {
self.known_items[entry] = new_items[entry]
self.known_items[entry].auto_assign_id(project)
assert(self.known_items[entry].storage_id != none)
queue.add(new_items[entry])
}
}
return later

# Now make sure the func signature is properly parsed:
if queue.len == 0 {
return later
}
var next_item = queue.pop()
var result = builtin_syms.resolve_func_signature_of_item(
next_item, project=project, is_moose64=is_moose64,
) later:

await result
if queue.len == 0 {
return later
}
next_item = queue.pop()
result = builtin_syms.resolve_func_signature_of_item(
next_item, project=project, is_moose64=is_moose64,
) later repeat
}

func CImportInfo.add_import_path(project, ipath) {
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/moose64/c_symbol.h64
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import text from core.horse64.org
import compiler.moose64.c_importer as m64_c_importer

type CFuncSymbol {
var c_func_signature protect
var c_func_signature_str protect
var func_signature_str protect
var func_signature_expr protect

var storage_id protect

Expand Down Expand Up @@ -70,7 +72,14 @@ func get_ident_ending_at(s, idx) {
}

func CFuncSymbol.init(c_signature, _override_func_signature=none) {
self.c_func_signature = c_signature
self.c_func_signature_str = c_signature
if _override_func_signature != none {
if typename(_override_func_signature) == "str" {
self.func_signature_str = _override_func_signature
} else {
self.func_signature_expr = _override_func_signature
}
}

var symbol_name = none
var had_bracket = no
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/moose64/codegen/c_gen.h64
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ func node_to_c_string(project_file, node, parents,
var var_tinfo = m64_ast_analyze.get_c_or_m64_type_of_node(
project_file, node, parents,
store_self_value_ref_for_funcs=no,
msgs=msgs
msgs=msgs,
) later:

await var_tinfo
Expand Down

0 comments on commit 0b90ef8

Please sign in to comment.