mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
fix parameter access of sret functions
This commit is contained in:
parent
419652ee8f
commit
f5cc7f65a3
@ -18,8 +18,7 @@ pub fn main(args: [][]u8) %void => {
|
|||||||
return error.GetRandomFail;
|
return error.GetRandomFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
var rand : Rand;
|
var rand = rand_new(seed);
|
||||||
rand.init(seed);
|
|
||||||
|
|
||||||
const answer = rand.range_u64(0, 100) + 1;
|
const answer = rand.range_u64(0, 100) + 1;
|
||||||
|
|
||||||
|
|||||||
@ -3955,7 +3955,6 @@ static void analyze_top_level_fn_def(CodeGen *g, ImportTableEntry *import, AstNo
|
|||||||
|
|
||||||
AstNodeFnProto *fn_proto = &fn_proto_node->data.fn_proto;
|
AstNodeFnProto *fn_proto = &fn_proto_node->data.fn_proto;
|
||||||
bool is_exported = (fn_proto->visib_mod == VisibModExport);
|
bool is_exported = (fn_proto->visib_mod == VisibModExport);
|
||||||
int gen_arg_index = 0;
|
|
||||||
for (int i = 0; i < fn_proto->params.length; i += 1) {
|
for (int i = 0; i < fn_proto->params.length; i += 1) {
|
||||||
AstNode *param_decl_node = fn_proto->params.at(i);
|
AstNode *param_decl_node = fn_proto->params.at(i);
|
||||||
assert(param_decl_node->type == NodeTypeParamDecl);
|
assert(param_decl_node->type == NodeTypeParamDecl);
|
||||||
@ -3978,12 +3977,7 @@ static void analyze_top_level_fn_def(CodeGen *g, ImportTableEntry *import, AstNo
|
|||||||
var->src_arg_index = i;
|
var->src_arg_index = i;
|
||||||
param_decl_node->data.param_decl.variable = var;
|
param_decl_node->data.param_decl.variable = var;
|
||||||
|
|
||||||
if (type->size_in_bits > 0) {
|
var->gen_arg_index = param_decl_node->data.param_decl.gen_index;
|
||||||
var->gen_arg_index = gen_arg_index;
|
|
||||||
gen_arg_index += 1;
|
|
||||||
} else {
|
|
||||||
var->gen_arg_index = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeTableEntry *expected_type = unwrapped_node_type(fn_proto->return_type);
|
TypeTableEntry *expected_type = unwrapped_node_type(fn_proto->return_type);
|
||||||
|
|||||||
@ -2319,6 +2319,10 @@ static void do_code_gen(CodeGen *g) {
|
|||||||
if (param_type->id == TypeTableEntryIdPointer && param_type->data.pointer.is_const) {
|
if (param_type->id == TypeTableEntryIdPointer && param_type->data.pointer.is_const) {
|
||||||
LLVMAddAttribute(argument_val, LLVMReadOnlyAttribute);
|
LLVMAddAttribute(argument_val, LLVMReadOnlyAttribute);
|
||||||
}
|
}
|
||||||
|
if (param_type->id == TypeTableEntryIdPointer) {
|
||||||
|
// when https://github.com/andrewrk/zig/issues/82 is fixed, add
|
||||||
|
// non null attribute here
|
||||||
|
}
|
||||||
if (param_node->data.param_decl.is_byval) {
|
if (param_node->data.param_decl.is_byval) {
|
||||||
LLVMAddAttribute(argument_val, LLVMByValAttribute);
|
LLVMAddAttribute(argument_val, LLVMByValAttribute);
|
||||||
}
|
}
|
||||||
|
|||||||
28
std/rand.zig
28
std/rand.zig
@ -6,20 +6,6 @@ pub struct Rand {
|
|||||||
array: [ARRAY_SIZE]u32,
|
array: [ARRAY_SIZE]u32,
|
||||||
index: isize,
|
index: isize,
|
||||||
|
|
||||||
/// Initialize random state with the given seed.
|
|
||||||
pub fn init(r: &Rand, seed: u32) => {
|
|
||||||
r.index = 0;
|
|
||||||
r.array[0] = seed;
|
|
||||||
var i : isize = 1;
|
|
||||||
var prev_value: u64 = seed;
|
|
||||||
while (i < ARRAY_SIZE) {
|
|
||||||
r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + u32(i));
|
|
||||||
prev_value = r.array[i];
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get 32 bits of randomness.
|
/// Get 32 bits of randomness.
|
||||||
pub fn get_u32(r: &Rand) u32 => {
|
pub fn get_u32(r: &Rand) u32 => {
|
||||||
if (r.index == 0) {
|
if (r.index == 0) {
|
||||||
@ -91,3 +77,17 @@ pub struct Rand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initialize random state with the given seed.
|
||||||
|
pub fn rand_new(seed: u32) Rand => {
|
||||||
|
var r: Rand;
|
||||||
|
r.index = 0;
|
||||||
|
r.array[0] = seed;
|
||||||
|
var i : isize = 1;
|
||||||
|
var prev_value: u64 = seed;
|
||||||
|
while (i < ARRAY_SIZE) {
|
||||||
|
r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + u32(i));
|
||||||
|
prev_value = r.array[i];
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|||||||
@ -1242,14 +1242,14 @@ struct Foo {
|
|||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
}
|
}
|
||||||
fn make_foo() Foo => {
|
fn make_foo(x: i32, y: i32) Foo => {
|
||||||
Foo {
|
Foo {
|
||||||
.x = 1234,
|
.x = x,
|
||||||
.y = 5678,
|
.y = y,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn main(args: [][]u8) %void => {
|
pub fn main(args: [][]u8) %void => {
|
||||||
const foo = make_foo();
|
const foo = make_foo(1234, 5678);
|
||||||
if (foo.y != 5678) {
|
if (foo.y != 5678) {
|
||||||
print_str("BAD\n");
|
print_str("BAD\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user