summary refs log tree commit diff
path: root/tools/vatest.py
blob: 1a1f19913221d5a6af9c242ca62eb567b9d2272f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# generate variadic calls to test the
# abi implementation

from random import seed, randint, uniform
from struct import unpack

I, D = 'd', 'g'

formats = [
	# list of formats to test
	[I],
	[D],
	[I,D],
	[D,D],
	[I,I,I,I],
	[D,D,D,D],
	[I,D,I,D],
	[D,D,I,I],
	[I,I,D,D],
	[],
]

generate = [
	# numbers of fixed integer and
	# floating point arguments to
	# test
	(0, 0), (1, 0), (0, 1), (4, 0),
	(0, 6), (5, 7), (10, 10), (9, 0),
]

def mkargs(nargs, type, name):
	args = map(
		lambda n: ''.join([type, name, str(n), ', ']),
		range(nargs)
	)
	return ''.join(args)

def mkfstr(fmt):
	fstr = map(
		lambda x: {I: '%d ', D: '%g '}[x],
		fmt
	)
	return '"' + ''.join(fstr) + '\\n"'

def randargs(fmt):
	ra = {
		I: lambda: '{}'.format(randint(-10, 10)),
		D: lambda: '{0:.4g}'.format(uniform(-10, 10))
	}
	return list(map(lambda x: ra[x](), fmt))

def genssa(qbeprint, qbecall):
	funcs = [('qbeprint', qbeprint), ('qbecall', qbecall)]
	for fnum, (nia, nfa) in enumerate(generate):
		params = "{}{}l %fmt, ...".format(
			mkargs(nia, 'w ', '%argw'),
			mkargs(nfa, 'd ', '%argd')
		)
		for name, code in funcs:
			print('export function ${}{}({}) {}'
				.format(name, fnum, params, code)
			)

def gendriver():
	print('# >>> driver')
	print('# #include <stdarg.h>')
	print('# #include <stdio.h>')

	for fnum, (nia, nfa) in enumerate(generate):
		params = "{}{}char *, ...".format(
			mkargs(nia, 'int ', 'argw'),
			mkargs(nfa, 'double ', 'argd')
		)
		for name in ['qbeprint', 'qbecall']:
			print('# extern void {}{}({});'
				.format(name, fnum, params)
			)

	output = ''
	print('# int print(char *fmt, va_list *ap) {')
	print('#    return vprintf(fmt, *ap);');
	print('# }')
	print('# int main() {')

	for fnum, (nia, nfa) in enumerate(generate):
		info = '# ({} int, {} double)'.format(nia, nfa)
		print('# 	puts("{}");'.format(info))
		output += '# {}\n'.format(info)
		for fmt in formats:
			ra = randargs(fmt)
			vaargs = ', '.join(ra)
			expect = ' '.join(ra)
			if fmt:
				vaargs = ', ' + vaargs
				expect = expect + ' '
			args = ''.join(
				['0, '] * (nia+nfa) +
				[mkfstr(fmt), vaargs]
			)
			for name in ['qbeprint', 'qbecall']:
				print('# 	{}{}({});'
					.format(name, fnum, args)
				)
				output += '# {}\n'.format(expect)

	print('# }')
	print('# <<<')

	print('\n# >>> output\n' + output + '# <<<')


qbeprint="""{{
@start
	%fmtdbl =l alloc4 4
	%fmtint =l alloc4 4
	%emptys =l alloc4 4
	storew {}, %fmtint
	storew {}, %fmtdbl
	storew 0, %emptys
	%vp =l alloc8 32
	%fmt1 =l add 1, %fmt
	vastart %vp
@loop
	%p =l phi @start %fmt1, @casef %p1, @cased %p1
	%c =w loadsb %p
	%p1 =l add 3, %p
	jnz %c, @loop1, @end
@loop1
	%isg =w ceqw %c, 103
	jnz %isg, @casef, @cased
@casef
	%dbl =d vaarg %vp
	%r =w call $printf(l %fmtdbl, ..., d %dbl)
	jmp @loop
@cased
	%int =w vaarg %vp
	%r =w call $printf(l %fmtint, ..., w %int)
	jmp @loop
@end
	%r =w call $puts(l %emptys)
	ret
}}
""".format(
	unpack("i", b'%d \x00')[0],
	unpack("i", b'%g \x00')[0]
)

qbecall="""{
@start
	%vp =l alloc8 32
	vastart %vp
	%r =w call $print(l %fmt, l %vp)
	ret
}
"""


if __name__ == "__main__":
	seed(42)
	genssa(qbeprint, qbecall)
	gendriver()