📄 algo.c
字号:
/************************************************************* * File: tools/algo.c * Purpose: The pmcc compiler driver for the Algorithmics toolset * Author: Phil Bunce (pjb@carmel.com) * Revision History: * 970303 Added -ice and -driver options * 970313 Modified mksyms to work with algo's .map file * 970313 Added -ssyms option (separate symbols) * 970314 Removed $LSIPKG refs in crtn pathnames * 970417 Added check for nfiles==0 * 970902 Removed -ice option * 980402 Added support for sde3 * 980720 Added -mips16 * 981123 Fixed crt0 prob. * 990113 Fixed another crt0 prob. * 990119 Added support for .rec extents. */#include <stdio.h>#include <malloc.h>#include <string.h>#include "../include/defines.h"#include "misc.h"#define DYNDEFS/*************************************************************pmcc -- PMON compiler interfaceThis program provides a standardized interface to the Algorithmics toolset.When linking it places the object file in OFILE, the downloadable recordsin OFILE.rec, and the link map in OFILE.map.This program assumes that the environment variable LSIPKG has been set. In addition to the usual cc options, this script supports: -fast - generate fast-format download records (default) -srec - generate S-record download records -syms - omit symbols from the download file -double - use LSI-supplied double-precision math library -chksum - don't generate chksum data in fast records -crt0 - don't include the standard crt0 -prom - append data to text for building promsPlease checkout the pmcc man page for the latest information.*************************************************************/#ifndef TOOLREV#define TOOLREV 10400#endif#ifdef MSDOS#define LINKER "ld"char cmdchar[] = "@";#else /* Unix */#if TOOLREV >= 20000#define LINKER "sde-ld"#else#define LINKER "ld-sde"#endif#endif#define MAXPATH 400/* set various defaults */int vflag = 0; /* verbose flag */int fast = 1;int ssyms_flag;int syms = 1;char *chksum = "";char *ofile = "a";char *initial_lc = "-lc";int driver;char cmdfile[] = "cmdfile";char LIBM[MAXPATH] = "/usr/lib/cmplrs/cc";char tmp[LNMAX],tmp2[LNMAX];char *ENDIAN,*Gnum,*LSIPMCC;char *COMP_HOST_ROOT,*SDEROOT,*TMPDIR;Str flags,cflags,asflags,ldflags,ppflags,ofiles,llist,ilist,LIBS,files;Str *strlst[] = {&flags,&cflags,&asflags,&ldflags,&ppflags,&ofiles, &llist,&ilist,&LIBS,&files,0};char *LSIPKG;char LIBC[MAXPATH];char objfile[MAXPATH];char tstart[MAXPATH];char *dstart;char *CC;int nfiles;char stoppoint;int SZ;char tmppath[MAXPATH],cpppath[MAXPATH];char mname[MAXPATH]; /* module object name */char *clientpc;int toolrev;char *conv;extern int long_cmds_ok;char *getDef();/************************************************************** main(argc,argv)*/main(argc,argv)int argc;char *argv[];{int i,status,SZ,sz;char *e,*p,h[MAXPATH],*redir;char tmp2[MAXPATH];char *vtmp,*fmt,*file,*symsw,*promsw;SZ = 0;if (! (LSIPKG=getenv("LSIPKG"))) { fprintf(stderr,"pmcc: LSIPKG not set\n"); Exit(1); }Strcpy(&LIBS,"");#ifdef DYNDEFSsprintf(tmp,"%s/include/defines.h",LSIPKG);readDefs(tmp);if (getDef("SREC")) fast = 0;if (clientpc=getDef("CLIENTPC")) strcpy(tstart,&clientpc[2]);else strcpy(tstart,"80020000");sscanf(getDef("TOOLREV"),"%d",&toolrev);/* only need this for really old tools */if (toolrev < 20002) strcpy(cpppath,getDef("CPPPATH")); if (toolrev >= 30000) conv = "sde-conv";else conv = "conv-sde";#else#ifdef SRECfast = 0;#endif#ifdef CLIENTPCsprintf(tstart,"%08lx",CLIENTPC);#elsestrcpy(tstart,"80020000");#endifstrcpy(cpppath,CPPPATH);#endif /* !DYNDEFS */#ifdef MSDOSif (! (TMPDIR=getenv("TMPDIR"))) { fprintf(stderr,"pmcc: TMPDIR not set\n"); Exit(1); }p = tmppath + sprintf(tmppath,"%s",TMPDIR);strcat(tmppath,"/PMXXXXXX");mktemp(p);#elseif (! (SDEROOT=getenv("SDEROOT"))) { fprintf(stderr,"pmcc: SDEROOT not set\n"); Exit(1); }strcpy(tmppath,"/tmp/PMXXXXXX");mktemp(&tmppath[5]);#endif/* for backward compatability we set LSIPMCC */if (! (LSIPMCC=getenv("LSIPMCC"))) LSIPMCC = "";/* Select default Endianess. */#ifdef LENDEFENDIAN = "l";#elseENDIAN = "b";#endifGnum = "o";if (toolrev >= 30000) { CC = "sde-gcc"; long_cmds_ok = 1; }else CC = "gcc-sde";stdcrt0 = "%s/lib/%s%s/crt0.o";sprintf(tmp,"-I%s/include",LSIPKG);Strcpy(&ilist,tmp);sprintf(tmp,"-L%s/lib/%%s%%s",LSIPKG);Strcpy(&llist,tmp);sprintf(tmp,"-DPMCC -DALGOR %s",LSIPMCC);Strcpy(&flags,tmp);getargs(argc,argv);if (vflag) addarg(&flags,"-v");if (strequ(ENDIAN,"b")) { addarg(&asflags,"-EB"); addarg(&cflags,"-EB"); }else {#if TOOLREV >= 20000 addarg(&ldflags,"-oformat elf32-littlemips");#else addarg(&ldflags,"-b elf-little");#endif#if !defined(MSDOS) || TOOLREV >= 20000 addarg(&asflags,"-EL"); addarg(&cflags,"-EL");#endif }strcpy(objfile,ofile);if (strequ(ofile,"a")) strcpy(objfile,"a.out");if (stoppoint==0) { addarg(&cflags,"-c"); /* never want the cc driver to call ld */ addarg(&asflags,"-c"); /* never want the cc driver to call ld */ addarg(&ldflags,"-o"); addarg(&ldflags,objfile); }sprintf(tmp,llist.str,ENDIAN,Gnum,ENDIAN,Gnum);addarg(&ldflags,tmp);addarg(&cflags,ilist.str);addarg(&ppflags,ilist.str);addarg(&asflags,"-DLANGUAGE_ASSEMBLY");addarg(&cflags,"-DLANGUAGE_C");if (strequ(ENDIAN,"l")) { addarg(&cflags,"-DMIPSEL"); addarg(&ppflags,"-DMIPSEL"); }else { addarg(&cflags,"-DMIPSEB"); addarg(&ppflags,"-DMIPSEB"); }if (nfiles == 0) { printf("Fatal error: no files specified.\n"); exit(1); } /* now invoke the compiler */for (i=0;i<nfiles;i++) { file = getStrn(&files,i); e = extent(file); getHead(h,file); if (nfiles == 1 && !strequ(ofile,"a") && stoppoint != 0) strcpy(mname,ofile); else sprintf(mname,"%s.o",h); if (strequ(e,"s")) {#if TOOLREV < 20002 /* fix the hash */ sprintf(tmp,"fixhash(%s,%s.t1)",file,tmppath); if (vflag) fprintf(stderr,"%s\n",tmp); sprintf(tmp,"%s.t1",tmppath); fixhash(file,tmp); /* hack to support .struct */ getRoot(tmp2,file); sprintf(tmp,"%s/cpp %s -I%s %s %s %s.t1 %s.t2", cpppath,flags.str,tmp2,ppflags.str,"-DLANGUAGE_ASSEMBLY", tmppath,tmppath); if (status=System(tmp)) Exit(status); sprintf(tmp,"%s.t2",tmppath); sprintf(tmp2,"%s.sx",tmppath); if (vflag) fprintf(stderr,"fixstruct(%s,%s)\n",tmp,tmp2); fixstruct(tmp,tmp2); fmt = "%s -c %s %s %s -o %s %s.sx"; sz = strlen(CC)+strlen(flags.str)+strlen(ppflags.str)+ strlen(asflags.str)+strlen(mname)+strlen(tmppath)+ strlen(fmt); vtmp = malloc(sz); sprintf(vtmp,fmt,CC,flags.str,ppflags.str,asflags.str, mname,tmppath);#else fmt = "%s -c -DLANGUAGE_ASSEMBLY -x assembler-with-cpp -traditional %s %s %s -o %s %s"; sz = strlen(CC)+strlen(flags.str)+strlen(ppflags.str)+ strlen(asflags.str)+strlen(mname)+strlen(file)+ strlen(fmt); vtmp = malloc(sz); sprintf(vtmp,fmt,CC,flags.str,ppflags.str, asflags.str, mname,file);#endif if (status=System(vtmp)) Exit(status); free(vtmp); getHead(tmp,file); strcat(tmp,".o"); addarg(&ofiles,tmp); } else if (strequ(e,"rec")) { fmt = "rdsrec -A dat1 %s -o tmp.s"; sz = strlen(fmt)+strlen(file); vtmp = malloc(sz); sprintf(vtmp,fmt,file); if (status=System(vtmp)) Exit(status); free(vtmp); fmt = "%s -c -DLANGUAGE_ASSEMBLY -x assembler-with-cpp -traditional %s %s %s -o %s tmp.s"; sz = strlen(CC)+strlen(flags.str)+strlen(ppflags.str)+ strlen(asflags.str)+strlen(mname)+strlen(file)+ strlen(fmt); vtmp = malloc(sz); sprintf(vtmp,fmt,CC,flags.str,ppflags.str, asflags.str, mname); if (status=System(vtmp)) Exit(status); free(vtmp); getHead(tmp,file); strcat(tmp,".o"); addarg(&ofiles,tmp); } else if (strequ(e,"c")) { fmt = "%s %s %s -o %s %s"; sz = strlen(CC)+strlen(flags.str)+strlen(cflags.str)+ strlen(mname)+strlen(file)+strlen(fmt); vtmp = malloc(sz); sprintf(vtmp,fmt,CC,flags.str,cflags.str,mname,file); if (status=System(vtmp)) Exit(status); free(vtmp); getHead(tmp,file); strcat(tmp,".o"); addarg(&ofiles,tmp); } else if (strequ(e,"o")) { addarg(&ofiles,file); } else if (strequ(e,"a")) { addarg(&ofiles,file); } else { fprintf(stderr,"pmcc: %s: bad extent\n",e); Exit(1); } free(file); }if (stoppoint != 0) Exit(0);/* Select math library */if (SZ) { sprintf(tmp,"%s/libm/%s/libm.a",LSIPKG,ENDIAN); if (fileExists(tmp)) strcpy(LIBM,tmp); }/* Select C library */sprintf(LIBC,"%s/lib/%s%s",LSIPKG,ENDIAN,Gnum);if (stdcrt0) addcrt0(stdcrt0,LSIPKG,ENDIAN,Gnum);if (crt0.str == 0) Strcpy(&crt0,""); /* -nostdlib */#if TOOLREV >= 20000redir = "-Map";#elseredir = ">";#endifif (dstart) { fmt = "%s -N -M -e _start -Ttext %s -Tdata %s %s %s %s %s %s %s %s.map"; sz = strlen(LINKER)+strlen(tstart)+strlen(dstart)+strlen(ldflags.str)+ Strlen(&crt0)+strlen(ofiles.str)+strlen(initial_lc)+strlen(LIBS.str)+ strlen(redir)+strlen(ofile)+strlen(fmt); vtmp = malloc(sz); sprintf(vtmp,fmt,LINKER,tstart,dstart,ldflags.str,crt0.str,ofiles.str, initial_lc,LIBS.str,redir,ofile); }else { fmt = "%s -N -M -e _start -Ttext %s %s %s %s %s %s %s %s.map"; sz = strlen(LINKER)+strlen(tstart)+strlen(ldflags.str)+ Strlen(&crt0)+strlen(ofiles.str)+strlen(initial_lc)+strlen(LIBS.str)+ strlen(redir)+strlen(ofile)+strlen(fmt); vtmp = malloc(sz); sprintf(vtmp,fmt,LINKER,tstart,ldflags.str,crt0.str,ofiles.str, initial_lc,LIBS.str,redir,ofile); }if (status=System(vtmp)) Exit(status);free(vtmp);if (driver) { fmt = "%s -o %s.tmp %s"; sz = strlen(conv)+strlen(fmt)+strlen(ofile)+ strlen(objfile); vtmp = malloc(sz); sprintf(vtmp,fmt,conv,ofile,objfile); if (status=System(vtmp)) Exit(status); free(vtmp); sprintf(tmp,"%s.tmp",ofile); sprintf(tmp2,"%s.rec",ofile); str2file(tmp2,"SD"); /* write "SD" to ofile.rec */ appendFile(tmp2,tmp); Exit(0); }/* now make the appropriate download records */if (fast) fmt = "%s -f lsi %s %s -o %s.rec %s";else fmt = "%s %s %s -o %s.rec %s";if (syms) symsw = "-y";else symsw = "";if (prom) promsw = "-p";else promsw = "";sz = strlen(fmt)+strlen(conv)+strlen(promsw)+strlen(symsw)+strlen(ofile)+strlen(objfile);vtmp = malloc(sz);sprintf(vtmp,fmt,conv,promsw,symsw,ofile,objfile);if (status=System(vtmp)) Exit(status);free(vtmp);if (ssyms_flag) { sprintf(tmp,"%s.map",ofile); sprintf(tmp2,"%s.sym",ofile); mksyms(tmp,tmp2); }Exit(0);}/************************************************************** getargs(argc,argv)*/getargs(argc,argv)int argc;char *argv[];{int i;char *p;/*# This while loop does several jobs:# 1. strips off any pmcc defined options# 2. some options, if specified, must preceed path specs# so extract them from here, and add them to flags.# 3. find the output filename, if specified*/for (i=1;i < argc;i++) { if (strequ(argv[i],"-mips2")) { addarg(&cflags,argv[i]); addarg(&cflags,"-mcpu=r4000"); addarg(&asflags,argv[i]); addarg(&asflags,"-mcpu=r4000"); } else if (strequ(argv[i],"-mips16")) { addarg(&cflags,argv[i]); addarg(&asflags,argv[i]); mips16 = 1; } else if (strequ(argv[i],"-G")) { i++; addarg(&cflags,"-G"); addarg(&cflags,argv[i]); addarg(&asflags,"-G"); addarg(&asflags,argv[i]); if (strequ(argv[i],"0")) Gnum = "g"; else Gnum = "o"; } else if (!strncmp(argv[i],"-O",2)) { addarg(&cflags,argv[i]); } else if (strequ(argv[i],"-float")) { addarg(&flags,argv[i]); addarg(&flags,"-DFLOAT"); SZ = 1; } else if (getStdArgs(argc,argv,&i)) ; else if (argv[i][0] == '-') { fprintf(stderr,"%s: bad arg\n",argv[i]); exit(1); } else { addarg(&files,argv[i]); nfiles++; } }}/************************************************************** mksyms(char *ifn,char *ofn)*/mksyms(ifn,ofn)char *ifn,*ofn;{FILE *ifp,*ofp;char buf[MAXPATH],*field[10];int nf,flag,csum,len;if (vflag) fprintf(stderr,"mksyms %s > %s\n",ifn,ofn);flag = 0;ifp = fopen(ifn,"r");if (ifp == 0) { fprintf(stderr,"Can't open %s\n",ifn); Exit(1); }ofp = fopen(ofn,"w");if (ofp == 0) { fprintf(stderr,"Can't open %s\n",ofn); Exit(1); }for (;;) { if (!fgets(buf,100,ifp)) break; buf[strlen(buf)-1] = 0; if (!strcmp(buf,"**LINK EDITOR MEMORY MAP**")) flag = 1; if (!flag) continue; if (strlen(buf)==0) continue; if (buf[0] != ' ') continue; nf = argvize(field,buf); if (nf != 2) continue; if (strlen(field[0]) != 8) continue; csum = 0; len = strlen(field[1])+8+2; fprintf(ofp,"S4%02X%s%s,%02x\n",len,field[0],field[1],csum); }fclose(ifp);fclose(ofp);}#ifndef Exit/************************************************************** Exit(x)*/Exit(x)int x;{int i;for (i=0;strlst[i];i++) { if (strlst[i]->str) free(strlst[i]->str); }sprintf(tmp,"%s.t1",tmppath);if (fileExists(tmp)) unlink(tmp);sprintf(tmp,"%s.t2",tmppath);if (fileExists(tmp)) unlink(tmp);sprintf(tmp,"%s.sx",tmppath);if (fileExists(tmp)) unlink(tmp);exit(((x)>127)?1:(x));}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -