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

📄 asmdemo.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
字号:
// $Id: ASMDemo.java,v 1.5 2000/10/21 23:18:09 mdeeds Exp $package java6035.tools.ASM;import java.io.*;public class ASMDemo{    /**     * Runs the ASMDemo.     * This creates an Assembly tree, and writes it to the     * filename specified on the command line.     **/  public static void main(String[] args)   {    if (args.length != 1) {	System.out.println("usage: java ASMDemo filename");	return;    }    try {	/*	 * For the heck of it, let's build an ASM file.	 */ 	ASM asm = new ASM();	ASMDemo asmd = new ASMDemo();	asmd.genInit(asm);	asmd.genMain(asm);	asmd.genFibRec(asm);		FileOutputStream out = new FileOutputStream(args[0]);	SPARCGenerator sparcgen = 	    new SPARCGenerator(out);	/*	 * Generate SPARC asm file	 */	sparcgen.gen(asm);	sparcgen.done();	/*	 * Close file	 */	out.close();    } catch (Exception e) {	System.out.println(e);	e.printStackTrace();    }  }  	private void genInit(ASM asm)  	{  		asm.appendEntry(new dFile("any_file_name"));		asm.appendEntry(new dSection(".rodata"));		asm.appendEntry(new dAlign(8));						asm.appendEntry(new ASMLabel(".Scanf_label"));		asm.appendEntry(new dAsciz("%d"));		asm.appendEntry(new dAlign(8));				asm.appendEntry(new ASMLabel(".Printf_label1"));		asm.appendEntry(new dAsciz("fib_rec(%d) = %d\n"));		asm.appendEntry(new dAlign(8));		asm.appendEntry(new ASMLabel(".Printf_label2"));		asm.appendEntry(new dAsciz("fib_loop(%d) = %d\n"));			asm.appendEntry(new dSection(".text"));		asm.appendEntry(new dAlign(4));		asm.appendEntry(new dGlobal("main"));	}    	private void genMain(ASM asm)	{  		asm.appendEntry(new ASMLabel("main"));		asm.appendEntry(SPARCOpcode.save_instr(SPARCRegister.sp,											   -120,											   SPARCRegister.sp));		asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.fp,											  new SPARCImmed(-20),											  SPARCRegister.o1));		asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(new ASMLabel(".Scanf_label")),												SPARCRegister.o2));		asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o2, new SPARCLoOp(new ASMLabel(".Scanf_label")), SPARCRegister.o0));				asm.appendEntry(SPARCOpcode.call_instr("scanf"));		asm.appendEntry(SPARCOpcode.nop_instr());				asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.fp,-20), SPARCRegister.o0));		asm.appendEntry(SPARCOpcode.call_instr("fib_rec"));		asm.appendEntry(SPARCOpcode.nop_instr());				asm.appendEntry(SPARCOpcode.mov_instr(SPARCRegister.o0, SPARCRegister.o2));		asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(new ASMLabel(".Printf_label1")), SPARCRegister.o1));		asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o1, new SPARCLoOp(new ASMLabel(".Printf_label1")), SPARCRegister.o0));		asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.fp, -20), SPARCRegister.o1));		asm.appendEntry(SPARCOpcode.call_instr("printf"));		asm.appendEntry(SPARCOpcode.nop_instr());						asm.appendEntry(SPARCOpcode.ret_instr());		asm.appendEntry(SPARCOpcode.restore_instr());	}  	  	private void genFibRec(ASM asm)  	{  		asm.appendEntry(new ASMLabel("fib_rec"));		asm.appendEntry(SPARCOpcode.save_instr(SPARCRegister.sp,											   -112,											   SPARCRegister.sp));		asm.appendEntry(SPARCOpcode.st_instr(  SPARCRegister.i0,												new SPARCAddress(SPARCRegister.fp,																68)));																		asm.appendEntry(SPARCOpcode.ld_instr(  new SPARCAddress(SPARCRegister.fp,																68),												SPARCRegister.o0));														asm.appendEntry(SPARCOpcode.cmp_instr(  SPARCRegister.o0,												new SPARCImmed(1)));		asm.appendEntry(SPARCOpcode.bne_instr(".LL4"));		asm.appendEntry(SPARCOpcode.nop_instr());		asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(1), 											  SPARCRegister.i0));		asm.appendEntry(SPARCOpcode.b_instr(".LL3"));														asm.appendEntry(SPARCOpcode.nop_instr());		asm.appendEntry(SPARCOpcode.b_instr(".LL5"));														asm.appendEntry(SPARCOpcode.nop_instr());		asm.appendEntry(new ASMLabel(".LL4"));												  		asm.appendEntry(SPARCOpcode.ld_instr(  new SPARCAddress(SPARCRegister.fp,																68),												SPARCRegister.o0));														asm.appendEntry(SPARCOpcode.cmp_instr(  SPARCRegister.o0,												new SPARCImmed(0)));		asm.appendEntry(SPARCOpcode.bne_instr(".LL6"));				asm.appendEntry(SPARCOpcode.nop_instr());		asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(0), 											  SPARCRegister.i0));		asm.appendEntry(SPARCOpcode.b_instr(".LL3"));														asm.appendEntry(SPARCOpcode.nop_instr());		asm.appendEntry(SPARCOpcode.b_instr(".LL5"));														asm.appendEntry(SPARCOpcode.nop_instr());																	asm.appendEntry(new ASMLabel(".LL6"));												  						asm.appendEntry(SPARCOpcode.ld_instr(  new SPARCAddress(SPARCRegister.fp,																68),												SPARCRegister.o0));			asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.o0,											  new SPARCImmed(-1),											  SPARCRegister.o1));		asm.appendEntry(SPARCOpcode.mov_instr(SPARCRegister.o1, 											  SPARCRegister.o0));		asm.appendEntry(SPARCOpcode.call_instr("fib_rec"));		asm.appendEntry(SPARCOpcode.nop_instr());		asm.appendEntry(SPARCOpcode.mov_instr(SPARCRegister.o0, 											  SPARCRegister.l0));		asm.appendEntry(SPARCOpcode.ld_instr(  new SPARCAddress(SPARCRegister.fp,																68),												SPARCRegister.o0));					asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.o0,											  new SPARCImmed(-2),											  SPARCRegister.o1));						asm.appendEntry(SPARCOpcode.mov_instr(SPARCRegister.o1, 											  SPARCRegister.o0));											  		asm.appendEntry(SPARCOpcode.call_instr("fib_rec"));		asm.appendEntry(SPARCOpcode.nop_instr());				asm.appendEntry(SPARCOpcode.mov_instr(SPARCRegister.o0, 											  SPARCRegister.o1));		asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.l0,											  SPARCRegister.o1,											  SPARCRegister.o0));		asm.appendEntry(SPARCOpcode.mov_instr(SPARCRegister.o0, 											  SPARCRegister.i0));											  		asm.appendEntry(SPARCOpcode.b_instr(".LL3"));														asm.appendEntry(SPARCOpcode.nop_instr());				asm.appendEntry(new ASMLabel(".LL7"));												  		asm.appendEntry(new ASMLabel(".LL5"));			asm.appendEntry(new ASMLabel(".LL3"));			asm.appendEntry(SPARCOpcode.ret_instr());		asm.appendEntry(SPARCOpcode.restore_instr());  	}}

⌨️ 快捷键说明

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