1 # generate variadic calls to test the
4 from random
import seed
, randint
, uniform
5 from struct
import unpack
10 # list of format to tests
24 # numbers of fixed integer and
25 # floating point arguments to
27 (0, 0), (1, 0), (0, 1), (4, 0),
28 (0, 6), (5, 7), (10, 10),
31 def mkargs(nargs
, type, name
):
33 lambda n
: ''.join([type, name
, str(n
), ', ']),
40 lambda x
: {I
: '%d ', D
: '%g '}[x
],
43 return '"' + ''.join(fstr
) + '\\n"'
47 I
: lambda: '{}'.format(randint(-10, 10)),
48 D
: lambda: '{0:.4g}'.format(uniform(-10, 10))
50 return list(map(lambda x
: ra
[x
](), fmt
))
52 def genssa(qbeprint
, qbecall
):
53 funcs
= [('qbeprint', qbeprint
), ('qbecall', qbecall
)]
54 for fnum
, (nia
, nfa
) in enumerate(generate
):
55 params
= "{}{}l %fmt, ...".format(
56 mkargs(nia
, 'w ', '%argw'),
57 mkargs(nfa
, 'd ', '%argd')
59 for name
, code
in funcs
:
60 print('export function ${}{}({}) {}'
61 .format(name
, fnum
, params
, code
)
66 print('# #include <stdio.h>')
68 for fnum
, (nia
, nfa
) in enumerate(generate
):
69 params
= "{}{}char *, ...".format(
70 mkargs(nia
, 'int ', 'argw'),
71 mkargs(nfa
, 'double ', 'argd')
73 for name
in ['qbeprint', 'qbecall']:
74 print('# extern void {}{}({});'
75 .format(name
, fnum
, params
)
79 print('# int main() {')
81 for fnum
, (nia
, nfa
) in enumerate(generate
):
82 info
= '# ({} int, {} double)'.format(nia
, nfa
)
83 print('# puts("{}");'.format(info
))
84 output
+= '# {}\n'.format(info
)
87 vaargs
= ', '.join(ra
)
90 vaargs
= ', ' + vaargs
96 for name
in ['qbeprint', 'qbecall']:
98 .format(name
, fnum
, args
)
100 output
+= '# {}\n'.format(expect
)
105 print('\n# >>> output\n' + output
+ '# <<<')
120 %p =l phi @start %fmt1, @casef %p1, @cased %p1
126 jnz %isg, @casef, @cased
129 call $printf(l %fmtdbl, d %dbl, ...)
133 call $printf(l %fmtint, w %int, ...)
136 call $puts(l %emptys)
140 unpack("i", b
'%d \x00')[0],
141 unpack("i", b
'%g \x00')[0]
148 call $vprintf(l %fmt, l %vp)
154 if __name__
== "__main__":
156 genssa(qbeprint
, qbecall
)