1#!/usr/bin/perl
2#	$OpenBSD: genc.pl,v 1.4 2004/08/05 11:39:37 art Exp $
3#
4# Copyright (c) 2003 Jason L. Wright (jason@thought.net)
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19# DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29print <<MY__EOF
30
31#include <stdio.h>
32#include <err.h>
33#include "fpregs.h"
34
35struct fpquad {
36	u_int32_t x1;
37	u_int32_t x2;
38	u_int32_t x3;
39	u_int32_t x4;
40};
41
42int compare_regs(union fpregs *, union fpregs *);
43void dump_reg(union fpregs *);
44void dump_regs(union fpregs *, union fpregs *, union fpregs *);
45void c_ldq(union fpregs *, int, struct fpquad *);
46void test_asm_ldq(char *, void (*)(struct fpquad *));
47
48int
49compare_regs(union fpregs *fr1, union fpregs *fr2)
50{
51	return (memcmp(fr1, fr2, sizeof(*fr2)));
52}
53
54void
55dump_reg(union fpregs *fr)
56{
57	int i;
58
59	for (i = 0; i < 64; i++) {
60		if ((i & 3) == 0)
61			printf("f%-2d:", i);
62		printf(" %08x", fr->f_reg32[i]);
63		if ((i & 3) == 3)
64			printf("\\n");
65	}
66}
67
68void
69dump_regs(union fpregs *fr1, union fpregs *fr2, union fpregs *fr3)
70{
71	printf("BEFORE ASM\\n");
72	dump_reg(fr1);
73	printf("AFTER ASM\\n");
74	dump_reg(fr2);
75	printf("MANUAL\\n");
76	dump_reg(fr3);
77}
78
79void
80c_ldq(union fpregs *frp, int freg, struct fpquad *q)
81{
82	frp->f_reg32[freg] = q->x1;
83	frp->f_reg32[freg + 1] = q->x2;
84	frp->f_reg32[freg + 2] = q->x3;
85	frp->f_reg32[freg + 3] = q->x4;
86}
87
88MY__EOF
89;
90for ($i = -4096; $i <= 4095; $i++) {
91	if ($i < 0) {
92		$name = -$i;
93		$name = "_$name";
94	} else {
95		$name = $i;
96	}
97	print "void simm13_ldq_$name(struct fpquad *);\n";
98	print "void simm13_stq_$name(struct fpquad *);\n";
99}
100
101print <<MY__EOF
102void
103test_asm_ldq(char *desc, void (*func)(struct fpquad *))
104{
105	union fpregs fr1, fr2, fr3;
106	struct fpquad q;
107
108	q.x1 = 0x01234567;
109	q.x2 = 0x89abcdef;
110	q.x3 = 0x55aa55aa;
111	q.x4 = 0xaaaa5555;
112
113	initfpregs(&fr1);
114	initfpregs(&fr2);
115	initfpregs(&fr3);
116
117	loadfpregs(&fr1);
118	(*func)(&q);
119	savefpregs(&fr2);
120
121	c_ldq(&fr3, 0, &q);
122
123	if (compare_regs(&fr2, &fr3)) {
124		dump_regs(&fr1, &fr2, &fr3);
125		exit(1);
126		errx(1, "%s failed", desc);
127	}
128}
129MY__EOF
130;
131
132$j = 0;
133for ($i = -4096; $i <= 4095; $i++) {
134	if (($i % 256) == 0) {
135		print "void test_$j(void);\n";
136		$j++;
137	}
138}
139
140$j = 0;
141for ($i = -4096; $i <= 4095; $i++) {
142	if ($i < 0) {
143		$name = -$i;
144		$name = "_$name";
145	} else {
146		$name = $i;
147	}
148	if (($i % 256) == 0) {
149		print "void\n";
150		print "test_$j(void) {\n";
151		$j++;
152	}
153	print "	test_asm_ldq(\"ldq [%o + $i], %f0\", simm13_ldq_$name);\n";
154	if (($i % 256) == 255) {
155		print "}\n";
156	}
157}
158
159print <<MY__EOF
160int
161main(void)
162{
163MY__EOF
164;
165
166for ($i = 0; $i < $j; $i++) {
167	print "	test_$i();\n";
168}
169print "	return (0);\n";
170print "}\n";
171