⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outas386.c

📁 C编译器 ,简单的一个C语言的编译器。
💻 C
📖 第 1 页 / 共 3 页
字号:
{
			if (prm_asmfile)
        if( gentype == srrefgen && outcol < 56) {
                fprintf(outputFile,",%s,%d",sp->name,val);
                outcol += strlen(sp->name)+1;
                }
        else    {
                nl();
                fprintf(outputFile,"\tDD\t%s,%d",sp->name,val);
                gentype = srrefgen;
                outcol = 25;
                }
}
void genref(SYM *sp,int offset)
/*
 * Output a reference to the data area (also gens fixups )
 */
{       char    sign;
			char buf[40];
        if( offset < 0) {
                sign = '-';
                offset = -offset;
                }
        else
                sign = '+';
			sprintf(buf,"%s%c%d",sp->name,sign,offset);
			if (prm_asmfile) {
        if( gentype == longgen && outcol < 55 - strlen(sp->name)) {
                fprintf(outputFile,",%s",buf);
                outcol += (11 + strlen(sp->name));
                }
        else    {
								if (!newlabel)
									nl();
								else newlabel = FALSE;
                fprintf(outputFile,"\tDD\t%s",buf);
                outcol = 26 + strlen(sp->name);
                gentype = longgen;
                }
			}
}
void genpcref(SYM *sp,int offset)
/*
 * Output a reference to the code area (also gens fixups )
 */
{
	genref(sp,offset);
}
void genstorage(int nbytes)
/*
 * Output bytes of storage
 */
{			if (prm_asmfile) {
								if (!newlabel)
									nl();
								else newlabel = FALSE;
				if (prm_nasm)
        	fprintf(outputFile,"\tRESB\t0%XH",nbytes);
				else
        	fprintf(outputFile,"\tDB\t0%XH DUP (?)",nbytes);
				outcol = 28;
				gentype = storagegen;
			}
}

void gen_labref(int n)
/*
 * Generate a reference to a label
 */
{			if (prm_asmfile)
        if( gentype == longgen && outcol < 58) {
                fprintf(outputFile,",L_%d",n);
                outcol += 6;
                }
        else    {
								if (!newlabel)
									nl();
								else newlabel = FALSE;
                fprintf(outputFile,"\tDD\tL_%d",n);
                outcol = 22;
                gentype = longgen;
                }
}

int     stringlit(char *s, int uselong)
/*
 *      make s a string literal and return it's label number.
 */
{       struct slit     *lp;
        ++global_flag;          /* always allocate from global space. */
        lp = xalloc(sizeof(struct slit));
        lp->label = nextlabel++;
				if (uselong) 
					lp->str = plitlate(s);
				else
        	lp->str = litlate(s);
        lp->next = strtab;
				lp->type = uselong;
        strtab = lp;
        --global_flag;
        return lp->label;
}

void dumplits(void)
/*
 *      dump the string literal pool.
 */
{
        while( strtab != 0) {
                cseg();
                nl();
                put_label(strtab->label);
								genstring(strtab->str,strtab->type);
								if (strtab->type)
									genword(0);
								else
									genbyte(0);
                strtab = strtab->next;
                }
        nl();
}

/*
 * Exit if from a special segment
 */
void exitseg(void)
{
	if (!prm_nasm) {
		if (curseg == startupxseg) {
			curseg = noseg;
			fprintf(outputFile,"cstartup\tENDS\n");
		}
		else if (curseg == rundownxseg) {
			curseg = noseg;
			fprintf(outputFile,"crundown\tENDS\n");
		}
		else if (curseg == cppxseg) {
			curseg = noseg;
			fprintf(outputFile,"cppinit\tENDS\n");
		}
	}
}
/*
 * Switch to cseg 
 */
void cseg(void)
{			if (prm_asmfile)
       	if( curseg != codeseg) {
                nl();
								exitseg();
								if (prm_nasm)
                	fprintf(outputFile,"[SECTION .text]\n");
								else
                	fprintf(outputFile,"\t.CODE\n");
                curseg = codeseg;
                }
}
/*
 * Switch to deseg
 */
void dseg(void)
{     if (prm_asmfile)  
				if( curseg != dataseg) {
                nl();
								exitseg();
								if (prm_nasm)
                	fprintf(outputFile,"[SECTION .data]\n");
								else
                	fprintf(outputFile,"\t.DATA\n");
                curseg = dataseg;
                }
}
/*
 * Switch to bssseg
 */
void bssseg(void)
{     if (prm_asmfile)  
				if( curseg != bssxseg) {
                nl();
								exitseg();
								if (prm_nasm)
                	fprintf(outputFile,"[SECTION .bss]\n");
								else
                	fprintf(outputFile,"\t.DATA?\n");
                curseg = bssxseg;
                }
}
/*
 * Switch to startupseg
 */
void startupseg(void)
{     if (prm_asmfile)  
				if( curseg != startupxseg) {
                nl();
								exitseg();	
								if (prm_nasm)
                	fprintf(outputFile,"[SECTION cstartup]\n");
								else
                	fprintf(outputFile,"cstartup\tSEGMENT USE32 PUBLIC DWORD \042INITDATA\042\n");
                curseg = startupxseg;
                }
}
/*
 * Switch to rundownseg
 */
void rundownseg(void)
{     if (prm_asmfile)  
				if( curseg != rundownxseg) {
                nl();
								exitseg();
								if (prm_nasm)
	                fprintf(outputFile,"[SECTION crundown]\n");
								else
  	              fprintf(outputFile,"crundown\tSEGMENT USE32 PUBLIC DWORD \042EXITDATA\042\n");
                curseg = rundownxseg;
                }
}
void cppseg(void)
{     if (prm_asmfile)  
				if( curseg != cppxseg) {
                nl();
								exitseg();
								if (prm_nasm)
                	fprintf(outputFile,"[SECTION cppinit]\n");
								else
                	fprintf(outputFile,"cppinit\tSEGMENT USE32 PUBLIC DWORD \042CPPDATA\042\n");
                curseg = cppxseg;
                }
}
void gen_virtual(char *name)
{
	if (prm_asmfile) {
		nl();
		fprintf(outputFile,"@%s\tSEGMENT VIRTUAL",name);
	}
}
void gen_endvirtual(char *name)
{
	if (prm_asmfile) {
		nl();
		fprintf(outputFile,"@%s\tENDS",name);
	}
}
/*
 * Align
 */
void align(int size)
{
			if (prm_asmfile) {
				nl();
				if (prm_nasm)
/* NASM 0.91 wouldn't let me use parenthesis but this should work
 * according to the documented precedence levels
 */
					fprintf(outputFile,"\tTIMES $$-$ & %d NOP\n",3);
				else
					fprintf(outputFile,"\tALIGN\t%d\n",4);
			}
}
/* muldiv val init
 */
void init_muldivval(void)
{
	muldivlink = 0;
}
/*
 * queue muldiv val
 */
void queue_muldivval(int label, long number)
{
	MULDIV *p = xalloc(sizeof(MULDIV));
	p->link = muldivlink;
	p->value = number;
	p->label = label;
	p->size = 0;
	muldivlink = p;
}
void queue_floatval(int label, double number, int size)
{
	MULDIV *p = xalloc(sizeof(MULDIV));
	p->link = muldivlink;
	p->floatvalue = number;
	p->label = label;
	p->size = size;
	muldivlink = p;
}
void dump_muldivval(void)
{
	int tag = FALSE;
	if (prm_asmfile) {
		fprintf(outputFile,"\n");
		if (muldivlink) {
			tag = TRUE;
			align(4);
		}
		while (muldivlink) {
			put_label(muldivlink->label);
			if (muldivlink->size == 0)
				fprintf(outputFile,"\tDD\t0%xH\n",muldivlink->value);
			else if (muldivlink->size == 6)
				fprintf(outputFile,"\tDD\t%f\n",muldivlink->floatvalue);
			else if (muldivlink->size == 8)
				fprintf(outputFile,"\tDQ\t%f\n",muldivlink->floatvalue);
			else
				fprintf(outputFile,"\tDT\t%f\n",muldivlink->floatvalue);
			muldivlink = muldivlink->link;
		}
		if (tag)
			fprintf(outputFile,"\n");
	}
}
void asm_header(void)
{
	nl();
	if (prm_nasm)
				fprintf(outputFile,"[BITS 32]\n\n");
	else {
		fprintf(outputFile,"\tTITLE\t'%s'\n",outfile);
		if (prm_flat)
				fprintf(outputFile,"\t.486p\n\t.MODEL FLAT\n\n");
		else
				fprintf(outputFile,"\t.486p\n\t.MODEL SMALL\n\n");
	}
}
void globaldef(SYM *sp)
{
	char buf[100],*q=buf,*p=sp->name;
	if (curseg == codeseg && currentfunc->pascaldefn) {
		if (prm_cmangle)
			p++;
		while(*p)
			*q++=toupper(*p++);
		*q++ = 0;
	}
	else
		strcpy(buf,p);
	if (prm_nasm)
      fprintf(outputFile,"[GLOBAL\t%s]\n",buf);
	else
      fprintf(outputFile,"\tPUBLIC\t%s\n",buf);
}			
void putexterns(void)
/*
 * Output the fixup tables and the global/external list
 */
{       SYM     *sp;
			int i;
			if (prm_asmfile){
						int notyet = TRUE;
				nl();
				exitseg();
				for (i=0; i < HASHTABLESIZE; i++) {
					if ((sp=(SYM *) globalhash[i]) != 0) {
						while (sp) {
    	    		if( sp->storage_class == sc_externalfunc && sp->extflag) {
								char buf[100],*q=buf,*p=sp->name;
								if (curseg == codeseg && sp->pascaldefn) {
									if (prm_cmangle)
										p++;
									while(*p)
										*q++=toupper(*p++);
									*q++ = 0;
								}
								else
									strcpy(buf,p);
								if (prm_nasm) {
							 		if (notyet) {
										fprintf(outputFile,"\n[SECTION .text]\n");
										notyet = FALSE;
									}
      	          fprintf(outputFile,"[EXTERN\t%s]\n",buf);
								}
								else {
							 		if (notyet) {
										fprintf(outputFile,"\n\t.CODE\n");
										notyet = FALSE;
									}
      	          fprintf(outputFile,"\tEXTRN\t%s:PROC\n",buf);
								}
							}
         			sp = sp->next;
						}
					}
				}
				notyet = TRUE;
				for (i=0; i < HASHTABLESIZE; i++) {
					if ((sp=(SYM *) globalhash[i]) != 0) {
						while (sp) {
	        		if( sp->storage_class == sc_external && sp->extflag) {
								char buf[100],*q=buf,*p=sp->name;
								if (curseg == codeseg && sp->pascaldefn) {
									if (prm_cmangle)
										p++;
									while(*p)
										*q++=toupper(*p++);
									*q++ = 0;
								}
								else
									strcpy(buf,p);
							  if (prm_nasm) {
							 		if (notyet) {
										fprintf(outputFile,"\n[SECTION .data]\n");
										notyet = FALSE;
									}
  	              fprintf(outputFile,"[EXTERN\t%s]\n",buf);
							  }
							  else {
									if (notyet) {
										fprintf(outputFile,"\n\t.DATA\n");
										notyet = FALSE;
									}
  	              fprintf(outputFile,"\tEXTRN\t%s\n",buf);
							  }
							}
         			sp = sp->next;
						}
					}
				}
				if (!prm_nasm)
					fprintf(outputFile,"\tEND\n");
			}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -