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

📄 sparcgenerator.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
字号:
// $Id: SPARCGenerator.java,v 1.11 2000/10/21 23:18:09 mdeeds Exp $package java6035.tools.ASM;import java.io.*;/** * SPARCGenerator  * * is a map on ASM that generates SPARC assembly code * Use this class to create an assembly listing from your * ASM tree. */ public final class SPARCGenerator {    private PrintWriter output;    private void checkOperand(Object o, int index, String instr)    {		if (o == null)	    	throw new ASMException			("null object supplied as operand "+index+" of "+instr);    }    /**     * Opens a SPARCGenerator output to the file output given.  Call     * gen() with your ASM structure to print it to the file.  Multiple     * calls to gen() will be concatanated.     *     * @param output	Output file stream where the assembly is dumped.     */    public SPARCGenerator(OutputStream output)    {		this.output = new PrintWriter(output);			this.output.print("    ! SPARC assembly: ");		this.output.println		    ("    ! Automatically generated by ASM.SPARCGenerator");		this.output.println("");    }    /**     * closes a SPARCGenerator output.  Use this after your final     * call to gen().     */    public void done()    {		this.output.println("");		this.output.close();    }    /**     * Creates an assembly listing for @param asm, and prints     * it to the output file specified in the constructor.     * Requires that done() has not been called previously.     * call done() exactly once after final call to gen().     **/   	public void gen(ASM asm)    {		for(int asm_iter = 0; asm_iter < asm.size(); asm_iter++)		{		    ASMEntry elm = asm.getEntryAt(asm_iter);	    	if (elm instanceof SPARCInstruction) {	        	SPARCInstruction instr = (SPARCInstruction)elm;	        	int op = instr.getOpcode();	    		        SPARCOperand dest = instr.getDest();		        SPARCOperand src1 = instr.getSrc1();		        SPARCOperand src2 = instr.getSrc2();                String comment = instr.getComment();    		        String d_rep = null, s1_rep = null, s2_rep = null;    		        if (dest != null)		        {				    d_rep = dest.toString();	        	}	    	        	if (src1 != null)		        {				    s1_rep = src1.toString();	        	}	        		        if (src2 != null)		        {				    s2_rep = src2.toString();	        	}    		        switch(op) {	    	        /* all the src1,src2 instructions */	    	        case SPARCOpcode.ST:					case SPARCOpcode.CMP:		    	    	checkOperand(dest,0,SPARCOpcode.getStringRep(op));		    	    	checkOperand(src1,1,SPARCOpcode.getStringRep(op));    						this.output.print("\t"+SPARCOpcode.getStringRep(op)+"\t"+			                    d_rep+", "+s1_rep);											        	break;    				    /* all the label instructions (uses dest operand) */				    case SPARCOpcode.B:					case SPARCOpcode.BA:					case SPARCOpcode.BN:					case SPARCOpcode.BNE:					case SPARCOpcode.BE:					case SPARCOpcode.BG:					case SPARCOpcode.BLE:					case SPARCOpcode.BGE:					case SPARCOpcode.BL:					case SPARCOpcode.BGU:					case SPARCOpcode.BLEU:					case SPARCOpcode.BGEU:					case SPARCOpcode.BLU:					case SPARCOpcode.BPOS:					case SPARCOpcode.BNEG:					case SPARCOpcode.BVC:					case SPARCOpcode.BVS:    		        		checkOperand(dest,0,SPARCOpcode.getStringRep(op));    						this.output.print("\t"+SPARCOpcode.getStringRep(op)			                    +"\t"+d_rep);		        		break;    		    				    		/* special cases */    							/*RET*/										case SPARCOpcode.RET:		        		this.output.print("\tret");												break;											/*RESTORE*/					case SPARCOpcode.RESTORE:						if((src1==null)&&(src2==null)&&(dest==null))						{							this.output.print("\trestore");						}						else						{							checkOperand(src1,1,SPARCOpcode.getStringRep(op));		        			checkOperand(src2,2,SPARCOpcode.getStringRep(op));		        			checkOperand(dest,0,SPARCOpcode.getStringRep(op));    		        			this.output.print("\t"+SPARCOpcode.getStringRep(op)+"\t"+			            			        s1_rep+", "+s2_rep+", "+ d_rep);						}						break;										/* SAVE */					case SPARCOpcode.SAVE:						if((src1==null)&&(src2==null)&&(dest==null))						{							this.output.print("\tsave");						}						else						{							checkOperand(src1,1,SPARCOpcode.getStringRep(op));		        			checkOperand(src2,2,SPARCOpcode.getStringRep(op));		        			checkOperand(dest,0,SPARCOpcode.getStringRep(op));    		        			this.output.print("\t"+SPARCOpcode.getStringRep(op)+"\t"+			            			        s1_rep+", "+s2_rep+", "+ d_rep);						}						break;				    		/* CALL uses symbol */	            	case SPARCOpcode.CALL:							        		checkOperand(dest,0,SPARCOpcode.getStringRep(op));		        								this.output.print("\tcall\t"+d_rep+",0");										break;											/* LD */					case SPARCOpcode.MOV:					case SPARCOpcode.LD:						checkOperand(dest,1,SPARCOpcode.getStringRep(op));		        		checkOperand(src1,0, SPARCOpcode.getStringRep(op));						this.output.print("\t"+SPARCOpcode.getStringRep(op)+"\t"+s1_rep+", "+d_rep);										break;					    		    		/* NOP */	            	case SPARCOpcode.NOP:		        		this.output.print("\tnop");		        		break;    				case SPARCOpcode.SETHI:						this.output.print("\tsethi\t"+s1_rep+", " + d_rep);						break;		    		/* by default: dest,src1,src2 instructions */		    		default:    		        		checkOperand(src1,1,SPARCOpcode.getStringRep(op));		        		checkOperand(src2,2,SPARCOpcode.getStringRep(op));		        		checkOperand(dest,0,SPARCOpcode.getStringRep(op));    		        		this.output.print("\t"+SPARCOpcode.getStringRep(op)+"\t"+			            			        s1_rep+", "+s2_rep+", "+ d_rep);		        		break;	        	}                                // print the comment if there is one                if (comment != null) {                    this.output.println("\t\t/* " + comment + " */");                } else {                    this.output.println("");                }	    	} 	    	else if (elm instanceof ASMLabel) {	        	if (((ASMLabel)elm).toString().equals("main"))	        	{		    		this.output.println("\t.globl main");	        	}	        	this.output.println(((ASMLabel)elm).toString()+":");	    	}						else if (elm instanceof ASMDirective) {				if(elm instanceof dSection)		        	this.output.println(((ASMDirective)elm).toString());				else	        		this.output.println("\t"+((ASMDirective)elm).toString());            }            	else if (elm instanceof ASMComment) {	        	this.output.println(((ASMComment)elm).toString());	    	}            	else if (elm == null) {                // blank line                this.output.println();            }        }    }}

⌨️ 快捷键说明

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