Home | History | Annotate | Line # | Download | only in gspa
      1 /*	$NetBSD: gsp_pseu.c,v 1.4 2025/11/24 08:04:28 nia Exp $	*/
      2 /*
      3  * GSP assembler - assembler directives
      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_pseu.c,v 1.4 2025/11/24 08:04:28 nia Exp $");
     34 #endif
     35 
     36 #include "gsp_ass.h"
     37 #include "gsp_code.h"
     38 
     39 extern unsigned highest_pc, line_pc;
     40 
     41 void
     42 pseudo(int code, operand ops)
     43 {
     44 	operand o;
     45 	int32_t val;
     46 	unsigned ln;
     47 	u_int16_t words[2];
     48 
     49 	switch( code ){
     50 	case ORG:
     51 		if( ops == NULL )
     52 			break;
     53 		if( ops->type != EXPR ){
     54 			perr("Inappropriate operand");
     55 			break;
     56 		}
     57 		if( !eval_expr(ops->op_u.value, &val, &ln) ){
     58 			p1err("ORG operand must be defined on pass 1");
     59 			break;
     60 		}
     61 		if( pc > highest_pc )
     62 			highest_pc = pc;
     63 		line_pc = pc = val;
     64 		do_list_pc();
     65 		break;
     66 #ifdef EQU
     67 	case EQU:
     68 		if( label == NULL ){
     69 			perr("Label required");
     70 			break;
     71 		}
     72 		if( ops == NULL )
     73 			break;
     74 		if( ops->type != EXPR ){
     75 			perr("Inappropriate operand");
     76 			break;
     77 		}
     78 		do_asg(label, ops->op_u.value, 0);
     79 		break;
     80 #endif /* EQU */
     81 	case WORD:
     82 	case LONG:
     83 		if( ops == NULL )
     84 			break;
     85 		for( o = ops; o != NULL; o = o->next ){
     86 			if( o->type != EXPR ){
     87 				perr("Inappropriate operand");
     88 				continue;
     89 			}
     90 			if( pass2 ){
     91 				eval_expr(o->op_u.value, &val, &ln);
     92 				words[0] = val;
     93 				if( code == LONG ){
     94 					words[1] = val >> 16;
     95 					putcode(words, 2);
     96 				} else {
     97 					if( val < -32768 || val > 65535 )
     98 						perr("Word value too large");
     99 					putcode(words, 1);
    100 				}
    101 			} else
    102 				pc += code == LONG? 0x20: 0x10;
    103 		}
    104 		return;
    105 	case INCL:
    106 		if( ops == NULL )
    107 			break;
    108 		if( ops->type != STR_OPN ){
    109 			perr("Require filename string");
    110 			break;
    111 		}
    112 		push_input(ops->op_u.string);
    113 		break;
    114 	case BLKB:
    115 	case BLKW:
    116 	case BLKL:
    117 		if( ops == NULL )
    118 			break;
    119 		if( ops->type != EXPR ){
    120 			perr("Inappropriate operand");
    121 			break;
    122 		}
    123 		if( !eval_expr(ops->op_u.value, &val, &ln) ){
    124 			p1err(".BLK%c operand must be defined on pass 1",
    125 				code==BLKB? 'B': code==BLKW? 'W': 'L');
    126 			break;
    127 		}
    128 		val *= 8;
    129 		if( code == BLKB )
    130 			val = (val + 8) & ~15;	/* round to word */
    131 		else
    132 			val *= (code==BLKW? 2: 4);
    133 		pc += val;
    134 		do_list_pc();
    135 		break;
    136 	case START:
    137 		if( !pass2 || ops == NULL )
    138 			break;
    139 		if( ops->type != EXPR ){
    140 			perr("Inappropriate operand");
    141 			break;
    142 		}
    143 		eval_expr(ops->op_u.value, &val, &ln);
    144 		start_at(val);
    145 		do_show_val(val);
    146 		break;
    147 	}
    148 	if( ops == NULL )
    149 		perr("Insufficient operands");
    150 	else if( ops->next != NULL )
    151 		perr("Extra operands ignored");
    152 }
    153