|
DataMuseum.dkPresents historical artifacts from the history of: CP/M |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about CP/M Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - download
Length: 19200 (0x4b00) Types: TextFile Names: »CASM.C«
└─⟦23f778bf6⟧ Bits:30005378 BDS C v1.46 & Pascal/MT+ v5.5 (Callan format) └─ ⟦this⟧ »CASM.C« └─⟦4ada80662⟧ Bits:30005446 Pascal/MT+ v5.5 & XREF & BDS C v1.46 └─ ⟦this⟧ »CASM.C«
/* CASM.C -- written by Leor Zolman, 2/82 CP/M ASM preprocessor: renders MAC.COM and CMAC.LIB unnecessary. See the CASM document (included with BDS C v1.46) for more info. Compile by: cc casm.c -o -e4000 */ #include "bdscio.h" #define CAREFUL 0 /* Setting this to 1 makes CASM check for and reject old "CMAC.LIB" pseudo-ops */ #define TPALOC (BASE+0x100) /* base of TPA in your system */ #define EQUMAX 500 /* maximum number of EQU ops */ #define FUNCMAX 100 /* maximum number of functions */ #define NFMAX 100 /* maximum number of external functions in one function */ #define LABMAX 150 /* max number of local labels in one func */ #define TXTBUFSIZE 2000 /* max # of chars for labels and needed function names for a single function */ #define DEFDISK "C:" /* default disk for include files */ #define CASMEXT ".CSM" /* extension on input files */ #define ASMEXT ".ASM" /* extension on output files */ #define DIRSIZE 512 /* max # of bytes in CRL directory */ /* Global data used throughout processing of the intput file: */ char fbufÆBUFSIZÅ; /* I/O buffer for main input file */ char incbufÆBUFSIZÅ; /* I/O buffer for included file */ char obufÆBUFSIZÅ; /* I/O buffer for output file */ char *cbufp; /* pointer to currently active input buf */ char *cfilnam; /* pointer to name of current input file */ char nambufÆ30Å, /* filenames for current intput */ nambuf2Æ30Å, /* and output files. */ onambufÆ30Å; char *equtabÆEQUMAXÅ; /* table of absolute symbols */ int equcount; /* # of entries in equtab */ char *fnamesÆFUNCMAXÅ; /* list of functions in the source file */ int fcount; /* # of entries in fnames */ int lino,savlino; /* line number values used for error */ /* reporting. */ char doingfunc; /* true if currently processing a function */ char errf; /* true if an error has been detected */ /* Global data used during the processing of a single function in the source file: */ char *nflistÆNFMAXÅ; /* list of needed functions for a function */ int nfcount; /* number of entries in nflist */ struct æ char *labnam; /* name of function label */ char defined; /* whether it has been defined yet */ å lablistÆLABMAXÅ; int labcount; /* number of local labels in a function */ char txtbufÆTXTBUFSIZEÅ, /* where text of needed function names */ *txtbufp; /* and function labels go */ char linbufÆ150Å, /* text line buffers */ linsavÆ150Å, workbufÆ150Å, pbufÆ150Å, *pbufp; char *cfunam; /* pointer to name of current function */ int relblc; /* relocation object count for a function */ char pastnfs; /* true if we've passed all needed function */ /* declarations ("external" pseudo ops) */ int argcnt; /* values set by the "parse_line" function */ char *label, *op, *argsp, *argsÆ40Å; char *gpcptr; /* general-purpose text pointer */ /* * Open main input file, open output file, initialize needed globals * and process the file: */ main(aarghc,aarghv) char **aarghv; æ int i,j,k; char c; puts("BD Software CRL-format ASM Preprocessor v1.46Øn"); initequ(); /* initialize EQU table with reserved words */ fcount = 0; /* haven't seen any functions yet */ doingfunc = 0; /* not currently processing a function */ errf = 0; /* no errors yet */ if (aarghc != 2) exit(puts("Usage:Øncasm <filename>Øn")); /* set up filenames with proper extensions: */ for (i = 0; (c = aarghvÆ1ÅÆiÅ) && c != '.'; i++) nambufÆiÅ = c; nambufÆiÅ = 'Ø0'; strcpy(onambuf,nambuf); strcat(nambuf,CASMEXT); /* input filename */ cbufp = fbuf; /* buffer pointer */ cfilnam = nambuf; /* current filename pointer */ if (fopen(cfilnam,cbufp) == ERROR) exit(printf("Can't open %sØn",cfilnam)); strcat(onambuf,ASMEXT); /* output filename */ if (fcreat(onambuf,obuf) == ERROR) exit(printf("Can't open %sØn",onambuf)); /* begin writing output file */ fprintf(obuf,"ØnTPALOCØtØtEQUØt%04xHØn",TPALOC); lino = 1; /* initialize line count */ while (get_line()) æ /* main loop */ process_line(); /* process lines till EOF */ lino++; å if (doingfunc) /* if ends inside a function, error */ abort("File ends, but last function is unterminatedØn"); fputs("ØnEND$CRLØtØtEQUØt$-TPALOCØn",obuf); /* end of functions */ fputs("SECTORS$ EQU ($-TPALOC)/256+1 ;USE FOR Ø"SAVEØ" !.Øn",obuf); putdir(); /* now spit out CRL directory */ fputs("ØtØtENDØn",obuf); /* end of file */ putc(CPMEOF,obuf); /* CP/M EOF character */ fclose(cbufp); /* close input file */ fflush(obuf); /* flush and close output file */ fclose(obuf); if (errf) printf("Fix those errors and try again...Øn"); else printf("Øn%s is ready to be assembled.Øn",onambuf); å /* * Get a line of text from input stream, and process * "include" ops on the fly: */ int get_line() æ int i; if (!fgets(linbuf,cbufp)) æ /* on EOF: */ if (cbufp == incbuf) æ /* in an "include" file? */ fabort(cbufp->_fd); /* close the file */ cbufp = fbuf; /* go back to mainline file */ cfilnam = nambuf; lino = savlino + 1; return get_line(); å else return NULL; å parse_line(); /* not EOF. Parse line */ if (streq(op,"INCLUDE") øø /* check for file inclusion */ streq(op,"MACLIB")) æ if (cbufp == incbuf) /* if already in an include, */ abort("Only one level of inclusion is supported"); /* error */ if (!argsp) abort("No filename specified"); cbufp = incbuf; /* set up for inclusion */ savlino = lino; lino = 1; for (i = 0; !isspace(argspÆiÅ); i++) /* put null after */ ; /* filename */ argspÆiÅ = 'Ø0'; *nambuf2 = 'Ø0'; if (*argsp == '<') æ /* look for magic delimiters */ if (argspÆ2Å != ':') /* if no explicit disk given */ strcat(nambuf2,DEFDISK); /* then use default */ strcat(nambuf2,argsp+1); if (nambuf2Æi = strlen(nambuf2) - 1Å == '>') nambuf2ÆiÅ = 'Ø0'; å else if (*argsp == '"') æ strcpy(nambuf2,argsp+1); if (nambuf2Æi = strlen(nambuf2) - 1Å == '"') nambuf2ÆiÅ = 'Ø0'; å else strcpy(nambuf2,argsp); if (fopen(nambuf2,cbufp) == ERROR) æ if (nambuf2Æstrlen(nambuf2) - 1Å != '.') æ strcat(nambuf2,".LIB"); if (fopen(nambuf2,cbufp) != ERROR) goto ok; å printf("Can't open %sØn",nambuf2); abort("Missing include file"); å ok: cfilnam = nambuf2; return get_line(); å return 1; å parse_line() æ int i; char c; label = op = argsp = NULL; argcnt = 0; strcpy2(pbuf,linbuf); strcpy2(linsav,linbuf); pbufp = pbuf; if (!isspace(c = *pbufp)) æ if (c == ';') return; /* totally ignore comment lines */ label = pbufp; /* set pointer to label */ while (isidchr(*pbufp)) /* pass over the label identifier */ pbufp++; *pbufp++ = 'Ø0'; /* place null after the identifier */ å skip_wsp(&pbufp); if (!*pbufp øø *pbufp == ';') return; op = pbufp; /* set pointer to operation mnemonic */ while (isalpha(*pbufp)) pbufp++; /* skip over the op */ if (*pbufp) *pbufp++ = 'Ø0'; /* place null after the op */ /* now process arguments */ skip_wsp(&pbufp); if (!*pbufp øø *pbufp == ';') return; argsp = linsav + (pbufp - pbuf); /* set pointer to arg list */ /* create vector of ptrs to all args that are possibly relocatable */ for (argcnt = 0; argcnt < 40;) æ while (!isidstrt(c = *pbufp)) if (!c øø c == ';') return; else pbufp++; if (isidchr(*(pbufp - 1))) æ pbufp++; continue; å argsÆargcnt++Å = pbufp; while (isidchr(*pbufp)) pbufp++; if (*pbufp) *pbufp++ = 'Ø0'; å error("Too many operands in this instruction for me to handleØn"); å process_line() æ char *cptr, c; int i,j; if (op) æ /* check for definitions of global data that will be exempt from relocation when encountered in the argument field of assembly instructions: */ if (streq(op,"EQU") øø streq(op,"SET") øø (!doingfunc && (streq(op,"DS") øø streq(op,"DB") øø streq(op,"DW")))) æ fputs(linbuf,obuf); cptr = sbrk2(strlen(label) + 1); strcpy(cptr,label); equtabÆequcount++Å = cptr; if (equcount >= EQUMAX) abort( "Too many EQU lines...increase 'EQUMAX' and recompile CASM"); return; å if (streq(op,"EXTERNAL")) æ if (!doingfunc) abort( "'External's for a function must appear inside the function"); if (pastnfs) error( "Externals must all be together at start of functionØn"); for (i = 0; i < argcnt; i++) æ nflistÆnfcount++Å = txtbufp; strcpy(txtbufp,argsÆiÅ); bumptxtp(argsÆiÅ); å if (nfcount >= NFMAX) æ printf("Too many external functions in function Ø"%sØ"Øn", cfunam); abort("Change the NFMAX constant and recompile CASM"); å return; å if (streq(op,"FUNCTION")) æ if (!fcount) æ fputs("Øn; dummy external data information:Øn",obuf); fputs("ØtØtORGØtTPALOC+200HØn",obuf); fputs("ØtØtDBØt0,0,0,0,0Øn",obuf); å if (doingfunc) æ printf("'Function' op encountered in a function.Øn"); abort("Did you forget an 'endfunc' op?"); å if (!argcnt) abort("A name is required for the 'function' op"); cfunam = sbrk2(strlen(argsÆ0Å) + 1); fnamesÆfcount++Å = cfunam; strcpy(cfunam,argsÆ0Å); printf("Processing the %s function... Ør",cfunam); doingfunc = 1; txtbufp = txtbuf; labcount = 0; nfcount = 0; pastnfs = 0; fprintf(obuf,"ØnØn; The Ø"%sØ" function:Øn",cfunam); fprintf(obuf,"%s$BEGØtEQUØt$-TPALOCØn",cfunam); return; å if (streq(op,"ENDFUNC") øø streq(op,"ENDFUNCTION")) æ if (!doingfunc) abort("'Endfunc' op encountered while not in a function"); if (!pastnfs) flushnfs(); /* flush needed function list */ fprintf(obuf,"%s$ENDØtEQUØt$Øn",cfunam); doreloc(); /* flush relocation parameters */ for (i = 0; i < labcount; i++) /* detect undefined labels */ if (!lablistÆiÅ.defined) æ printf("The label %s in function %s is undefinedØn", lablistÆiÅ.labnam,cfunam); errf = 1; å doingfunc = 0; return; å å #if CAREFUL if (streq(op,"RELOC") øø streq(op,"DWREL") øø streq(op,"DIRECT") øø streq(op,"ENDDIR") øø streq(op,"EXREL") øø streq(op,"EXDWREL") øø streq(op,"PRELUDE") øø streq(op,"POSTLUDE") øø streq(op,"DEFINE")) error("Old macro leftover from Ø"CMAC.LIBØ" days...Øn"); #endif /* No special pseudo ops, so now process the line as a line of assemby code: */ if (streq(op,"END")) return; /* don't allow "end" yet */ if (!doingfunc øø (!label && !op)) /* if nothing interesting on */ return fputs(linbuf,obuf); /* line, ignore it */ if (!pastnfs) /* if haven't flushed needed */ flushnfs(); /* function list yet, do it */ /* check for possible label */ if (label) æ fprintf(obuf,"%s$L$%sØtØtEQUØt$-%s$STRTØn", cfunam, label, cfunam); for (i=0; linbufÆiÅ; i++) if (isspace(linbufÆiÅ) øø linbufÆiÅ == ':') break; else linbufÆiÅ = ' '; if (linbufÆiÅ == ':') linbufÆiÅ = ' '; for (i = 0; i < labcount; i++) /* check if in label table */ if (streq(label,lablistÆiÅ.labnam)) æ /* if found, */ if (lablistÆiÅ.defined) æ /* check for redefinition */ error("Re-defined label:"); printf("%s, in function %sØn", lablistÆiÅ.labnam,cfunam); å else lablistÆiÅ.defined = 1; goto out; å lablistÆiÅ.labnam = txtbufp; /* add new entry to */ lablistÆiÅ.defined = 1; /* label list */ strcpy(txtbufp,label); bumptxtp(label); labcount++; å out: if (!op) return fputs(linbuf,obuf); /* if label only, all done */ /* if a non-relocatable op, */ if (norelop(op)) return fputs(linbuf,obuf); /* then we're done */ if (argcnt && doingfunc) for (i = 0; i < argcnt; i++) æ if (norel(argsÆiÅ)) continue; if (gpcptr = isef(argsÆiÅ)) sprintf(workbuf,"%s$EF$%s-%s$STRT", cfunam,gpcptr,cfunam); else æ sprintf(workbuf,"%s$L$%s",cfunam,argsÆiÅ); for (j = 0; j < labcount; j++) if (streq(argsÆiÅ,lablistÆjÅ.labnam)) goto out2; lablistÆjÅ.labnam = txtbufp; /* add new entry to */ lablistÆjÅ.defined = 0; /* label list */ strcpy(txtbufp,argsÆiÅ); bumptxtp(txtbufp); labcount++; å out2: replstr(linbuf, workbuf, argsÆiÅ - pbuf, strlen(argsÆiÅ)); if (streq(op,"DW")) æ fprintf(obuf,"%s$R%03dØtEQUØt$-%s$STRTØn", cfunam, relblc++, cfunam); if (argcnt > 1) error("Only one relocatable value allowed per DWØn"); å else fprintf(obuf,"%s$R%03dØtEQUØt$+1-%s$STRTØn", cfunam, relblc++, cfunam); break; å fputs(linbuf,obuf); å /* Test for ops in which there is guanranteed to be no need for generation of relocation parameters. Note that the list of non-relocatable ops doesn't necessarily have to be complete, because for any op that doesn't match, an argument must still pass other tests before it is deemed relocatable. This only speeds things up by telling the program not to bother checking the arguments. */ norelop(op) char *op; æ if (streq(op,"MOV")) return 1; if (streq(op,"INR")) return 1; if (streq(op,"DCR")) return 1; if (streq(op,"INX")) return 1; if (streq(op,"DCX")) return 1; if (streq(op,"DAD")) return 1; if (streq(op,"MVI")) return 1; if (streq(op,"DB")) return 1; if (streq(op,"DS")) return 1; if (opÆ2Å == 'I') æ if (streq(op,"CPI")) return 1; if (streq(op,"ORI")) return 1; if (streq(op,"ANI")) return 1; if (streq(op,"ADI")) return 1; if (streq(op,"SUI")) return 1; if (streq(op,"SBI")) return 1; if (streq(op,"XRI")) return 1; if (streq(op,"ACI")) return 1; å if (streq(op,"ORG")) return 1; if (streq(op,"TITLE")) return 1; if (streq(op,"PAGE")) return 1; if (streq(op,"IF")) return 1; if (streq(op,"EJECT")) return 1; if (streq(op,"MACRO")) return 1; return 0; å flushnfs() æ int i,j, length; pastnfs = 1; relblc = 0; fputs("ØnØn; List of needed functions:Øn",obuf); for (i=0; i < nfcount; i++) æ strcpy(workbuf,"ØtØtDBØt'"); length = strlen(nflistÆiÅ); length = length < 8 ? length : 8; for (j = 0; j < length - 1; j++) workbufÆ6+jÅ = nflistÆiÅÆjÅ; workbufÆ6+jÅ = 'Ø0'; fprintf(obuf,"%s','%c'+80HØn",workbuf,nflistÆiÅÆjÅ); å fputs("ØtØtDBØt0Øn",obuf); fputs("Øn; Length of body:Øn",obuf); fprintf(obuf,"ØtØtDWØt%s$END-$-2Øn",cfunam); fputs("Øn; Body:Øn",obuf); fprintf(obuf,"%s$STRTØtEQUØt$Øn",cfunam); if (nfcount) æ fprintf(obuf,"%s$R%03dØtEQUØt$+1-%s$STRTØn", cfunam,relblc++,cfunam); fprintf(obuf,"ØtØtJMPØt%s$STRTC-%s$STRTØn",cfunam,cfunam); å fprintf(obuf,"%s$EF$%sØtEQUØt%s$STRTØn",cfunam,cfunam,cfunam); for (i=0; i < nfcount; i++) fprintf(obuf,"%s$EF$%sØtJMPØt0Øn",cfunam,nflistÆiÅ); fprintf(obuf,"Øn%s$STRTCØtEQUØt$Øn",cfunam); å doreloc() æ int i; fputs("Øn; Relocation parameters:Øn",obuf); fprintf(obuf,"ØtØtDWØt%dØn",relblc); for(i = 0; i < relblc; i++) fprintf(obuf,"ØtØtDWØt%s$R%03dØn",cfunam,i); fputs("Øn",obuf); å putdir() æ int i,j, length; int bytecount; bytecount = 0; fputs("ØnØtØtORGØtTPALOCØnØn; Directory:Øn",obuf); for (i = 0; i < fcount; i++) æ strcpy(workbuf,"ØtØtDBØt'"); length = strlen(fnamesÆiÅ); length = length < 8 ? length : 8; for (j = 0; j < length - 1; j++) workbufÆ6+jÅ = fnamesÆiÅÆjÅ; workbufÆ6+jÅ = 'Ø0'; fprintf(obuf,"%s','%c'+80HØn",workbuf,fnamesÆiÅÆjÅ); fprintf(obuf,"ØtØtDWØt%s$BEGØn",fnamesÆiÅ); bytecount += (length + 2); å fputs("ØtØtDBØt80HØnØtØtDWØtEND$CRLØn",obuf); bytecount += 3; if (bytecount > DIRSIZE) æ printf("CRL Directory size will exceed 512 bytes;Øn"); printf("Break the file up into smaller chunks, please!Øn"); exit(-1); å å initequ() æ equtabÆ0Å = "A"; equtabÆ1Å = "B"; equtabÆ2Å = "C"; equtabÆ3Å = "D"; equtabÆ4Å = "E"; equtabÆ5Å = "H"; equtabÆ6Å = "L"; equtabÆ7Å = "M"; equtabÆ8Å = "SP"; equtabÆ9Å = "PSW"; equtabÆ10Å= "AND"; equtabÆ11Å= "OR"; equtabÆ12Å= "MOD"; equtabÆ13Å= "NOT"; equtabÆ14Å= "XOR"; equtabÆ15Å= "SHL"; equtabÆ16Å= "SHR"; equcount = 14; å int isidchr(c) /* return true if c is legal character in identifier */ char c; æ return isalpha(c) øø c == '$' øø isdigit(c) øø c == '.'; å int isidstrt(c) /* return true if c is legal as first char of idenfitier */ char c; æ return isalpha(c); å int streq(s1, s2) /* return true if the two strings are equal */ char *s1, *s2; æ if (*s1 != *s2) return 0; /* special case for speed */ while (*s1) if (*s1++ != *s2++) return 0; return (*s2) ? 0 : 1; å skip_wsp(strptr) /* skip white space at *strptr and modify the ptr */ char **strptr; æ while (isspace(**strptr)) (*strptr)++; å strcpy2(s1,s2) /* copy s2 to s1, converting to upper case as we go */ char *s1, *s2; æ while (*s2) *s1++ = toupper(*s2++); *s1 = 'Ø0'; å /* General-purpose string-replacement function: 'string' is pointer to entire string, 'insstr' is pointer to string to be inserted, 'pos' is the position in 'string' where 'insstr' is to be inserted 'lenold' is the length of the substring in 'string' that is being replaced. */ replstr(string, insstr, pos, lenold) char *string, *insstr; æ int length, i, j, k, x; length = strlen(string); x = strlen(insstr); k = x - lenold; i = string + pos + lenold; if (k) movmem(i, i+k, length - (pos + lenold) + 1); for (i = 0, j = pos; i < x; i++, j++) stringÆjÅ = insstrÆiÅ; å error(msg) char *msg; æ printf("ØnØ7%s: %d: %s ",cfilnam,lino,msg); errf = 1; å abort(msg) char *msg; æ error(msg); putchar('Øn'); if (cbufp == incbuf) fclose(incbuf); fclose(fbuf); exit(-1); å sbrk2(n) /* allocate storage and check for out of space condition */ æ int i; if ((i = sbrk(n)) == ERROR) abort("Out of storage allocation spaceØn"); return i; å bumptxtp(str) /* bump txtbufp by size of given string + 1 */ char *str; æ txtbufp += strlen(str) + 1; if (txtbufp >= txtbuf + (TXTBUFSIZE - 8)) abort("Out of text space. Increase TXTBUFSIZE and recompile CASM"); å int norel(id) /* return true if identifier is exempt from relocatetion */ char *id; æ if (isequ(id)) return 1; return 0; å int isequ(str) /* return true if given string is in the EQU table */ char *str; æ int i; for (i = 0; i < equcount; i++) if (streq(str,equtabÆiÅ)) return 1; return 0; å char *isef(str) /* return nflist entry if given string is an external */ char *str; /* function name */ æ int i; for (i = 0; i < nfcount; i++) if (streq(str,nflistÆiÅ)) return nflistÆiÅ; return 0; å «eof»