ref: ac965251351332f405c9e5390e00d436fa467ff4
dir: /libstd/varargs.myr/
use "types.use" use "introspect.use" use "sleq.use" use "die.use" pkg std = type valist const vastart : (args : ...# -> valist) const vatype : (ap : valist# -> byte[:]) const vaskip : (ap : valist# -> byte[:]) generic vanext : (ap : valist# -> @a) ;; type valist = struct args : byte# tc : typecursor ;; /* * a valist is really just a pointer to the varargs. * we assume that these sit on the stack nicely, * and don't need special handling to get to. * * This will be a problem when we switch to a * register based convention. We might want to * force varargs onto the stack regardless. */ const vastart = {args var tc, a, ip /* pull out the args. These are on the stacks like so: [ required ] [ args ] ---variadic--- [ typeinfo ] --> type description ------------ [ variadic ] [ args ] [ here ] &args points to the typeinfo, &args + sizeof(void#) points to the rest argument. */ tc = typeenc(args) ip = (args castto(intptr)) + sizeof(byte#) a = ip castto(byte#) -> [.args = a, .tc = tc] } const vatype = {ap -> tcpeek(&ap.tc) } generic vanext = {ap -> @a var v : @a var align var p /* Assumptions about the ABI: * all types smaller than a word are * aligned to their own size. Larger * types are aligned to word size. */ if sizeof(@a) > 8 align = 8 else align = sizeof(@a) ;; /* apply the alignment to the arg pointer */ p = ap.args castto(intptr) p = (p + align - 1) & ~(align - 1) ap.args = p castto(byte#) v = (ap.args castto(@a#))# /* TODO: check for type mismatch */ tcnext(&ap.tc) /* only move on after we read through the value */ ap.args = ((p castto(intptr)) + sizeof(@a)) castto(byte#) -> v }