fix parameter access of sret functions

This commit is contained in:
Andrew Kelley 2016-01-24 19:27:12 -07:00
parent 419652ee8f
commit f5cc7f65a3
5 changed files with 24 additions and 27 deletions

View File

@ -18,8 +18,7 @@ pub fn main(args: [][]u8) %void => {
return error.GetRandomFail;
}
var rand : Rand;
rand.init(seed);
var rand = rand_new(seed);
const answer = rand.range_u64(0, 100) + 1;

View File

@ -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;
bool is_exported = (fn_proto->visib_mod == VisibModExport);
int gen_arg_index = 0;
for (int i = 0; i < fn_proto->params.length; i += 1) {
AstNode *param_decl_node = fn_proto->params.at(i);
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;
param_decl_node->data.param_decl.variable = var;
if (type->size_in_bits > 0) {
var->gen_arg_index = gen_arg_index;
gen_arg_index += 1;
} else {
var->gen_arg_index = -1;
}
var->gen_arg_index = param_decl_node->data.param_decl.gen_index;
}
TypeTableEntry *expected_type = unwrapped_node_type(fn_proto->return_type);

View File

@ -2319,6 +2319,10 @@ static void do_code_gen(CodeGen *g) {
if (param_type->id == TypeTableEntryIdPointer && param_type->data.pointer.is_const) {
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) {
LLVMAddAttribute(argument_val, LLVMByValAttribute);
}

View File

@ -6,20 +6,6 @@ pub struct Rand {
array: [ARRAY_SIZE]u32,
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.
pub fn get_u32(r: &Rand) u32 => {
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;
}

View File

@ -1242,14 +1242,14 @@ struct Foo {
x: i32,
y: i32,
}
fn make_foo() Foo => {
fn make_foo(x: i32, y: i32) Foo => {
Foo {
.x = 1234,
.y = 5678,
.x = x,
.y = y,
}
}
pub fn main(args: [][]u8) %void => {
const foo = make_foo();
const foo = make_foo(1234, 5678);
if (foo.y != 5678) {
print_str("BAD\n");
}