1/*	$NetBSD: gsp_act.c,v 1.4 2001/06/13 10:46:06 wiz Exp $	*/
2/*
3 * GSP assembler - semantic actions
4 *
5 * Copyright (c) 1993 Paul Mackerras.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *      This product includes software developed by Paul Mackerras.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35#ifndef lint
36__RCSID("$NetBSD: gsp_act.c,v 1.4 2001/06/13 10:46:06 wiz Exp $");
37#endif
38
39#include <stdlib.h>
40#include <string.h>
41#include <util.h>
42#include "gsp_ass.h"
43
44void
45free_operands(operand l)
46{
47	operand op, oq;
48
49	for( op = l; op != NULL; op = oq ){
50		oq = op->next;
51		if( op->type == EXPR ||
52		    (op->type == EA && op->mode >= M_INDEX) )
53			free_expr(op->op_u.value);
54		free(op);
55	}
56}
57
58operand
59add_operand(operand first, operand last)
60{
61	operand p;
62
63	for( p = first; p->next != NULL; p = p->next )
64		;
65	p->next = last;
66	return first;
67}
68
69operand
70reg_op(int reg)
71{
72	operand o;
73
74/*	printf("reg_op reg=%d sign=%d\n", reg, sign);	*/
75	new(o);
76	o->type = REG;
77	o->reg_no = reg;
78	o->next = NULL;
79	return o;
80}
81
82operand
83expr_op(expr val)
84{
85	operand o;
86
87/*	printf("immed len=%d\n", len);	*/
88	new(o);
89	o->type = EXPR;
90	o->op_u.value = val;
91	o->next = NULL;
92	return o;
93}
94
95operand
96string_op(char *str)
97{
98	operand o;
99
100/*	printf("string_op str=%s\n", str);	*/
101	new(o);
102	o->type = STR_OPN;
103	o->op_u.string = str;
104	o->next = NULL;
105	return o;
106}
107
108operand
109abs_adr(expr adr)
110{
111	operand o;
112
113/*	printf("abs_adr len=%d\n", len);	*/
114	new(o);
115	o->type = EA;
116	o->mode = M_ABSOLUTE;
117	o->op_u.value = adr;
118	o->next = NULL;
119	return o;
120}
121
122operand
123reg_ind(int reg, int mode)
124{
125	operand o;
126
127/*	printf("reg_adr r1=%d r2=%d mode=%d\n", r1, r2, mode);	*/
128	new(o);
129	o->type = EA;
130	o->mode = mode;
131	o->reg_no = reg;
132	o->next = NULL;
133	return o;
134}
135
136operand
137reg_indxy(int reg, char *xy)
138{
139	ucasify(xy);
140	if( strcmp(xy, ".XY") != 0 )
141		perr("Register format must be .XY");
142	return reg_ind(reg, M_INDXY);
143}
144
145operand
146reg_index(int reg, expr disp)
147{
148	operand o;
149
150	o = reg_ind(reg, M_INDEX);
151	o->op_u.value = disp;
152	return o;
153}
154
155expr
156id_expr(char *id)
157{
158	expr x;
159
160/*	printf("id_expr id=%s\n", id);	*/
161	new(x);
162	x->e_op = SYM;
163	x->e_sym = lookup(id, TRUE);
164	return x;
165}
166
167expr
168num_expr(int val)
169{
170	expr x;
171
172/*	printf("num_expr val=%d\n", val);	*/
173	new(x);
174	x->e_op = CONST;
175	x->e_val = val;
176	return x;
177}
178
179expr
180here_expr()
181{
182	expr x;
183
184/*	printf("here_expr()\n");	*/
185	new(x);
186	x->e_op = '.';
187	return x;
188}
189
190expr
191bexpr(int op, expr l, expr r)
192{
193	expr x;
194
195/*	printf("bexpr op=%d\n", op);	*/
196	new(x);
197	x->e_op = op;
198	x->e_left = l;
199	x->e_right = r;
200	return x;
201}
202
203void
204free_expr(expr x)
205{
206	if( x->e_op != SYM && x->e_op != CONST && x->e_op != '.' ){
207		free_expr(x->e_left);
208		if( x->e_right != NULL )
209			free_expr(x->e_right);
210	}
211	free(x);
212}
213