scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

ins.c (978B)


      1 static char sccsid[] = "@(#) ./as/ins.c";
      2 
      3 #include "../inc/scc.h"
      4 #include "as.h"
      5 
      6 char *
      7 tobytes(TUINT v, int nbytes, int inc)
      8 {
      9 	static char buf[sizeof(TUINT)];
     10 	int idx;
     11 
     12 	idx = (inc < 0) ? nbytes-1 : 0;
     13 	while (nbytes--) {
     14 		buf[idx] = v;
     15 		idx += inc;
     16 		v >>= 8;
     17 	}
     18 
     19 	if (v)
     20 		error("overflow in immediate value");
     21 	return buf;
     22 }
     23 
     24 void
     25 noargs(Op *op, Node **args)
     26 {
     27 	emit(cursec, op->bytes, op->size);
     28 }
     29 
     30 void
     31 def(Node **args, int siz)
     32 {
     33 	Node *np;
     34 
     35 	for ( ; np = *args; ++args) {
     36 		Symbol *sym = np->sym;
     37 
     38 		if (sym->flags & FUNDEF)
     39 			reloc(sym, 0, siz, siz * 8, 0);
     40 		emit(cursec, tobytes(sym->value, siz, endian), siz);
     41 	}
     42 }
     43 
     44 void
     45 defb(Op *op, Node **args)
     46 {
     47 	def(args, 1);
     48 }
     49 
     50 void
     51 defw(Op *op, Node **args)
     52 {
     53 	def(args, 2);
     54 }
     55 
     56 void
     57 defd(Op *op, Node **args)
     58 {
     59 	def(args, 4);
     60 }
     61 
     62 void
     63 defq(Op *op, Node **args)
     64 {
     65 	def(args, 8);
     66 }
     67 
     68 void
     69 equ(Op *op, Node **args)
     70 {
     71 	if (!linesym)
     72 		error("label definition lacks a label");
     73 	else
     74 		linesym->value = (*args)->sym->value;
     75 }