use 'debug/printf' for Singeli printf

This commit is contained in:
dzaima 2023-01-17 01:01:13 +02:00
parent 10c98685ab
commit 5de565f1b0
3 changed files with 10 additions and 72 deletions

@ -1 +1 @@
Subproject commit 69f494a4ab491cc52522f64516f27b71b18bc85d
Subproject commit 4c05f5ba96b3f83f0f1c10f1d4ee048ba55a13de

View File

@ -161,9 +161,14 @@ def forNZ{vars,begin,end,block} = {
}
def tree_fold{F, x} = {
def h = tuplen{x}/2
F{tree_fold{F, slice{x,0,h}}, tree_fold{F, slice{x,h,h*2}}}
def h = tuplen{x}>>1
F{tree_fold{F, slice{x,0,h}}, tree_fold{F, slice{x,h,tuplen{x}}}}
}
def tree_fold{F, x & tuplen{x}==1} = tupsel{0,x}
def eachx{F, ...args} = {
def l = tree_fold{max, each{{x} => {if(ktup{x}) tuplen{x}; else 0}, args}}
each{F, ...each{{x} => {if (istup{x}) x; else l**x}, args}}
}
def makeregs{v, n} = @collect(n) { reg:=v }

View File

@ -1,71 +1,4 @@
include 'debug/printf' # printf & lprintf
def assert{x:u1} = { if (not x) emit{void, '__builtin_trap'} }
def test_assert = assert # test_assert is guaranteed to either not exist, or always trap on bad input
def printf{...vs0} = {
def nsym = ~each{ksym, vs0}
def vs = join{each{{a,b}=>{
if (b) tup{' ', a}
else tup{a}
}, vs0, nsym & shiftright{tup{0}, nsym}}}
def listfmt{hex, s, e, vs} = {
if (tuplen{vs}==0) {
tup{merge{s, e}, tup{}}
} else {
def f = each{{v} => runfmt{hex, v}, vs}
def f2 = each{{f1, i} => {
if (i+1 == tuplen{f}) f1
else merge{f1, tup{tup{',%c', tup{32}}}}
}, f, iota{tuplen{f}}}
tup{
tup{s, tup{}},
...join{f2},
tup{e, tup{}}
}
}
}
def runfmt{hex, x} = {
if (ksym{x}) {
each{{c} => {
if (c==' ') tup{'%c', tup{32}}
else tup{c, tup{}}
}, symchars{x}}
} else if (ktup{x}) {
if (tuplen{x}==2 and match{tupsel{0,x}, 'x'}) runfmt{1, tupsel{1,x}}
else listfmt{hex, '{', '}', x}
} else if (kreg{x} or kcon{x}) {
def T = type{x}
if (match{typekind{T},'primitive'}) {
def q = quality{T}
def w = width{T}
def u = (w>1) & (q=='u')
def spec = {
if (q=='f') '%.18g'
else merge{tern{u & hex, '0x', ''}, '%', tern{w==64, 'l', ''}, tern{u, tern{hex, 'x', 'u'}, 'd'}}
}
tup{tup{spec, tup{x}}}
} else if (match{typekind{T},'vector'}) {
listfmt{hex, '[', ']', each{{i} => extract{x, i}, iota{vcount{T}}}}
} else {
tup{tup{'(unknown typekind)', tup{}}}
}
} else if (knum{x}) {
if ((x>>0) == x) {
if (x>=minvalue{i64} and x<=maxvalue{i64}) runfmt{0, i64~~x}
else if (x>=minvalue{u64} and x<=maxvalue{u64}) runfmt{0, u64~~x}
else runfmt{0, f64~~x}
} else runfmt{0, f64~~x}
} else {
tup{tup{'(unknown type)', tup{}}}
}
}
def is = join{each{{v} => runfmt{0,v}, vs}}
def e0s = join{each{{x}=>tupsel{0,x}, is}}
def e1s = join{each{{x}=>tupsel{1,x}, is}}
emit{void, 'printf', merge{'"', e0s, '"'}, ...e1s}
}
def lprintf{...vs} = printf{...vs, '\n'}