IR: implement setFnVisible builtin

master
Andrew Kelley 2016-11-21 15:36:25 -05:00
parent 052cd44588
commit a2257e4b81
6 changed files with 371 additions and 305 deletions

View File

@ -1448,6 +1448,7 @@ enum IrInstructionId {
IrInstructionIdToPtrType, IrInstructionIdToPtrType,
IrInstructionIdPtrTypeChild, IrInstructionIdPtrTypeChild,
IrInstructionIdSetFnTest, IrInstructionIdSetFnTest,
IrInstructionIdSetFnVisible,
IrInstructionIdSetDebugSafety, IrInstructionIdSetDebugSafety,
IrInstructionIdArrayType, IrInstructionIdArrayType,
IrInstructionIdSliceType, IrInstructionIdSliceType,
@ -1712,6 +1713,13 @@ struct IrInstructionSetFnTest {
IrInstruction *is_test; IrInstruction *is_test;
}; };
struct IrInstructionSetFnVisible {
IrInstruction base;
IrInstruction *fn_value;
IrInstruction *is_visible;
};
struct IrInstructionSetDebugSafety { struct IrInstructionSetDebugSafety {
IrInstruction base; IrInstruction base;

View File

@ -1649,6 +1649,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdPtrTypeChild: case IrInstructionIdPtrTypeChild:
case IrInstructionIdFieldPtr: case IrInstructionIdFieldPtr:
case IrInstructionIdSetFnTest: case IrInstructionIdSetFnTest:
case IrInstructionIdSetFnVisible:
case IrInstructionIdSetDebugSafety: case IrInstructionIdSetDebugSafety:
case IrInstructionIdArrayType: case IrInstructionIdArrayType:
case IrInstructionIdSliceType: case IrInstructionIdSliceType:

View File

@ -202,6 +202,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionSetFnTest *) {
return IrInstructionIdSetFnTest; return IrInstructionIdSetFnTest;
} }
static constexpr IrInstructionId ir_instruction_id(IrInstructionSetFnVisible *) {
return IrInstructionIdSetFnVisible;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionSetDebugSafety *) { static constexpr IrInstructionId ir_instruction_id(IrInstructionSetDebugSafety *) {
return IrInstructionIdSetDebugSafety; return IrInstructionIdSetDebugSafety;
} }
@ -808,6 +812,19 @@ static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, AstNode *source_node,
return &instruction->base; return &instruction->base;
} }
static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, AstNode *source_node, IrInstruction *fn_value,
IrInstruction *is_visible)
{
IrInstructionSetFnVisible *instruction = ir_build_instruction<IrInstructionSetFnVisible>(irb, source_node);
instruction->fn_value = fn_value;
instruction->is_visible = is_visible;
ir_ref_instruction(fn_value);
ir_ref_instruction(is_visible);
return &instruction->base;
}
static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, AstNode *source_node, static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, AstNode *source_node,
IrInstruction *scope_value, IrInstruction *debug_safety_on) IrInstruction *scope_value, IrInstruction *debug_safety_on)
{ {
@ -1432,6 +1449,20 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
return ir_build_set_fn_test(irb, node, arg0_value, arg1_value); return ir_build_set_fn_test(irb, node, arg0_value, arg1_value);
} }
case BuiltinFnIdSetFnVisible:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->block_context);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, node->block_context);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
return ir_build_set_fn_visible(irb, node, arg0_value, arg1_value);
}
case BuiltinFnIdSetDebugSafety: case BuiltinFnIdSetDebugSafety:
{ {
AstNode *arg0_node = node->data.fn_call_expr.params.at(0); AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
@ -1509,7 +1540,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
case BuiltinFnIdDivExact: case BuiltinFnIdDivExact:
case BuiltinFnIdTruncate: case BuiltinFnIdTruncate:
case BuiltinFnIdIntType: case BuiltinFnIdIntType:
case BuiltinFnIdSetFnVisible:
case BuiltinFnIdSetFnStaticEval: case BuiltinFnIdSetFnStaticEval:
case BuiltinFnIdSetFnNoInline: case BuiltinFnIdSetFnNoInline:
zig_panic("TODO IR gen more builtin functions"); zig_panic("TODO IR gen more builtin functions");
@ -4394,6 +4424,43 @@ static TypeTableEntry *ir_analyze_instruction_set_fn_test(IrAnalyze *ira,
return ira->codegen->builtin_types.entry_void; return ira->codegen->builtin_types.entry_void;
} }
static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira,
IrInstructionSetFnVisible *set_fn_visible_instruction)
{
IrInstruction *fn_value = set_fn_visible_instruction->fn_value->other;
IrInstruction *is_visible_value = set_fn_visible_instruction->is_visible->other;
FnTableEntry *fn_entry = ir_resolve_fn(ira, fn_value);
if (!fn_entry)
return ira->codegen->builtin_types.entry_invalid;
bool want_export;
if (!ir_resolve_bool(ira, is_visible_value, &want_export))
return ira->codegen->builtin_types.entry_invalid;
AstNode *source_node = set_fn_visible_instruction->base.source_node;
if (fn_entry->fn_export_set_node) {
ErrorMsg *msg = add_node_error(ira->codegen, source_node,
buf_sprintf("function visibility set twice"));
add_error_note(ira->codegen, msg, fn_entry->fn_export_set_node, buf_sprintf("first set here"));
return ira->codegen->builtin_types.entry_invalid;
}
fn_entry->fn_export_set_node = source_node;
AstNodeFnProto *fn_proto = &fn_entry->proto_node->data.fn_proto;
if (fn_proto->top_level_decl.visib_mod != VisibModExport) {
ErrorMsg *msg = add_node_error(ira->codegen, source_node,
buf_sprintf("function must be marked export to set function visibility"));
add_error_note(ira->codegen, msg, fn_entry->proto_node, buf_sprintf("function declared here"));
return ira->codegen->builtin_types.entry_invalid;
}
if (!want_export)
LLVMSetLinkage(fn_entry->fn_value, LLVMInternalLinkage);
ir_build_const_from(ira, &set_fn_visible_instruction->base, false);
return ira->codegen->builtin_types.entry_void;
}
static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira, static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
IrInstructionSetDebugSafety *set_debug_safety_instruction) IrInstructionSetDebugSafety *set_debug_safety_instruction)
{ {
@ -4847,6 +4914,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_ptr_type_child(ira, (IrInstructionPtrTypeChild *)instruction); return ir_analyze_instruction_ptr_type_child(ira, (IrInstructionPtrTypeChild *)instruction);
case IrInstructionIdSetFnTest: case IrInstructionIdSetFnTest:
return ir_analyze_instruction_set_fn_test(ira, (IrInstructionSetFnTest *)instruction); return ir_analyze_instruction_set_fn_test(ira, (IrInstructionSetFnTest *)instruction);
case IrInstructionIdSetFnVisible:
return ir_analyze_instruction_set_fn_visible(ira, (IrInstructionSetFnVisible *)instruction);
case IrInstructionIdSetDebugSafety: case IrInstructionIdSetDebugSafety:
return ir_analyze_instruction_set_debug_safety(ira, (IrInstructionSetDebugSafety *)instruction); return ir_analyze_instruction_set_debug_safety(ira, (IrInstructionSetDebugSafety *)instruction);
case IrInstructionIdSliceType: case IrInstructionIdSliceType:
@ -4957,6 +5026,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdReturn: case IrInstructionIdReturn:
case IrInstructionIdUnreachable: case IrInstructionIdUnreachable:
case IrInstructionIdSetFnTest: case IrInstructionIdSetFnTest:
case IrInstructionIdSetFnVisible:
case IrInstructionIdSetDebugSafety: case IrInstructionIdSetDebugSafety:
return true; return true;
case IrInstructionIdPhi: case IrInstructionIdPhi:
@ -5523,44 +5593,6 @@ IrInstruction *ir_exec_const_result(IrExecutable *exec) {
// return g->builtin_types.entry_void; // return g->builtin_types.entry_void;
//} //}
// //
//static TypeTableEntry *analyze_set_fn_visible(CodeGen *g, ImportTableEntry *import,
// BlockContext *context, AstNode *node)
//{
// AstNode **fn_node = &node->data.fn_call_expr.params.at(0);
// AstNode **value_node = &node->data.fn_call_expr.params.at(1);
//
// FnTableEntry *fn_entry = resolve_const_expr_fn(g, import, context, fn_node);
// if (!fn_entry) {
// return g->builtin_types.entry_invalid;
// }
//
// bool want_export;
// bool ok = resolve_const_expr_bool(g, import, context, value_node, &want_export);
// if (!ok) {
// return g->builtin_types.entry_invalid;
// }
//
// if (fn_entry->fn_export_set_node) {
// ErrorMsg *msg = add_node_error(g, node, buf_sprintf("function visibility set twice"));
// add_error_note(g, msg, fn_entry->fn_export_set_node, buf_sprintf("first set here"));
// return g->builtin_types.entry_invalid;
// }
// fn_entry->fn_export_set_node = node;
//
// AstNodeFnProto *fn_proto = &fn_entry->proto_node->data.fn_proto;
// if (fn_proto->top_level_decl.visib_mod != VisibModExport) {
// ErrorMsg *msg = add_node_error(g, node,
// buf_sprintf("function must be marked export to set function visibility"));
// add_error_note(g, msg, fn_entry->proto_node, buf_sprintf("function declared here"));
// return g->builtin_types.entry_void;
// }
// if (!want_export) {
// fn_proto->top_level_decl.visib_mod = VisibModPub;
// }
//
// return g->builtin_types.entry_void;
//}
//
//static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context, //static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
// TypeTableEntry *expected_type, AstNode *node) // TypeTableEntry *expected_type, AstNode *node)
//{ //{
@ -5784,8 +5816,6 @@ IrInstruction *ir_exec_const_result(IrExecutable *exec) {
// return analyze_set_fn_no_inline(g, import, context, node); // return analyze_set_fn_no_inline(g, import, context, node);
// case BuiltinFnIdSetFnStaticEval: // case BuiltinFnIdSetFnStaticEval:
// return analyze_set_fn_static_eval(g, import, context, node); // return analyze_set_fn_static_eval(g, import, context, node);
// case BuiltinFnIdSetFnVisible:
// return analyze_set_fn_visible(g, import, context, node);
// } // }
// zig_unreachable(); // zig_unreachable();
//} //}
@ -8310,7 +8340,6 @@ static void analyze_goto_pass2(CodeGen *g, ImportTableEntry *import, AstNode *no
// case BuiltinFnIdUnreachable: // case BuiltinFnIdUnreachable:
// zig_panic("moved to ir render"); // zig_panic("moved to ir render");
// case BuiltinFnIdSetFnTest: // case BuiltinFnIdSetFnTest:
// case BuiltinFnIdSetFnVisible:
// case BuiltinFnIdSetFnStaticEval: // case BuiltinFnIdSetFnStaticEval:
// case BuiltinFnIdSetFnNoInline: // case BuiltinFnIdSetFnNoInline:
// case BuiltinFnIdSetDebugSafety: // case BuiltinFnIdSetDebugSafety:

View File

@ -425,6 +425,14 @@ static void ir_print_set_fn_test(IrPrint *irp, IrInstructionSetFnTest *instructi
fprintf(irp->f, ")"); fprintf(irp->f, ")");
} }
static void ir_print_set_fn_visible(IrPrint *irp, IrInstructionSetFnVisible *instruction) {
fprintf(irp->f, "@setFnVisible(");
ir_print_other_instruction(irp, instruction->fn_value);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->is_visible);
fprintf(irp->f, ")");
}
static void ir_print_set_debug_safety(IrPrint *irp, IrInstructionSetDebugSafety *instruction) { static void ir_print_set_debug_safety(IrPrint *irp, IrInstructionSetDebugSafety *instruction) {
fprintf(irp->f, "@setDebugSafety("); fprintf(irp->f, "@setDebugSafety(");
ir_print_other_instruction(irp, instruction->scope_value); ir_print_other_instruction(irp, instruction->scope_value);
@ -603,6 +611,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdSetFnTest: case IrInstructionIdSetFnTest:
ir_print_set_fn_test(irp, (IrInstructionSetFnTest *)instruction); ir_print_set_fn_test(irp, (IrInstructionSetFnTest *)instruction);
break; break;
case IrInstructionIdSetFnVisible:
ir_print_set_fn_visible(irp, (IrInstructionSetFnVisible *)instruction);
break;
case IrInstructionIdSetDebugSafety: case IrInstructionIdSetDebugSafety:
ir_print_set_debug_safety(irp, (IrInstructionSetDebugSafety *)instruction); ir_print_set_debug_safety(irp, (IrInstructionSetDebugSafety *)instruction);
break; break;

View File

@ -1,262 +1,263 @@
const CHAR_BIT = 8; // TODO
const du_int = u64; //const CHAR_BIT = 8;
const di_int = i64; //const du_int = u64;
const si_int = c_int; //const di_int = i64;
const su_int = c_uint; //const si_int = c_int;
//const su_int = c_uint;
const udwords = [2]su_int; //
const low = if (@compileVar("is_big_endian")) 1 else 0; //const udwords = [2]su_int;
const high = 1 - low; //const low = if (@compileVar("is_big_endian")) 1 else 0;
//const high = 1 - low;
export fn __udivdi3(a: du_int, b: du_int) -> du_int { //
@setDebugSafety(this, false); //export fn __udivdi3(a: du_int, b: du_int) -> du_int {
return __udivmoddi4(a, b, null); // @setDebugSafety(this, false);
} // return __udivmoddi4(a, b, null);
//}
fn du_int_to_udwords(x: du_int) -> udwords { //
@setDebugSafety(this, false); //fn du_int_to_udwords(x: du_int) -> udwords {
return *(&udwords)(&x); // @setDebugSafety(this, false);
} // return *(&udwords)(&x);
//}
export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int { //
@setDebugSafety(this, false); //export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
// @setDebugSafety(this, false);
const n_uword_bits = @sizeOf(su_int) * CHAR_BIT; //
const n_udword_bits = @sizeOf(du_int) * CHAR_BIT; // const n_uword_bits = @sizeOf(su_int) * CHAR_BIT;
var n = du_int_to_udwords(a); // const n_udword_bits = @sizeOf(du_int) * CHAR_BIT;
var d = du_int_to_udwords(b); // var n = du_int_to_udwords(a);
var q: udwords = undefined; // var d = du_int_to_udwords(b);
var r: udwords = undefined; // var q: udwords = undefined;
var sr: c_uint = undefined; // var r: udwords = undefined;
// special cases, X is unknown, K != 0 // var sr: c_uint = undefined;
if (n[high] == 0) { // // special cases, X is unknown, K != 0
if (d[high] == 0) { // if (n[high] == 0) {
// 0 X // if (d[high] == 0) {
// --- // // 0 X
// 0 X // // ---
if (const rem ?= maybe_rem) { // // 0 X
*rem = n[low] % d[low]; // if (const rem ?= maybe_rem) {
} // *rem = n[low] % d[low];
return n[low] / d[low]; // }
} // return n[low] / d[low];
// 0 X // }
// --- // // 0 X
// K X // // ---
if (const rem ?= maybe_rem) { // // K X
*rem = n[low]; // if (const rem ?= maybe_rem) {
} // *rem = n[low];
return 0; // }
} // return 0;
// n[high] != 0 // }
if (d[low] == 0) { // // n[high] != 0
if (d[high] == 0) { // if (d[low] == 0) {
// K X // if (d[high] == 0) {
// --- // // K X
// 0 0 // // ---
if (var rem ?= maybe_rem) { // // 0 0
*rem = n[high] % d[low]; // if (var rem ?= maybe_rem) {
} // *rem = n[high] % d[low];
return n[high] / d[low]; // }
} // return n[high] / d[low];
// d[high] != 0 // }
if (n[low] == 0) { // // d[high] != 0
// K 0 // if (n[low] == 0) {
// --- // // K 0
// K 0 // // ---
if (var rem ?= maybe_rem) { // // K 0
r[high] = n[high] % d[high]; // if (var rem ?= maybe_rem) {
r[low] = 0; // r[high] = n[high] % d[high];
*rem = *(&du_int)(&r[0]); // r[low] = 0;
} // *rem = *(&du_int)(&r[0]);
return n[high] / d[high]; // }
} // return n[high] / d[high];
// K K // }
// --- // // K K
// K 0 // // ---
// if d is a power of 2 // // K 0
if ((d[high] & (d[high] - 1)) == 0) { // // if d is a power of 2
if (var rem ?= maybe_rem) { // if ((d[high] & (d[high] - 1)) == 0) {
r[low] = n[low]; // if (var rem ?= maybe_rem) {
r[high] = n[high] & (d[high] - 1); // r[low] = n[low];
*rem = *(&du_int)(&r[0]); // r[high] = n[high] & (d[high] - 1);
} // *rem = *(&du_int)(&r[0]);
return n[high] >> @ctz(@typeOf(d[high]), d[high]); // }
} // return n[high] >> @ctz(@typeOf(d[high]), d[high]);
// K K // }
// --- // // K K
// K 0 // // ---
sr = @clz(su_int, d[high]) - @clz(su_int, n[high]); // // K 0
// 0 <= sr <= n_uword_bits - 2 or sr large // sr = @clz(su_int, d[high]) - @clz(su_int, n[high]);
if (sr > n_uword_bits - 2) { // // 0 <= sr <= n_uword_bits - 2 or sr large
if (var rem ?= maybe_rem) { // if (sr > n_uword_bits - 2) {
*rem = *(&du_int)(&n[0]); // if (var rem ?= maybe_rem) {
} // *rem = *(&du_int)(&n[0]);
return 0; // }
} // return 0;
sr += 1; // }
// 1 <= sr <= n_uword_bits - 1 // sr += 1;
// q.all = n.all << (n_udword_bits - sr); // // 1 <= sr <= n_uword_bits - 1
q[low] = 0; // // q.all = n.all << (n_udword_bits - sr);
q[high] = n[low] << (n_uword_bits - sr); // q[low] = 0;
// r.all = n.all >> sr; // q[high] = n[low] << (n_uword_bits - sr);
r[high] = n[high] >> sr; // // r.all = n.all >> sr;
r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); // r[high] = n[high] >> sr;
} else { // r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr);
// d[low] != 0 // } else {
if (d[high] == 0) { // // d[low] != 0
// K X // if (d[high] == 0) {
// --- // // K X
// 0 K // // ---
// if d is a power of 2 // // 0 K
if ((d[low] & (d[low] - 1)) == 0) { // // if d is a power of 2
if (var rem ?= maybe_rem) { // if ((d[low] & (d[low] - 1)) == 0) {
*rem = n[low] & (d[low] - 1); // if (var rem ?= maybe_rem) {
} // *rem = n[low] & (d[low] - 1);
if (d[low] == 1) { // }
return *(&du_int)(&n[0]); // if (d[low] == 1) {
} // return *(&du_int)(&n[0]);
sr = @ctz(@typeOf(d[low]), d[low]); // }
q[high] = n[high] >> sr; // sr = @ctz(@typeOf(d[low]), d[low]);
q[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); // q[high] = n[high] >> sr;
return *(&du_int)(&q[0]); // q[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr);
} // return *(&du_int)(&q[0]);
// K X // }
// --- // // K X
// 0 K // // ---
sr = 1 + n_uword_bits + @clz(su_int, d[low]) - @clz(su_int, n[high]); // // 0 K
// 2 <= sr <= n_udword_bits - 1 // sr = 1 + n_uword_bits + @clz(su_int, d[low]) - @clz(su_int, n[high]);
// q.all = n.all << (n_udword_bits - sr); // // 2 <= sr <= n_udword_bits - 1
// r.all = n.all >> sr; // // q.all = n.all << (n_udword_bits - sr);
if (sr == n_uword_bits) { // // r.all = n.all >> sr;
q[low] = 0; // if (sr == n_uword_bits) {
q[high] = n[low]; // q[low] = 0;
r[high] = 0; // q[high] = n[low];
r[low] = n[high]; // r[high] = 0;
} else if (sr < n_uword_bits) { // r[low] = n[high];
// 2 <= sr <= n_uword_bits - 1 // } else if (sr < n_uword_bits) {
q[low] = 0; // // 2 <= sr <= n_uword_bits - 1
q[high] = n[low] << (n_uword_bits - sr); // q[low] = 0;
r[high] = n[high] >> sr; // q[high] = n[low] << (n_uword_bits - sr);
r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); // r[high] = n[high] >> sr;
} else { // r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr);
// n_uword_bits + 1 <= sr <= n_udword_bits - 1 // } else {
q[low] = n[low] << (n_udword_bits - sr); // // n_uword_bits + 1 <= sr <= n_udword_bits - 1
q[high] = (n[high] << (n_udword_bits - sr)) | // q[low] = n[low] << (n_udword_bits - sr);
(n[low] >> (sr - n_uword_bits)); // q[high] = (n[high] << (n_udword_bits - sr)) |
r[high] = 0; // (n[low] >> (sr - n_uword_bits));
r[low] = n[high] >> (sr - n_uword_bits); // r[high] = 0;
} // r[low] = n[high] >> (sr - n_uword_bits);
} else { // }
// K X // } else {
// --- // // K X
// K K // // ---
sr = @clz(su_int, d[high]) - @clz(su_int, n[high]); // // K K
// 0 <= sr <= n_uword_bits - 1 or sr large // sr = @clz(su_int, d[high]) - @clz(su_int, n[high]);
if (sr > n_uword_bits - 1) { // // 0 <= sr <= n_uword_bits - 1 or sr large
if (var rem ?= maybe_rem) { // if (sr > n_uword_bits - 1) {
*rem = *(&du_int)(&n[0]); // if (var rem ?= maybe_rem) {
} // *rem = *(&du_int)(&n[0]);
return 0; // }
} // return 0;
sr += 1; // }
// 1 <= sr <= n_uword_bits // sr += 1;
// q.all = n.all << (n_udword_bits - sr); // // 1 <= sr <= n_uword_bits
q[low] = 0; // // q.all = n.all << (n_udword_bits - sr);
if (sr == n_uword_bits) { // q[low] = 0;
q[high] = n[low]; // if (sr == n_uword_bits) {
r[high] = 0; // q[high] = n[low];
r[low] = n[high]; // r[high] = 0;
} else { // r[low] = n[high];
q[high] = n[low] << (n_uword_bits - sr); // } else {
r[high] = n[high] >> sr; // q[high] = n[low] << (n_uword_bits - sr);
r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); // r[high] = n[high] >> sr;
} // r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr);
} // }
} // }
// Not a special case // }
// q and r are initialized with: // // Not a special case
// q.all = n.all << (n_udword_bits - sr); // // q and r are initialized with:
// r.all = n.all >> sr; // // q.all = n.all << (n_udword_bits - sr);
// 1 <= sr <= n_udword_bits - 1 // // r.all = n.all >> sr;
var carry: su_int = 0; // // 1 <= sr <= n_udword_bits - 1
while (sr > 0) { // var carry: su_int = 0;
// r:q = ((r:q) << 1) | carry // while (sr > 0) {
r[high] = (r[high] << 1) | (r[low] >> (n_uword_bits - 1)); // // r:q = ((r:q) << 1) | carry
r[low] = (r[low] << 1) | (q[high] >> (n_uword_bits - 1)); // r[high] = (r[high] << 1) | (r[low] >> (n_uword_bits - 1));
q[high] = (q[high] << 1) | (q[low] >> (n_uword_bits - 1)); // r[low] = (r[low] << 1) | (q[high] >> (n_uword_bits - 1));
q[low] = (q[low] << 1) | carry; // q[high] = (q[high] << 1) | (q[low] >> (n_uword_bits - 1));
// carry = 0; // q[low] = (q[low] << 1) | carry;
// if (r.all >= d.all) // // carry = 0;
// { // // if (r.all >= d.all)
// r.all -= d.all; // // {
// carry = 1; // // r.all -= d.all;
// } // // carry = 1;
const s: di_int = (di_int)(*(&du_int)(&d[0]) - *(&du_int)(&r[0]) - 1) >> (n_udword_bits - 1); // // }
carry = su_int(s & 1); // const s: di_int = (di_int)(*(&du_int)(&d[0]) - *(&du_int)(&r[0]) - 1) >> (n_udword_bits - 1);
*(&du_int)(&r[0]) -= *(&du_int)(&d[0]) & u64(s); // carry = su_int(s & 1);
// *(&du_int)(&r[0]) -= *(&du_int)(&d[0]) & u64(s);
sr -= 1; //
} // sr -= 1;
*(&du_int)(&q[0]) = (*(&du_int)(&q[0]) << 1) | u64(carry); // }
if (var rem ?= maybe_rem) { // *(&du_int)(&q[0]) = (*(&du_int)(&q[0]) << 1) | u64(carry);
*rem = *(&du_int)(&r[0]); // if (var rem ?= maybe_rem) {
} // *rem = *(&du_int)(&r[0]);
return *(&du_int)(&q[0]); // }
} // return *(&du_int)(&q[0]);
//}
export fn __umoddi3(a: du_int, b: du_int) -> du_int { //
@setDebugSafety(this, false); //export fn __umoddi3(a: du_int, b: du_int) -> du_int {
// @setDebugSafety(this, false);
var r: du_int = undefined; //
__udivmoddi4(a, b, &r); // var r: du_int = undefined;
return r; // __udivmoddi4(a, b, &r);
} // return r;
//}
fn test_umoddi3() { //
@setFnTest(this, true); //fn test_umoddi3() {
// @setFnTest(this, true);
test_one_umoddi3(0, 1, 0); //
test_one_umoddi3(2, 1, 0); // test_one_umoddi3(0, 1, 0);
test_one_umoddi3(0x8000000000000000, 1, 0x0); // test_one_umoddi3(2, 1, 0);
test_one_umoddi3(0x8000000000000000, 2, 0x0); // test_one_umoddi3(0x8000000000000000, 1, 0x0);
test_one_umoddi3(0xFFFFFFFFFFFFFFFF, 2, 0x1); // test_one_umoddi3(0x8000000000000000, 2, 0x0);
} // test_one_umoddi3(0xFFFFFFFFFFFFFFFF, 2, 0x1);
//}
fn test_one_umoddi3(a: du_int, b: du_int, expected_r: du_int) { //
const r = __umoddi3(a, b); //fn test_one_umoddi3(a: du_int, b: du_int, expected_r: du_int) {
assert(r == expected_r); // const r = __umoddi3(a, b);
} // assert(r == expected_r);
//}
fn test_udivmoddi4() { //
@setFnTest(this, true); //fn test_udivmoddi4() {
// @setFnTest(this, true);
const cases = [][4]du_int { //
[]du_int{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000}, // const cases = [][4]du_int {
[]du_int{0x0000000080000000, 0x0000000100000001, 0x0000000000000000, 0x0000000080000000}, // []du_int{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000},
[]du_int{0x7FFFFFFF00000001, 0x0000000000000001, 0x7FFFFFFF00000001, 0x0000000000000000}, // []du_int{0x0000000080000000, 0x0000000100000001, 0x0000000000000000, 0x0000000080000000},
[]du_int{0x7FFFFFFF7FFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0x7FFFFFFF7FFFFFFF}, // []du_int{0x7FFFFFFF00000001, 0x0000000000000001, 0x7FFFFFFF00000001, 0x0000000000000000},
[]du_int{0x8000000000000002, 0xFFFFFFFFFFFFFFFE, 0x0000000000000000, 0x8000000000000002}, // []du_int{0x7FFFFFFF7FFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0x7FFFFFFF7FFFFFFF},
[]du_int{0x80000000FFFFFFFD, 0xFFFFFFFFFFFFFFFD, 0x0000000000000000, 0x80000000FFFFFFFD}, // []du_int{0x8000000000000002, 0xFFFFFFFFFFFFFFFE, 0x0000000000000000, 0x8000000000000002},
[]du_int{0xFFFFFFFD00000010, 0xFFFFFFFF80000000, 0x0000000000000000, 0xFFFFFFFD00000010}, // []du_int{0x80000000FFFFFFFD, 0xFFFFFFFFFFFFFFFD, 0x0000000000000000, 0x80000000FFFFFFFD},
[]du_int{0xFFFFFFFDFFFFFFFF, 0xFFFFFFFF7FFFFFFF, 0x0000000000000000, 0xFFFFFFFDFFFFFFFF}, // []du_int{0xFFFFFFFD00000010, 0xFFFFFFFF80000000, 0x0000000000000000, 0xFFFFFFFD00000010},
[]du_int{0xFFFFFFFE0747AE14, 0xFFFFFFFF0747AE14, 0x0000000000000000, 0xFFFFFFFE0747AE14}, // []du_int{0xFFFFFFFDFFFFFFFF, 0xFFFFFFFF7FFFFFFF, 0x0000000000000000, 0xFFFFFFFDFFFFFFFF},
[]du_int{0xFFFFFFFF00000001, 0xFFFFFFFF078644FA, 0x0000000000000000, 0xFFFFFFFF00000001}, // []du_int{0xFFFFFFFE0747AE14, 0xFFFFFFFF0747AE14, 0x0000000000000000, 0xFFFFFFFE0747AE14},
[]du_int{0xFFFFFFFF80000000, 0xFFFFFFFF00000010, 0x0000000000000001, 0x000000007FFFFFF0}, // []du_int{0xFFFFFFFF00000001, 0xFFFFFFFF078644FA, 0x0000000000000000, 0xFFFFFFFF00000001},
[]du_int{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000001, 0x0000000000000000}, // []du_int{0xFFFFFFFF80000000, 0xFFFFFFFF00000010, 0x0000000000000001, 0x000000007FFFFFF0},
}; // []du_int{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000001, 0x0000000000000000},
// };
for (cases) |case| { //
test_one_udivmoddi4(case[0], case[1], case[2], case[3]); // for (cases) |case| {
} // test_one_udivmoddi4(case[0], case[1], case[2], case[3]);
} // }
//}
fn test_one_udivmoddi4(a: du_int, b: du_int, expected_q: du_int, expected_r: du_int) { //
var r: du_int = undefined; //fn test_one_udivmoddi4(a: du_int, b: du_int, expected_q: du_int, expected_r: du_int) {
const q = __udivmoddi4(a, b, &r); // var r: du_int = undefined;
assert(q == expected_q); // const q = __udivmoddi4(a, b, &r);
assert(r == expected_r); // assert(q == expected_q);
} // assert(r == expected_r);
//}
fn assert(b: bool) { //
if (!b) @unreachable(); //fn assert(b: bool) {
} // if (!b) @unreachable();
//}

View File

@ -1,13 +1,29 @@
pub const SYS_write = 1; pub const SYS_write = 1;
pub const SYS_exit = 60; pub const SYS_exit = 60;
pub const stdout_fileno = 1; pub const stdout_fileno = 1;
const text = "hello\n";
// normal comment
/// this is a documentation comment
/// doc comment line 2
fn emptyFunctionWithComments() {
}
export fn disabledExternFn() {
@setFnVisible(this, false);
}
fn runAllTests() {
emptyFunctionWithComments();
disabledExternFn();
}
export nakedcc fn _start() -> unreachable { export nakedcc fn _start() -> unreachable {
myMain(); myMain();
} }
fn myMain() -> unreachable { fn myMain() -> unreachable {
runAllTests();
const text = "OK\n";
write(stdout_fileno, &text[0], text.len); write(stdout_fileno, &text[0], text.len);
exit(0); exit(0);
} }