bring back code that uses export and fix tests

partial revert of 1fdebc1dc4881a00766f7c2b4b2d8ee6ad6e79b6
This commit is contained in:
Andrew Kelley 2017-12-19 02:39:43 -05:00
parent 27ba4f0baf
commit 9d9201c3b4
19 changed files with 443 additions and 698 deletions

View File

@ -136,6 +136,7 @@
<li><a href="#builtin-divFloor">@divFloor</a></li> <li><a href="#builtin-divFloor">@divFloor</a></li>
<li><a href="#builtin-divTrunc">@divTrunc</a></li> <li><a href="#builtin-divTrunc">@divTrunc</a></li>
<li><a href="#builtin-embedFile">@embedFile</a></li> <li><a href="#builtin-embedFile">@embedFile</a></li>
<li><a href="#builtin-export">@export</a></li>
<li><a href="#builtin-tagName">@tagName</a></li> <li><a href="#builtin-tagName">@tagName</a></li>
<li><a href="#builtin-EnumTagType">@EnumTagType</a></li> <li><a href="#builtin-EnumTagType">@EnumTagType</a></li>
<li><a href="#builtin-errorName">@errorName</a></li> <li><a href="#builtin-errorName">@errorName</a></li>
@ -4368,6 +4369,11 @@ test.zig:6:2: error: found compile log statement
<ul> <ul>
<li><a href="#builtin-import">@import</a></li> <li><a href="#builtin-import">@import</a></li>
</ul> </ul>
<h3 id="builtin-export">@export</h3>
<pre><code class="zig">@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) -&gt; []const u8</code></pre>
<p>
Creates a symbol in the output object file.
</p>
<h3 id="builtin-tagName">@tagName</h3> <h3 id="builtin-tagName">@tagName</h3>
<pre><code class="zig">@tagName(value: var) -&gt; []const u8</code></pre> <pre><code class="zig">@tagName(value: var) -&gt; []const u8</code></pre>
<p> <p>

View File

@ -5,13 +5,9 @@ const c = @cImport({
@cInclude("string.h"); @cInclude("string.h");
}); });
comptime { const msg = c"Hello, world!\n";
@export("main", main);
}
extern fn main(argc: c_int, argv: &&u8) -> c_int {
const msg = c"Hello, world!\n";
export fn main(argc: c_int, argv: &&u8) -> c_int {
if (c.printf(msg) != c_int(c.strlen(msg))) if (c.printf(msg) != c_int(c.strlen(msg)))
return -1; return -1;

View File

@ -1,9 +1,6 @@
const base64 = @import("std").base64; const base64 = @import("std").base64;
comptime { export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
@export("decode_base_64", decode_base_64);
}
extern fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
const src = source_ptr[0..source_len]; const src = source_ptr[0..source_len];
const dest = dest_ptr[0..dest_len]; const dest = dest_ptr[0..dest_len];
const base64_decoder = base64.standard_decoder_unsafe; const base64_decoder = base64.standard_decoder_unsafe;

View File

@ -1,6 +1,3 @@
comptime { export fn add(a: i32, b: i32) -> i32 {
@export("add", add);
}
extern fn add(a: i32, b: i32) -> i32 {
a + b a + b
} }

View File

@ -1285,7 +1285,6 @@ enum BuiltinFnId {
BuiltinFnIdSetAlignStack, BuiltinFnIdSetAlignStack,
BuiltinFnIdArgType, BuiltinFnIdArgType,
BuiltinFnIdExport, BuiltinFnIdExport,
BuiltinFnIdExportWithLinkage,
}; };
struct BuiltinFnEntry { struct BuiltinFnEntry {

View File

@ -111,6 +111,10 @@ static const char *extern_string(bool is_extern) {
return is_extern ? "extern " : ""; return is_extern ? "extern " : "";
} }
static const char *export_string(bool is_export) {
return is_export ? "export " : "";
}
//static const char *calling_convention_string(CallingConvention cc) { //static const char *calling_convention_string(CallingConvention cc) {
// switch (cc) { // switch (cc) {
// case CallingConventionUnspecified: return ""; // case CallingConventionUnspecified: return "";
@ -410,8 +414,9 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
{ {
const char *pub_str = visib_mod_string(node->data.fn_proto.visib_mod); const char *pub_str = visib_mod_string(node->data.fn_proto.visib_mod);
const char *extern_str = extern_string(node->data.fn_proto.is_extern); const char *extern_str = extern_string(node->data.fn_proto.is_extern);
const char *export_str = export_string(node->data.fn_proto.is_export);
const char *inline_str = inline_string(node->data.fn_proto.is_inline); const char *inline_str = inline_string(node->data.fn_proto.is_inline);
fprintf(ar->f, "%s%s%sfn", pub_str, inline_str, extern_str); fprintf(ar->f, "%s%s%s%sfn", pub_str, inline_str, export_str, extern_str);
if (node->data.fn_proto.name != nullptr) { if (node->data.fn_proto.name != nullptr) {
fprintf(ar->f, " "); fprintf(ar->f, " ");
print_symbol(ar, node->data.fn_proto.name); print_symbol(ar, node->data.fn_proto.name);

View File

@ -5016,8 +5016,7 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdOpaqueType, "OpaqueType", 0); create_builtin_fn(g, BuiltinFnIdOpaqueType, "OpaqueType", 0);
create_builtin_fn(g, BuiltinFnIdSetAlignStack, "setAlignStack", 1); create_builtin_fn(g, BuiltinFnIdSetAlignStack, "setAlignStack", 1);
create_builtin_fn(g, BuiltinFnIdArgType, "ArgType", 2); create_builtin_fn(g, BuiltinFnIdArgType, "ArgType", 2);
create_builtin_fn(g, BuiltinFnIdExport, "export", 2); create_builtin_fn(g, BuiltinFnIdExport, "export", 3);
create_builtin_fn(g, BuiltinFnIdExportWithLinkage, "exportWithLinkage", 3);
} }
static const char *bool_to_str(bool b) { static const char *bool_to_str(bool b) {

View File

@ -4739,7 +4739,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
return ir_build_arg_type(irb, scope, node, arg0_value, arg1_value); return ir_build_arg_type(irb, scope, node, arg0_value, arg1_value);
} }
case BuiltinFnIdExport: case BuiltinFnIdExport:
case BuiltinFnIdExportWithLinkage:
{ {
AstNode *arg0_node = node->data.fn_call_expr.params.at(0); AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
@ -4751,15 +4750,10 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
if (arg1_value == irb->codegen->invalid_instruction) if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value; return arg1_value;
IrInstruction *arg2_value; AstNode *arg2_node = node->data.fn_call_expr.params.at(2);
if (builtin_fn->id == BuiltinFnIdExportWithLinkage) { IrInstruction *arg2_value = ir_gen_node(irb, arg2_node, scope);
AstNode *arg2_node = node->data.fn_call_expr.params.at(2); if (arg2_value == irb->codegen->invalid_instruction)
arg2_value = ir_gen_node(irb, arg2_node, scope); return arg2_value;
if (arg2_value == irb->codegen->invalid_instruction)
return arg2_value;
} else {
arg2_value = nullptr;
}
return ir_build_export(irb, scope, node, arg0_value, arg1_value, arg2_value); return ir_build_export(irb, scope, node, arg0_value, arg1_value, arg2_value);
} }

View File

@ -740,9 +740,21 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
return node; return node;
} else if (token->id == TokenIdAtSign) { } else if (token->id == TokenIdAtSign) {
*token_index += 1; *token_index += 1;
Token *name_tok = ast_eat_token(pc, token_index, TokenIdSymbol); Token *name_tok = &pc->tokens->at(*token_index);
Buf *name_buf;
if (name_tok->id == TokenIdKeywordExport) {
name_buf = buf_create_from_str("export");
*token_index += 1;
} else if (name_tok->id == TokenIdSymbol) {
name_buf = token_buf(name_tok);
*token_index += 1;
} else {
ast_expect_token(pc, name_tok, TokenIdSymbol);
zig_unreachable();
}
AstNode *name_node = ast_create_node(pc, NodeTypeSymbol, name_tok); AstNode *name_node = ast_create_node(pc, NodeTypeSymbol, name_tok);
name_node->data.symbol_expr.symbol = token_buf(name_tok); name_node->data.symbol_expr.symbol = name_buf;
AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, token); AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, token);
node->data.fn_call_expr.fn_ref_expr = name_node; node->data.fn_call_expr.fn_ref_expr = name_node;
@ -2254,12 +2266,22 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn);
cc = CallingConventionStdcall; cc = CallingConventionStdcall;
} else if (first_token->id == TokenIdKeywordExtern) { } else if (first_token->id == TokenIdKeywordExtern) {
is_extern = true;
*token_index += 1; *token_index += 1;
fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); Token *next_token = &pc->tokens->at(*token_index);
if (next_token->id == TokenIdKeywordFn) {
fn_token = next_token;
*token_index += 1;
} else if (mandatory) {
ast_expect_token(pc, next_token, TokenIdKeywordFn);
zig_unreachable();
} else {
*token_index -= 1;
return nullptr;
}
cc = CallingConventionC; cc = CallingConventionC;
} else if (first_token->id == TokenIdKeywordFn) { } else if (first_token->id == TokenIdKeywordFn) {
fn_token = first_token; fn_token = first_token;
is_extern = true;
*token_index += 1; *token_index += 1;
cc = CallingConventionUnspecified; cc = CallingConventionUnspecified;
} else if (mandatory) { } else if (mandatory) {

View File

@ -73,6 +73,7 @@ struct Context {
ImportTableEntry *import; ImportTableEntry *import;
ZigList<ErrorMsg *> *errors; ZigList<ErrorMsg *> *errors;
VisibMod visib_mod; VisibMod visib_mod;
bool want_export;
AstNode *root; AstNode *root;
HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table; HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table; HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
@ -3250,8 +3251,8 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
StorageClass sc = fn_decl->getStorageClass(); StorageClass sc = fn_decl->getStorageClass();
if (sc == SC_None) { if (sc == SC_None) {
// TODO add export decl
proto_node->data.fn_proto.visib_mod = c->visib_mod; proto_node->data.fn_proto.visib_mod = c->visib_mod;
proto_node->data.fn_proto.is_export = fn_decl->hasBody() ? c->want_export : false;
} else if (sc == SC_Extern || sc == SC_Static) { } else if (sc == SC_Extern || sc == SC_Static) {
proto_node->data.fn_proto.visib_mod = c->visib_mod; proto_node->data.fn_proto.visib_mod = c->visib_mod;
} else if (sc == SC_PrivateExtern) { } else if (sc == SC_PrivateExtern) {
@ -4274,8 +4275,10 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
c->errors = errors; c->errors = errors;
if (buf_ends_with_str(buf_create_from_str(target_file), ".h")) { if (buf_ends_with_str(buf_create_from_str(target_file), ".h")) {
c->visib_mod = VisibModPub; c->visib_mod = VisibModPub;
c->want_export = false;
} else { } else {
c->visib_mod = VisibModPub; c->visib_mod = VisibModPub;
c->want_export = true;
} }
c->decl_table.init(8); c->decl_table.init(8);
c->macro_table.init(8); c->macro_table.init(8);

View File

@ -8,12 +8,13 @@ const builtin = @import("builtin");
var argc_ptr: &usize = undefined; var argc_ptr: &usize = undefined;
comptime { comptime {
const strong_linkage = builtin.GlobalLinkage.Strong;
if (builtin.link_libc) { if (builtin.link_libc) {
@export("main", main); @export("main", main, strong_linkage);
} else if (builtin.os == builtin.Os.windows) { } else if (builtin.os == builtin.Os.windows) {
@export("WinMainCRTStartup", WinMainCRTStartup); @export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);
} else { } else {
@export("_start", _start); @export("_start", _start, strong_linkage);
} }
} }

View File

@ -13,24 +13,10 @@ pub coldcc fn panic(msg: []const u8) -> noreturn {
} }
} }
comptime {
@export("memset", memset);
@export("memcpy", memcpy);
@export("fmodf", fmodf);
@export("fmod", fmod);
@export("floorf", floorf);
@export("ceilf", ceilf);
@export("floor", floor);
@export("ceil", ceil);
if (builtin.mode != builtin.Mode.ReleaseFast and builtin.os != builtin.Os.windows) {
@export("__stack_chk_fail", __stack_chk_fail);
}
}
// Note that memset does not return `dest`, like the libc API. // Note that memset does not return `dest`, like the libc API.
// The semantics of memset is dictated by the corresponding // The semantics of memset is dictated by the corresponding
// LLVM intrinsics, not by the libc API. // LLVM intrinsics, not by the libc API.
extern fn memset(dest: ?&u8, c: u8, n: usize) { export fn memset(dest: ?&u8, c: u8, n: usize) {
@setDebugSafety(this, false); @setDebugSafety(this, false);
var index: usize = 0; var index: usize = 0;
@ -41,7 +27,7 @@ extern fn memset(dest: ?&u8, c: u8, n: usize) {
// Note that memcpy does not return `dest`, like the libc API. // Note that memcpy does not return `dest`, like the libc API.
// The semantics of memcpy is dictated by the corresponding // The semantics of memcpy is dictated by the corresponding
// LLVM intrinsics, not by the libc API. // LLVM intrinsics, not by the libc API.
extern fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) { export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {
@setDebugSafety(this, false); @setDebugSafety(this, false);
var index: usize = 0; var index: usize = 0;
@ -49,21 +35,26 @@ extern fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {
(??dest)[index] = (??src)[index]; (??dest)[index] = (??src)[index];
} }
comptime {
if (builtin.mode != builtin.Mode.ReleaseFast and builtin.os != builtin.Os.windows) {
@export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
}
}
extern fn __stack_chk_fail() -> noreturn { extern fn __stack_chk_fail() -> noreturn {
@panic("stack smashing detected"); @panic("stack smashing detected");
} }
const math = @import("../math/index.zig"); const math = @import("../math/index.zig");
extern fn fmodf(x: f32, y: f32) -> f32 { generic_fmod(f32, x, y) } export fn fmodf(x: f32, y: f32) -> f32 { generic_fmod(f32, x, y) }
extern fn fmod(x: f64, y: f64) -> f64 { generic_fmod(f64, x, y) } export fn fmod(x: f64, y: f64) -> f64 { generic_fmod(f64, x, y) }
// TODO add intrinsics for these (and probably the double version too) // TODO add intrinsics for these (and probably the double version too)
// and have the math stuff use the intrinsic. same as @mod and @rem // and have the math stuff use the intrinsic. same as @mod and @rem
extern fn floorf(x: f32) -> f32 { math.floor(x) } export fn floorf(x: f32) -> f32 { math.floor(x) }
extern fn ceilf(x: f32) -> f32 { math.ceil(x) } export fn ceilf(x: f32) -> f32 { math.ceil(x) }
extern fn floor(x: f64) -> f64 { math.floor(x) } export fn floor(x: f64) -> f64 { math.floor(x) }
extern fn ceil(x: f64) -> f64 { math.ceil(x) } export fn ceil(x: f64) -> f64 { math.ceil(x) }
fn generic_fmod(comptime T: type, x: T, y: T) -> T { fn generic_fmod(comptime T: type, x: T, y: T) -> T {
@setDebugSafety(this, false); @setDebugSafety(this, false);

View File

@ -5,62 +5,62 @@ comptime {
const linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Weak; const linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Weak;
const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong; const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong;
@exportWithLinkage("__letf2", @import("comparetf2.zig").__letf2, linkage); @export("__letf2", @import("comparetf2.zig").__letf2, linkage);
@exportWithLinkage("__getf2", @import("comparetf2.zig").__getf2, linkage); @export("__getf2", @import("comparetf2.zig").__getf2, linkage);
if (!is_test) { if (!is_test) {
// only create these aliases when not testing // only create these aliases when not testing
@exportWithLinkage("__cmptf2", @import("comparetf2.zig").__letf2, linkage); @export("__cmptf2", @import("comparetf2.zig").__letf2, linkage);
@exportWithLinkage("__eqtf2", @import("comparetf2.zig").__letf2, linkage); @export("__eqtf2", @import("comparetf2.zig").__letf2, linkage);
@exportWithLinkage("__lttf2", @import("comparetf2.zig").__letf2, linkage); @export("__lttf2", @import("comparetf2.zig").__letf2, linkage);
@exportWithLinkage("__netf2", @import("comparetf2.zig").__letf2, linkage); @export("__netf2", @import("comparetf2.zig").__letf2, linkage);
@exportWithLinkage("__gttf2", @import("comparetf2.zig").__getf2, linkage); @export("__gttf2", @import("comparetf2.zig").__getf2, linkage);
} }
@exportWithLinkage("__unordtf2", @import("comparetf2.zig").__unordtf2, linkage); @export("__unordtf2", @import("comparetf2.zig").__unordtf2, linkage);
@exportWithLinkage("__fixunssfsi", @import("fixunssfsi.zig").__fixunssfsi, linkage); @export("__fixunssfsi", @import("fixunssfsi.zig").__fixunssfsi, linkage);
@exportWithLinkage("__fixunssfdi", @import("fixunssfdi.zig").__fixunssfdi, linkage); @export("__fixunssfdi", @import("fixunssfdi.zig").__fixunssfdi, linkage);
@exportWithLinkage("__fixunssfti", @import("fixunssfti.zig").__fixunssfti, linkage); @export("__fixunssfti", @import("fixunssfti.zig").__fixunssfti, linkage);
@exportWithLinkage("__fixunsdfsi", @import("fixunsdfsi.zig").__fixunsdfsi, linkage); @export("__fixunsdfsi", @import("fixunsdfsi.zig").__fixunsdfsi, linkage);
@exportWithLinkage("__fixunsdfdi", @import("fixunsdfdi.zig").__fixunsdfdi, linkage); @export("__fixunsdfdi", @import("fixunsdfdi.zig").__fixunsdfdi, linkage);
@exportWithLinkage("__fixunsdfti", @import("fixunsdfti.zig").__fixunsdfti, linkage); @export("__fixunsdfti", @import("fixunsdfti.zig").__fixunsdfti, linkage);
@exportWithLinkage("__fixunstfsi", @import("fixunstfsi.zig").__fixunstfsi, linkage); @export("__fixunstfsi", @import("fixunstfsi.zig").__fixunstfsi, linkage);
@exportWithLinkage("__fixunstfdi", @import("fixunstfdi.zig").__fixunstfdi, linkage); @export("__fixunstfdi", @import("fixunstfdi.zig").__fixunstfdi, linkage);
@exportWithLinkage("__fixunstfti", @import("fixunstfti.zig").__fixunstfti, linkage); @export("__fixunstfti", @import("fixunstfti.zig").__fixunstfti, linkage);
@exportWithLinkage("__udivmoddi4", @import("udivmoddi4.zig").__udivmoddi4, linkage); @export("__udivmoddi4", @import("udivmoddi4.zig").__udivmoddi4, linkage);
@exportWithLinkage("__udivmodti4", @import("udivmodti4.zig").__udivmodti4, linkage); @export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4, linkage);
@exportWithLinkage("__udivti3", @import("udivti3.zig").__udivti3, linkage); @export("__udivti3", @import("udivti3.zig").__udivti3, linkage);
@exportWithLinkage("__umodti3", @import("umodti3.zig").__umodti3, linkage); @export("__umodti3", @import("umodti3.zig").__umodti3, linkage);
@exportWithLinkage("__udivsi3", __udivsi3, linkage); @export("__udivsi3", __udivsi3, linkage);
@exportWithLinkage("__udivdi3", __udivdi3, linkage); @export("__udivdi3", __udivdi3, linkage);
@exportWithLinkage("__umoddi3", __umoddi3, linkage); @export("__umoddi3", __umoddi3, linkage);
@exportWithLinkage("__udivmodsi4", __udivmodsi4, linkage); @export("__udivmodsi4", __udivmodsi4, linkage);
if (isArmArch()) { if (isArmArch()) {
@exportWithLinkage("__aeabi_uldivmod", __aeabi_uldivmod, linkage); @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage);
@exportWithLinkage("__aeabi_uidivmod", __aeabi_uidivmod, linkage); @export("__aeabi_uidivmod", __aeabi_uidivmod, linkage);
@exportWithLinkage("__aeabi_uidiv", __udivsi3, linkage); @export("__aeabi_uidiv", __udivsi3, linkage);
} }
if (builtin.os == builtin.Os.windows) { if (builtin.os == builtin.Os.windows) {
switch (builtin.arch) { switch (builtin.arch) {
builtin.Arch.i386 => { builtin.Arch.i386 => {
if (!builtin.link_libc) { if (!builtin.link_libc) {
@exportWithLinkage("_chkstk", _chkstk, strong_linkage); @export("_chkstk", _chkstk, strong_linkage);
@exportWithLinkage("__chkstk_ms", __chkstk_ms, linkage); @export("__chkstk_ms", __chkstk_ms, linkage);
} }
@exportWithLinkage("_aulldiv", @import("aulldiv.zig")._aulldiv, strong_linkage); @export("_aulldiv", @import("aulldiv.zig")._aulldiv, strong_linkage);
@exportWithLinkage("_aullrem", @import("aullrem.zig")._aullrem, strong_linkage); @export("_aullrem", @import("aullrem.zig")._aullrem, strong_linkage);
}, },
builtin.Arch.x86_64 => { builtin.Arch.x86_64 => {
if (!builtin.link_libc) { if (!builtin.link_libc) {
@exportWithLinkage("__chkstk", __chkstk, strong_linkage); @export("__chkstk", __chkstk, strong_linkage);
@exportWithLinkage("___chkstk_ms", ___chkstk_ms, linkage); @export("___chkstk_ms", ___chkstk_ms, linkage);
} }
}, },
else => {}, else => {},

View File

@ -2,24 +2,23 @@ const config = @import("builtin");
const assert = @import("std").debug.assert; const assert = @import("std").debug.assert;
comptime { comptime {
@export("derp", derp);
if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) { if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
asm volatile ( asm volatile (
\\.globl my_aoeu_symbol_asdf; \\.globl aoeu;
\\.type my_aoeu_symbol_asdf, @function; \\.type aoeu, @function;
\\.set my_aoeu_symbol_asdf, derp; \\.set aoeu, derp;
); );
} }
} }
test "module level assembly" { test "module level assembly" {
if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) { if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
assert(my_aoeu_symbol_asdf() == 1234); assert(aoeu() == 1234);
} }
} }
extern fn my_aoeu_symbol_asdf() -> i32; extern fn aoeu() -> i32;
extern fn derp() -> i32 { export fn derp() -> i32 {
return 1234; return 1234;
} }

View File

@ -13,8 +13,9 @@ test "empty function with comments" {
} }
comptime { comptime {
@exportWithLinkage("disabledExternFn", disabledExternFn, builtin.GlobalLinkage.Internal) @export("disabledExternFn", disabledExternFn, builtin.GlobalLinkage.Internal);
} }
extern fn disabledExternFn() { extern fn disabledExternFn() {
} }
@ -535,10 +536,7 @@ var global_ptr = &gdt[0];
// can't really run this test but we can make sure it has no compile error // can't really run this test but we can make sure it has no compile error
// and generates code // and generates code
const vram = @intToPtr(&volatile u8, 0x20000000)[0..0x8000]; const vram = @intToPtr(&volatile u8, 0x20000000)[0..0x8000];
comptime { export fn writeToVRam() {
@export("writeToVRam", writeToVRam);
}
extern fn writeToVRam() {
vram[0] = 'X'; vram[0] = 'X';
} }
@ -562,15 +560,3 @@ fn hereIsAnOpaqueType(ptr: &OpaqueA) -> &OpaqueA {
var a = ptr; var a = ptr;
return a; return a;
} }
test "function and variable in weird section" {
if (builtin.os == builtin.Os.linux or builtin.os == builtin.Os.windows) {
// macos can't handle this
assert(fnInWeirdSection() == 1234);
}
}
var varInWeirdSection: i32 section(".data2") = 1234;
fn fnInWeirdSection() section(".text2") -> i32 {
return varInWeirdSection;
}

View File

@ -4,8 +4,7 @@ const tests = @import("tests.zig");
pub fn addCases(cases: &tests.CompareOutputContext) { pub fn addCases(cases: &tests.CompareOutputContext) {
cases.addC("hello world with libc", cases.addC("hello world with libc",
\\const c = @cImport(@cInclude("stdio.h")); \\const c = @cImport(@cInclude("stdio.h"));
\\comptime { @export("main", main); } \\export fn main(argc: c_int, argv: &&u8) -> c_int {
\\extern fn main(argc: c_int, argv: &&u8) -> c_int {
\\ _ = c.puts(c"Hello, world!"); \\ _ = c.puts(c"Hello, world!");
\\ return 0; \\ return 0;
\\} \\}
@ -138,8 +137,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
\\ @cInclude("stdio.h"); \\ @cInclude("stdio.h");
\\}); \\});
\\ \\
\\comptime { @export("main", main); } \\export fn main(argc: c_int, argv: &&u8) -> c_int {
\\extern fn main(argc: c_int, argv: &&u8) -> c_int {
\\ if (is_windows) { \\ if (is_windows) {
\\ // we want actual \n, not \r\n \\ // we want actual \n, not \r\n
\\ _ = c._setmode(1, c._O_BINARY); \\ _ = c._setmode(1, c._O_BINARY);
@ -284,10 +282,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
cases.addC("expose function pointer to C land", cases.addC("expose function pointer to C land",
\\const c = @cImport(@cInclude("stdlib.h")); \\const c = @cImport(@cInclude("stdlib.h"));
\\ \\
\\comptime { \\export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
\\ @export("main", main);
\\}
\\extern fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
\\ const a_int = @ptrCast(&align(1) i32, a ?? unreachable); \\ const a_int = @ptrCast(&align(1) i32, a ?? unreachable);
\\ const b_int = @ptrCast(&align(1) i32, b ?? unreachable); \\ const b_int = @ptrCast(&align(1) i32, b ?? unreachable);
\\ if (*a_int < *b_int) { \\ if (*a_int < *b_int) {
@ -299,7 +294,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
\\ } \\ }
\\} \\}
\\ \\
\\extern fn main() -> c_int { \\export fn main() -> c_int {
\\ var array = []u32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 }; \\ var array = []u32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
\\ \\
\\ c.qsort(@ptrCast(&c_void, &array[0]), c_ulong(array.len), @sizeOf(i32), compare_fn); \\ c.qsort(@ptrCast(&c_void, &array[0]), c_ulong(array.len), @sizeOf(i32), compare_fn);
@ -327,8 +322,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
\\ @cInclude("stdio.h"); \\ @cInclude("stdio.h");
\\}); \\});
\\ \\
\\comptime { @export("main", main); } \\export fn main(argc: c_int, argv: &&u8) -> c_int {
\\extern fn main(argc: c_int, argv: &&u8) -> c_int {
\\ if (is_windows) { \\ if (is_windows) {
\\ // we want actual \n, not \r\n \\ // we want actual \n, not \r\n
\\ _ = c._setmode(1, c._O_BINARY); \\ _ = c._setmode(1, c._O_BINARY);

File diff suppressed because it is too large Load Diff

View File

@ -2,9 +2,6 @@ pub fn panic(msg: []const u8) -> noreturn { @breakpoint(); while (true) {} }
fn bar() -> %void {} fn bar() -> %void {}
comptime { export fn foo() {
@export("foo", foo);
}
extern fn foo() {
%%bar(); %%bar();
} }

View File

@ -26,7 +26,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return a < 0 ? -a : a; \\ return a < 0 ? -a : a;
\\} \\}
, ,
\\pub fn abs(a: c_int) -> c_int { \\export fn abs(a: c_int) -> c_int {
\\ return if (a < 0) -a else a; \\ return if (a < 0) -a else a;
\\} \\}
); );
@ -325,12 +325,12 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return a; \\ return a;
\\} \\}
, ,
\\pub fn foo1(_arg_a: c_uint) -> c_uint { \\pub export fn foo1(_arg_a: c_uint) -> c_uint {
\\ var a = _arg_a; \\ var a = _arg_a;
\\ a +%= 1; \\ a +%= 1;
\\ return a; \\ return a;
\\} \\}
\\pub fn foo2(_arg_a: c_int) -> c_int { \\pub export fn foo2(_arg_a: c_int) -> c_int {
\\ var a = _arg_a; \\ var a = _arg_a;
\\ a += 1; \\ a += 1;
\\ return a; \\ return a;
@ -346,7 +346,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return i; \\ return i;
\\} \\}
, ,
\\pub fn log2(_arg_a: c_uint) -> c_int { \\pub export fn log2(_arg_a: c_uint) -> c_int {
\\ var a = _arg_a; \\ var a = _arg_a;
\\ var i: c_int = 0; \\ var i: c_int = 0;
\\ while (a > c_uint(0)) { \\ while (a > c_uint(0)) {
@ -367,7 +367,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return a; \\ return a;
\\} \\}
, ,
\\pub fn max(a: c_int, b: c_int) -> c_int { \\pub export fn max(a: c_int, b: c_int) -> c_int {
\\ if (a < b) return b; \\ if (a < b) return b;
\\ if (a < b) return b else return a; \\ if (a < b) return b else return a;
\\} \\}
@ -382,7 +382,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return a; \\ return a;
\\} \\}
, ,
\\pub fn max(a: c_int, b: c_int) -> c_int { \\pub export fn max(a: c_int, b: c_int) -> c_int {
\\ if (a == b) return a; \\ if (a == b) return a;
\\ if (a != b) return b; \\ if (a != b) return b;
\\ return a; \\ return a;
@ -407,7 +407,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ c = a % b; \\ c = a % b;
\\} \\}
, ,
\\pub fn s(a: c_int, b: c_int) -> c_int { \\pub export fn s(a: c_int, b: c_int) -> c_int {
\\ var c: c_int; \\ var c: c_int;
\\ c = (a + b); \\ c = (a + b);
\\ c = (a - b); \\ c = (a - b);
@ -415,7 +415,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ c = @divTrunc(a, b); \\ c = @divTrunc(a, b);
\\ c = @rem(a, b); \\ c = @rem(a, b);
\\} \\}
\\pub fn u(a: c_uint, b: c_uint) -> c_uint { \\pub export fn u(a: c_uint, b: c_uint) -> c_uint {
\\ var c: c_uint; \\ var c: c_uint;
\\ c = (a +% b); \\ c = (a +% b);
\\ c = (a -% b); \\ c = (a -% b);
@ -430,7 +430,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return (a & b) ^ (a | b); \\ return (a & b) ^ (a | b);
\\} \\}
, ,
\\pub fn max(a: c_int, b: c_int) -> c_int { \\pub export fn max(a: c_int, b: c_int) -> c_int {
\\ return (a & b) ^ (a | b); \\ return (a & b) ^ (a | b);
\\} \\}
); );
@ -444,7 +444,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return a; \\ return a;
\\} \\}
, ,
\\pub fn max(a: c_int, b: c_int) -> c_int { \\pub export fn max(a: c_int, b: c_int) -> c_int {
\\ if ((a < b) or (a == b)) return b; \\ if ((a < b) or (a == b)) return b;
\\ if ((a >= b) and (a == b)) return a; \\ if ((a >= b) and (a == b)) return a;
\\ return a; \\ return a;
@ -458,7 +458,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ a = tmp; \\ a = tmp;
\\} \\}
, ,
\\pub fn max(_arg_a: c_int) -> c_int { \\pub export fn max(_arg_a: c_int) -> c_int {
\\ var a = _arg_a; \\ var a = _arg_a;
\\ var tmp: c_int; \\ var tmp: c_int;
\\ tmp = a; \\ tmp = a;
@ -472,7 +472,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ c = b = a; \\ c = b = a;
\\} \\}
, ,
\\pub fn max(a: c_int) { \\pub export fn max(a: c_int) {
\\ var b: c_int; \\ var b: c_int;
\\ var c: c_int; \\ var c: c_int;
\\ c = { \\ c = {
@ -493,7 +493,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return i; \\ return i;
\\} \\}
, ,
\\pub fn log2(_arg_a: u32) -> c_int { \\pub export fn log2(_arg_a: u32) -> c_int {
\\ var a = _arg_a; \\ var a = _arg_a;
\\ var i: c_int = 0; \\ var i: c_int = 0;
\\ while (a > c_uint(0)) { \\ while (a > c_uint(0)) {
@ -518,7 +518,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\void foo(void) { bar(); } \\void foo(void) { bar(); }
, ,
\\pub fn bar() {} \\pub fn bar() {}
\\pub fn foo() { \\pub export fn foo() {
\\ bar(); \\ bar();
\\} \\}
); );
@ -534,7 +534,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\pub const struct_Foo = extern struct { \\pub const struct_Foo = extern struct {
\\ field: c_int, \\ field: c_int,
\\}; \\};
\\pub fn read_field(foo: ?&struct_Foo) -> c_int { \\pub export fn read_field(foo: ?&struct_Foo) -> c_int {
\\ return (??foo).field; \\ return (??foo).field;
\\} \\}
); );
@ -544,7 +544,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ ;;;;; \\ ;;;;;
\\} \\}
, ,
\\pub fn foo() {} \\pub export fn foo() {}
); );
cases.add("undefined array global", cases.add("undefined array global",
@ -560,7 +560,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\} \\}
, ,
\\pub var array: [100]c_int = undefined; \\pub var array: [100]c_int = undefined;
\\pub fn foo(index: c_int) -> c_int { \\pub export fn foo(index: c_int) -> c_int {
\\ return array[index]; \\ return array[index];
\\} \\}
); );
@ -571,7 +571,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return (int)a; \\ return (int)a;
\\} \\}
, ,
\\pub fn float_to_int(a: f32) -> c_int { \\pub export fn float_to_int(a: f32) -> c_int {
\\ return c_int(a); \\ return c_int(a);
\\} \\}
); );
@ -581,7 +581,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return x; \\ return x;
\\} \\}
, ,
\\pub fn foo(x: ?&c_ushort) -> ?&c_void { \\pub export fn foo(x: ?&c_ushort) -> ?&c_void {
\\ return @ptrCast(?&c_void, x); \\ return @ptrCast(?&c_void, x);
\\} \\}
); );
@ -592,7 +592,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return sizeof(int); \\ return sizeof(int);
\\} \\}
, ,
\\pub fn size_of() -> usize { \\pub export fn size_of() -> usize {
\\ return @sizeOf(c_int); \\ return @sizeOf(c_int);
\\} \\}
); );
@ -602,7 +602,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return 0; \\ return 0;
\\} \\}
, ,
\\pub fn foo() -> ?&c_int { \\pub export fn foo() -> ?&c_int {
\\ return null; \\ return null;
\\} \\}
); );
@ -612,7 +612,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return 1, 2; \\ return 1, 2;
\\} \\}
, ,
\\pub fn foo() -> c_int { \\pub export fn foo() -> c_int {
\\ return { \\ return {
\\ _ = 1; \\ _ = 1;
\\ 2 \\ 2
@ -625,7 +625,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return (1 << 2) >> 1; \\ return (1 << 2) >> 1;
\\} \\}
, ,
\\pub fn foo() -> c_int { \\pub export fn foo() -> c_int {
\\ return (1 << @import("std").math.Log2Int(c_int)(2)) >> @import("std").math.Log2Int(c_int)(1); \\ return (1 << @import("std").math.Log2Int(c_int)(2)) >> @import("std").math.Log2Int(c_int)(1);
\\} \\}
); );
@ -643,7 +643,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ a <<= (a <<= 1); \\ a <<= (a <<= 1);
\\} \\}
, ,
\\pub fn foo() { \\pub export fn foo() {
\\ var a: c_int = 0; \\ var a: c_int = 0;
\\ a += { \\ a += {
\\ const _ref = &a; \\ const _ref = &a;
@ -701,7 +701,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ a <<= (a <<= 1); \\ a <<= (a <<= 1);
\\} \\}
, ,
\\pub fn foo() { \\pub export fn foo() {
\\ var a: c_uint = c_uint(0); \\ var a: c_uint = c_uint(0);
\\ a +%= { \\ a +%= {
\\ const _ref = &a; \\ const _ref = &a;
@ -771,7 +771,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ u = u--; \\ u = u--;
\\} \\}
, ,
\\pub fn foo() { \\pub export fn foo() {
\\ var i: c_int = 0; \\ var i: c_int = 0;
\\ var u: c_uint = c_uint(0); \\ var u: c_uint = c_uint(0);
\\ i += 1; \\ i += 1;
@ -819,7 +819,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ u = --u; \\ u = --u;
\\} \\}
, ,
\\pub fn foo() { \\pub export fn foo() {
\\ var i: c_int = 0; \\ var i: c_int = 0;
\\ var u: c_uint = c_uint(0); \\ var u: c_uint = c_uint(0);
\\ i += 1; \\ i += 1;
@ -862,7 +862,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ while (b != 0); \\ while (b != 0);
\\} \\}
, ,
\\pub fn foo() { \\pub export fn foo() {
\\ var a: c_int = 2; \\ var a: c_int = 2;
\\ while (true) { \\ while (true) {
\\ a -= 1; \\ a -= 1;
@ -886,9 +886,9 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ baz(); \\ baz();
\\} \\}
, ,
\\pub fn foo() {} \\pub export fn foo() {}
\\pub fn baz() {} \\pub export fn baz() {}
\\pub fn bar() { \\pub export fn bar() {
\\ var f: ?extern fn() = foo; \\ var f: ?extern fn() = foo;
\\ (??f)(); \\ (??f)();
\\ (??f)(); \\ (??f)();
@ -901,7 +901,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ *x = 1; \\ *x = 1;
\\} \\}
, ,
\\pub fn foo(x: ?&c_int) { \\pub export fn foo(x: ?&c_int) {
\\ (*??x) = 1; \\ (*??x) = 1;
\\} \\}
); );