dymokomi/luce-base
Source of release 0.45.0
0.45.0 / src / back / calls.lucb
#==============================================================================================
#
# calls - The calls both backends recognise in the checked tree
#
# DESCRIPTION:
# Shapes of call the checker leaves for the backends to recognise: the core `error`
# and `trap`, the receiver a formatted field's `display` writes, the `indexed()` a
# `for` walks by index, the argument a call gives each parameter, and the fields a
# record literal supplies, an atomic call's arguments and orderings, and an extern
# that binds one of the C library's float functions. The C backend and the IR
# lowering ask the same questions here, so they agree on the answers.
#
#==============================================================================================
import back.ir.ir
import back.isa
import back.names
import front.ast
import sema.types
## The C library float function extern `d` binds (`isa.float_function`) when it is declared
## with that function's C types: `f32` or `f64` throughout, one argument (three for `fma`),
## none `out`. None for any other declaration, which is called as declared.
pub func float_function(table: types.Table*, d: ast.Node*) -> isa.FloatFunction?:
if d.kind != ast.Kind.extern_func:
return none
let found = isa.float_function(names.c_symbol(d)) else return none
let want = types.f32_id if found.single else types.f64_id
let ft = table.get(d.type_id)
if ft.variadic or ft.elem != want or ft.args.length != (3 if found.op == ir.Op.fma else 1):
return none
for a in ft.args:
if a != want:
return none
var param = d.right
while let p = param:
if p.has(ast.flag_out):
return none
param = p.next
return found
## `error(...)`, the core function rather than a declaration of that name.
pub func is_error_call(e: ast.Node*) -> bool:
if e.kind != ast.Kind.call:
return false
let callee = e.left else return false
return callee.kind == ast.Kind.name and callee.text == "error" and callee.resolved == none
## A text literal, `"..."` or `r"..."`: text the program holds for its whole run.
pub func is_text_literal(e: ast.Node*) -> bool:
if e.kind != ast.Kind.literal:
return false
let b = e.text.bytes
return (b.length > 0 and b[0] == '"') or (b.length > 1 and b[0] == 'r' and b[1] == '"')
## `trap(...)`, the core function rather than a declaration of that name.
pub func is_trap_call(e: ast.Node*) -> bool:
if e.kind != ast.Kind.call:
return false
let callee = e.left else return false
return callee.kind == ast.Kind.name and callee.text == "trap" and callee.resolved == none
## The receiver of a `value.display(__sink)` call, the value being formatted.
pub func sink_receiver(v: ast.Node*) -> ast.Node*?:
if v.kind != ast.Kind.call:
return none
let callee = v.left else return none
if callee.kind != ast.Kind.member:
return none
return callee.left
## The receiver of `items.indexed()`, when `e` is that call of the method the compiler
## supplies: a `for` over it walks `items` by index (ยง8.3).
pub func indexed_receiver(e: ast.Node*) -> ast.Node*?:
if e.kind != ast.Kind.call:
return none
let callee = e.left else return none
if callee.kind != ast.Kind.member or callee.text != "indexed" or callee.resolved != none:
return none
return callee.left
## The argument a call's `args` give parameter `p`, the `index`-th: the one named for it,
## else the `index`-th positional one; none when neither is given.
pub func given_argument(args: ast.Node*?, p: ast.Node*, index: usize) -> ast.Node*?:
var given: ast.Node*? = none
var position: usize = 0
var a = args
while let arg = a:
if arg.text.length > 0:
if arg.text == p.text:
given = arg
else:
if position == index:
given = arg
position += 1
a = arg.next
return given
## Whether the record literal `e` gives `field` a value.
pub func supplies(e: ast.Node*, field: ast.Node*) -> bool:
var argument = e.body
while let given = argument:
if given.resolved == field:
return true
argument = given.next
return false
## The values of a call's first `out.length` arguments, in order, the rest of `out` none;
## returns how many arguments the call has.
pub func argument_values(e: ast.Node*, out: ast.Node*?[]) -> usize:
for i in 0..<out.length:
out[i] = none
var count: usize = 0
var cursor = e.body
while let a = cursor:
if count < out.length:
out[count] = a.left
count += 1
cursor = a.next
return count
## An `atomic.Ordering` argument as C11 numbers it, `memory_order_relaxed` 0 to `seq_cst`
## 5, and 6 for a fence's `signal`; an ordering not given is `seq_cst`.
pub func ordering(given: ast.Node*?) -> u32:
let e = given else return 5
if e.text == "relaxed":
return 0
if e.text == "acquire":
return 2
if e.text == "release":
return 3
if e.text == "acq_rel":
return 4
if e.text == "signal":
return 6
return 5