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