// Non-blocking receive a value from a channel, used for two-case select
// statement with a default case.
-DEF_GO_RUNTIME(SELECTNBRECV, "runtime.selectnbrecv", P2(POINTER, CHAN), R1(BOOL))
-
-// Non-blocking tuple receive from a channel, used for two-case select
-// statement with a default case.
-DEF_GO_RUNTIME(SELECTNBRECV2, "runtime.selectnbrecv2", P3(POINTER, POINTER, CHAN),
- R1(BOOL))
+DEF_GO_RUNTIME(SELECTNBRECV, "runtime.selectnbrecv", P2(POINTER, CHAN),
+ R2(BOOL, BOOL))
// Block execution. Used for zero-case select.
DEF_GO_RUNTIME(BLOCK, "runtime.block", P0(), R0())
Expression* chanref = Expression::make_temporary_reference(chantmp, loc);
Block* bchan;
- Expression* call;
+ Expression* cond;
if (chancase.is_send())
{
// if selectnbsend(chan, &val) { body } else { default body }
Expression* ref = Expression::make_temporary_reference(ts, loc);
Expression* addr = Expression::make_unary(OPERATOR_AND, ref, loc);
- call = Runtime::make_call(Runtime::SELECTNBSEND, loc, 2, chanref, addr);
+ cond = Runtime::make_call(Runtime::SELECTNBSEND, loc, 2, chanref, addr);
bchan = chancase.statements();
}
else
Expression* ref = Expression::make_temporary_reference(ts, loc);
Expression* addr = Expression::make_unary(OPERATOR_AND, ref, loc);
- Expression* okref = NULL;
- if (chancase.closed() == NULL && chancase.closedvar() == NULL)
- {
- // Simple receive.
- // if selectnbrecv(&lhs, chan) { body } else { default body }
- call = Runtime::make_call(Runtime::SELECTNBRECV, loc, 2, addr, chanref);
- }
- else
- {
- // Tuple receive.
- // if selectnbrecv2(&lhs, &ok, chan) { body } else { default body }
-
- Type* booltype = Type::make_boolean_type();
- Temporary_statement* okts = Statement::make_temporary(booltype, NULL,
- loc);
- b->add_statement(okts);
-
- okref = Expression::make_temporary_reference(okts, loc);
- Expression* okaddr = Expression::make_unary(OPERATOR_AND, okref, loc);
- call = Runtime::make_call(Runtime::SELECTNBRECV2, loc, 3, addr, okaddr,
- chanref);
- }
+
+ // selected, ok = selectnbrecv(&lhs, chan)
+ Call_expression* call = Runtime::make_call(Runtime::SELECTNBRECV, loc, 2,
+ addr, chanref);
+
+ Temporary_statement* selected_temp =
+ Statement::make_temporary(Type::make_boolean_type(),
+ Expression::make_call_result(call, 0),
+ loc);
+ b->add_statement(selected_temp);
+
+ Temporary_statement* ok_temp =
+ Statement::make_temporary(Type::make_boolean_type(),
+ Expression::make_call_result(call, 1),
+ loc);
+ b->add_statement(ok_temp);
+
+ cond = Expression::make_temporary_reference(selected_temp, loc);
Location cloc = chancase.location();
bchan = new Block(b, loc);
if (chancase.val() != NULL && !chancase.val()->is_sink_expression())
{
- Statement* as = Statement::make_assignment(chancase.val(), ref->copy(),
+ Statement* as = Statement::make_assignment(chancase.val(),
+ ref->copy(),
cloc);
bchan->add_statement(as);
}
if (chancase.closed() != NULL && !chancase.closed()->is_sink_expression())
{
+ Expression* okref = Expression::make_temporary_reference(ok_temp,
+ cloc);
Statement* as = Statement::make_assignment(chancase.closed(),
- okref->copy(), cloc);
+ okref, cloc);
bchan->add_statement(as);
}
else if (chancase.closedvar() != NULL)
- chancase.closedvar()->var_value()->set_init(okref->copy());
+ {
+ Expression* okref = Expression::make_temporary_reference(ok_temp,
+ cloc);
+ chancase.closedvar()->var_value()->set_init(okref);
+ }
Statement* bs = Statement::make_block_statement(chancase.statements(),
cloc);
}
Statement* ifs =
- Statement::make_if_statement(call, bchan, defcase.statements(), loc);
+ Statement::make_if_statement(cond, bchan, defcase.statements(), loc);
b->add_statement(ifs);
Statement* label =
//go:linkname closechan
//go:linkname selectnbsend
//go:linkname selectnbrecv
-//go:linkname selectnbrecv2
const (
maxAlign = 8
return chansend(c, elem, false, getcallerpc())
}
-// compiler implements
-//
-// select {
-// case v = <-c:
-// ... foo
-// default:
-// ... bar
-// }
-//
-// as
-//
-// if selectnbrecv(&v, c) {
-// ... foo
-// } else {
-// ... bar
-// }
-//
-func selectnbrecv(elem unsafe.Pointer, c *hchan) (selected bool) {
- selected, _ = chanrecv(c, elem, false)
- return
-}
-
// compiler implements
//
// select {
//
// as
//
-// if c != nil && selectnbrecv2(&v, &ok, c) {
+// if selected, ok = selectnbrecv(&v, c); selected {
// ... foo
// } else {
// ... bar
// }
//
-func selectnbrecv2(elem unsafe.Pointer, received *bool, c *hchan) (selected bool) {
- // TODO(khr): just return 2 values from this function, now that it is in Go.
- selected, *received = chanrecv(c, elem, false)
- return
+func selectnbrecv(elem unsafe.Pointer, c *hchan) (selected, received bool) {
+ return chanrecv(c, elem, false)
}
//go:linkname reflect_chansend reflect.chansend