ref: ea50af981b8a0472e2e3588c31c51ae927098949
dir: /libstd/varargs.myr/
use "types.use" use "introspect.use" use "sleq.use" use "die.use" pkg std = type valist const vastart : (args : ...# -> valist) generic vanext : (ap : valist -> (@a, valist)) ;; 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] } generic vanext = {ap -> (@a, valist) 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: checked. Right now, too much is broken with named types. if !sleq(typeof(v), tcnext(&ap.tc)) std.die("wrong type from valist\n") ;; */ /* only move on after we read through the value */ ap.args = ((p castto(intptr)) + sizeof(@a)) castto(byte#) -> (v, ap) }