1 # generate variadic calls to test the
4 from random
import seed
, randint
, uniform
5 from struct
import unpack
10 # list of formats to test
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), (9, 0),
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 <stdarg.h>')
67 print('# #include <stdio.h>')
69 for fnum
, (nia
, nfa
) in enumerate(generate
):
70 params
= "{}{}char *, ...".format(
71 mkargs(nia
, 'int ', 'argw'),
72 mkargs(nfa
, 'double ', 'argd')
74 for name
in ['qbeprint', 'qbecall']:
75 print('# extern void {}{}({});'
76 .format(name
, fnum
, params
)
80 print('# int print(char *fmt, va_list *ap) {')
81 print('# return vprintf(fmt, *ap);');
83 print('# int main() {')
85 for fnum
, (nia
, nfa
) in enumerate(generate
):
86 info
= '# ({} int, {} double)'.format(nia
, nfa
)
87 print('# puts("{}");'.format(info
))
88 output
+= '# {}\n'.format(info
)
91 vaargs
= ', '.join(ra
)
94 vaargs
= ', ' + vaargs
100 for name
in ['qbeprint', 'qbecall']:
102 .format(name
, fnum
, args
)
104 output
+= '# {}\n'.format(expect
)
109 print('\n# >>> output\n' + output
+ '# <<<')
124 %p =l phi @start %fmt1, @casef %p1, @cased %p1
130 jnz %isg, @casef, @cased
133 %r =w call $printf(l %fmtdbl, ..., d %dbl)
137 %r =w call $printf(l %fmtint, ..., w %int)
140 %r =w call $puts(l %emptys)
144 unpack("i", b
'%d \x00')[0],
145 unpack("i", b
'%g \x00')[0]
152 %r =w call $print(l %fmt, l %vp)
158 if __name__
== "__main__":
160 genssa(qbeprint
, qbecall
)