1 package gnu.expr; 2 import gnu.bytecode.*; 3 4 public class ProcInitializer extends Initializer 5 { 6 LambdaExp proc; 7 8 public ProcInitializer(LambdaExp lexp, Compilation comp) 9 { 10 field = lexp.allocFieldFor(comp); 11 proc = lexp; 12 LambdaExp heapLambda = lexp.getOwningLambda(); 13 if (heapLambda instanceof ModuleExp && comp.isStatic()) 14 { 15 next = comp.clinitChain; 16 comp.clinitChain = this; 17 } 18 else 19 { 20 next = heapLambda.initChain; 21 heapLambda.initChain = this; 22 } 23 } 24 25 26 public static void emitLoadModuleMethod(LambdaExp proc, Compilation comp) 27 { 28 CodeAttr code = comp.getCode(); 29 ClassType procClass = Compilation.typeModuleMethod; 30 code.emitNew(procClass); 31 code.emitDup(1); 32 LambdaExp owning = proc.getOwningLambda(); 33 if (owning instanceof ClassExp && owning.staticLinkField != null) 34 code.emitLoad(code.getCurrentScope().getVariable(1)); 35 else if (! (owning instanceof ModuleExp) 36 || (comp.moduleClass == comp.mainClass 37 && ! comp.method.getStaticFlag())) 38 code.emitPushThis(); 39 else 40 { 41 if (comp.moduleInstanceVar == null) 42 { 43 comp.moduleInstanceVar 44 = code.locals.current_scope.addVariable(code, 45 comp.moduleClass, 46 "$instance"); 47 if (comp.moduleClass != comp.mainClass && ! comp.isStatic()) 48 { 49 code.emitNew(comp.moduleClass); 50 code.emitDup(comp.moduleClass); 51 code.emitInvokeSpecial(comp.moduleClass.constructor); 52 comp.moduleInstanceMainField = 53 comp.moduleClass.addField("$main", comp.mainClass, 0); 54 code.emitDup(comp.moduleClass); 55 code.emitPushThis(); 56 code.emitPutField(comp.moduleInstanceMainField); 57 } 58 else 59 code.emitGetStatic(comp.moduleInstanceMainField); 60 code.emitStore(comp.moduleInstanceVar); 61 } 62 code.emitLoad(comp.moduleInstanceVar); 63 } 64 code.emitPushInt(proc.getSelectorValue(comp)); 65 comp.compileConstant(proc.nameDecl != null ? proc.nameDecl.getSymbol() 66 : proc.getName(), 67 Target.pushObject); 68 code.emitPushInt(proc.min_args 71 | ((proc.keywords == null ? proc.max_args : -1) << 12)); 72 Method initModuleMethod = procClass.getDeclaredMethod("<init>", 4); 73 code.emitInvokeSpecial(initModuleMethod); 74 75 76 if (proc.properties != null) 77 { 78 int len = proc.properties.length; 79 for (int i = 0; i < len; i += 2) 80 { 81 Object key = proc.properties[i]; 82 if (key != null && key != "name") 84 { 85 Object val = proc.properties[i+1]; 86 code.emitDup(1); 87 comp.compileConstant(key); 88 Target target = Target.pushObject; 89 if (val instanceof Expression) 90 ((Expression) val).compile(comp, target); 91 else 92 comp.compileConstant(val, target); 93 Method m = (ClassType.make("gnu.mapping.PropertySet") 94 .getDeclaredMethod("setProperty", 2)); 95 code.emitInvokeVirtual(m); 96 } 97 } 98 } 99 } 100 101 public void emit(Compilation comp) 102 { 103 CodeAttr code = comp.getCode(); 104 if (! field.getStaticFlag()) 105 code.emitPushThis(); 106 107 emitLoadModuleMethod(proc, comp); 108 109 if (field.getStaticFlag()) 110 code.emitPutStatic(field); 111 else 112 code.emitPutField(field); 113 } 114 115 public void reportError (String message, Compilation comp) 116 { 117 String saveFile = comp.getFileName(); 118 int saveLine = comp.getLineNumber(); 119 int saveColumn = comp.getColumnNumber(); 120 comp.setLocation(proc); 121 String name = proc.getName(); 122 StringBuffer sbuf = new StringBuffer (message); 123 if (name == null) 124 sbuf.append("unnamed procedure"); 125 else 126 { 127 sbuf.append("procedure "); 128 sbuf.append(name); 129 } 130 comp.error('e', sbuf.toString()); 131 comp.setLine(saveFile, saveLine, saveColumn); 132 } 133 } 134 | Popular Tags |