home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * commands.c
- * Copyright © 1992 Niklas Röjemo
- */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include "error.h"
- #include "input.h"
- #include "lex.h"
- #include "help_lex.h"
- #include "symbol.h"
- #include "expr.h"
- #include "code.h"
- #include "value.h"
- #include "put.h"
- #include "commands.h"
- #include "fix.h"
- #include "include.h"
- #include "filestack.h"
- #include "area.h"
- #include "lit.h"
-
- extern FILE *asmfile; /* in input.c */
-
-
- static Symbol *c_define(char *msg, Symbol *sym, ValueTag legal)
- {
- Value value;
- if(!sym)
- error(ErrorError,FALSE,"Missing label before %s.");
- sym->type |= SYMBOL_ABSOLUTE;
- exprBuild();
- value = exprEval(legal);
- switch(value.Tag) {
- case ValueIllegal:
- error(ErrorError,TRUE,"Illegal %s.",msg);
- sym->value.Tag = ValueInt;
- sym->value.ValueInt.i = 0;
- break;
- default:
- sym->value = valueCopy(value);
- }
- sym->type |= SYMBOL_DEFINED;
- sym->area.ptr = 0;
- return sym;
- }
-
- void c_equ(Symbol *symbol)
- {
- (void)c_define("equ",symbol,ValueAll);
- }
-
- void c_fn(Symbol *symbol)
- {
- int no = c_define("float register",symbol,ValueInt)->value.ValueInt.i;
- symbol->type |= SYMBOL_FPUREG;
- if(no<0 || no>7) {
- symbol->value.ValueInt.i = 0;
- error(ErrorError,TRUE,"Illegal fpu register %d (using 0).",no);
- }
- }
-
- void c_rn(Symbol *symbol)
- {
- int no=c_define("register",symbol,ValueInt)->value.ValueInt.i;
- symbol->type |= SYMBOL_CPUREG;
- if(no<0 || no>15) {
- symbol->value.ValueInt.i = 0;
- error(ErrorError,TRUE,"Illegal cpu register %d (using 0).",no);
- }
- }
-
- void c_cn(Symbol *symbol)
- {
- int no = c_define("register",symbol,ValueInt)->value.ValueInt.i;
- symbol->type |= SYMBOL_COPREG;
- if(no<0 || no>15) {
- symbol->value.ValueInt.i = 0;
- error(ErrorError,TRUE,"Illegal cop register %d (using 0).",no);
- }
- }
-
- void c_ltorg(void)
- {
- if(areaCurrent) {
- litOrg(areaCurrent->area.info->lits);
- } else {
- error(ErrorError,TRUE,"No area defined.");
- }
- }
-
- static void defineint(int size)
- {
- Value value;
- WORD word = 0;
- int c;
- do {
- skipblanks();
- exprBuild();
- value = exprEval(ValueInt | ValueString|ValueCode|ValueLateLabel);
- switch(value.Tag) {
- case ValueInt:
- word = fixInt(inputLineNo,size,value.ValueInt.i);
- putData(size,word);
- break;
- case ValueString:
- if(size == 1) { /* Lay out a string */
- int len = value.ValueString.len;
- char *str = value.ValueString.s;
- while(len>0)
- putData(1,lexGetCharFromString(&len,&str));
- } else {
- putData(size,lexChar2Int(FALSE,value.ValueString.len,value.ValueString.s));
- }
- break;
- case ValueCode: case ValueLateLabel:
- relocInt(size,value);
- putData(size,word);
- break;
- default:
- error(ErrorError,TRUE,"Illegal int expression.");
- }
- skipblanks();
- } while(((c=inputGet())!=0) && c == ',');
- inputUnGet(c);
- }
-
- void c_dcb(void)
- {
- defineint(1);
- }
-
- void c_dcw(void)
- {
- defineint(2);
- }
-
- void c_dcd(void)
- {
- defineint(4);
- }
-
- static void definereal(int size)
- {
- Value value;
- int c;
- do {
- skipblanks();
- exprBuild();
- value = exprEval(ValueFloat | ValueLateLabel|ValueCode);
- switch(value.Tag) {
- case ValueFloat:
- putDataFloat(size,value.ValueFloat.f);
- break;
- case ValueCode: case ValueLateLabel:
- relocFloat(size,value);
- putDataFloat(size,0.0);
- break;
- default:
- error(ErrorError,TRUE,"Illegal float expression.");
- }
- skipblanks();
- } while(((c=inputGet())!=0) && c == ',');
- inputUnGet(c);
- }
-
- void c_dcfd(void)
- {
- definereal(8);
- }
-
- void c_dcfe(void)
- {
- error(ErrorError,FALSE,"Not implemented dcfe %s",inputRest());
- }
-
- void c_dcfs(void)
- {
- definereal(4);
- }
-
- void c_dcfp(void)
- {
- error(ErrorError,FALSE,"Not implemented dcfp %s",inputRest());
- }
-
- void c_globl(void)
- {
- Lex lex;
- Symbol *sym;
- lex = lexGetId();
- sym = symbolGet(lex);
- sym->type |= SYMBOL_REFERENCE;
- }
-
- extern int verbose;
-
- char *strdup(char *src)
- {
- char *dst = malloc(strlen(src)+1);
- if(dst)
- strcpy(dst,src);
- return dst;
- }
-
- void c_get(void)
- {
- char *filename, *cptr;
- FILE *getfp;
-
- filename = strdup(inputRest());
- if (!filename)
- error(ErrorSerious, TRUE, "strdup failed in c_get()");
- for (cptr = filename; !isspace(*cptr); cptr++)
- ;
- *cptr = '\0';
- inputNextLine();
- getfp = getInclude(filename);
- if (!getfp) {
- error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
- return;
- }
- push_file(asmfile);
- asmfile = getfp;
- if(verbose)
- printf("including file %s\n", filename);
- }
-
- void c_lnk(void)
- {
- char *filename, *cptr;
- FILE *lnkfp;
-
- filename = strdup(inputRest());
- if (!filename)
- error(ErrorSerious, TRUE, "strdup failed in c_lnk()");
- for (cptr = filename; !isspace(*cptr); cptr++)
- ;
- *cptr = '\0';
- inputNextLine();
- lnkfp = getInclude(filename);
- if (!lnkfp) {
- error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
- return;
- }
- skiprest();
- inputFinish();
- asmfile = lnkfp;
- if(verbose)
- printf("linking to file %s\n", filename);
- }
-