You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CodeGen.java 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.compiler;
  16. import java.util.ArrayList;
  17. import javassist.compiler.ast.*;
  18. import javassist.bytecode.*;
  19. /* The code generator is implemeted by three files:
  20. * CodeGen.java, MemberCodeGen.java, and JvstCodeGen.
  21. * I just wanted to split a big file into three smaller ones.
  22. */
  23. public abstract class CodeGen extends Visitor implements Opcode, TokenId {
  24. static final String javaLangObject = "java.lang.Object";
  25. static final String jvmJavaLangObject = "java/lang/Object";
  26. static final String javaLangString = "java.lang.String";
  27. static final String jvmJavaLangString = "java/lang/String";
  28. protected Bytecode bytecode;
  29. private int tempVar;
  30. /**
  31. * true if the last visited node is a return statement.
  32. */
  33. protected boolean hasReturned;
  34. /**
  35. * Must be true if compilation is for a static method.
  36. */
  37. public boolean inStaticMethod;
  38. protected ArrayList breakList, continueList;
  39. /* The following fields are used by atXXX() methods
  40. * for returning the type of the compiled expression.
  41. */
  42. protected int exprType; // VOID, NULL, CLASS, BOOLEAN, INT, ...
  43. protected int arrayDim;
  44. protected String className; // JVM-internal representation
  45. public CodeGen(Bytecode b) {
  46. bytecode = b;
  47. tempVar = -1;
  48. hasReturned = false;
  49. inStaticMethod = false;
  50. breakList = null;
  51. continueList = null;
  52. }
  53. protected static void fatal() throws CompileError {
  54. throw new CompileError("fatal");
  55. }
  56. public static boolean is2word(int type, int dim) {
  57. return dim == 0 && (type == DOUBLE || type == LONG);
  58. }
  59. public int getMaxLocals() { return bytecode.getMaxLocals(); }
  60. public void setMaxLocals(int n) {
  61. bytecode.setMaxLocals(n);
  62. }
  63. protected void incMaxLocals(int size) {
  64. bytecode.incMaxLocals(size);
  65. }
  66. /**
  67. * Returns a local variable that single or double words can be
  68. * stored in.
  69. */
  70. protected int getTempVar() {
  71. if (tempVar < 0) {
  72. tempVar = getMaxLocals();
  73. incMaxLocals(2);
  74. }
  75. return tempVar;
  76. }
  77. protected int getLocalVar(Declarator d) {
  78. int v = d.getLocalVar();
  79. if (v < 0) {
  80. v = getMaxLocals(); // delayed variable allocation.
  81. d.setLocalVar(v);
  82. incMaxLocals(1);
  83. }
  84. return v;
  85. }
  86. /**
  87. * Returns the JVM-internal representation of this class name.
  88. */
  89. protected abstract String getThisName();
  90. /**
  91. * Returns the JVM-internal representation of this super class name.
  92. */
  93. protected abstract String getSuperName() throws CompileError;
  94. /* Converts a class name into a JVM-internal representation.
  95. *
  96. * It may also expand a simple class name to java.lang.*.
  97. * For example, this converts Object into java/lang/Object.
  98. */
  99. protected abstract String resolveClassName(ASTList name)
  100. throws CompileError;
  101. /* Expands a simple class name to java.lang.*.
  102. * For example, this converts Object into java/lang/Object.
  103. */
  104. protected abstract String resolveClassName(String jvmClassName)
  105. throws CompileError;
  106. /**
  107. * @param name the JVM-internal representation.
  108. * name is not exapnded to java.lang.*.
  109. */
  110. protected static String toJvmArrayName(String name, int dim) {
  111. if (name == null)
  112. return null;
  113. if (dim == 0)
  114. return name;
  115. else {
  116. StringBuffer sbuf = new StringBuffer();
  117. int d = dim;
  118. while (d-- > 0)
  119. sbuf.append('[');
  120. sbuf.append('L');
  121. sbuf.append(name);
  122. sbuf.append(';');
  123. return sbuf.toString();
  124. }
  125. }
  126. protected static String toJvmTypeName(int type, int dim) {
  127. char c = 'I';
  128. switch(type) {
  129. case BOOLEAN :
  130. c = 'Z';
  131. break;
  132. case BYTE :
  133. c = 'B';
  134. break;
  135. case CHAR :
  136. c = 'C';
  137. break;
  138. case SHORT :
  139. c = 'S';
  140. break;
  141. case INT :
  142. c = 'I';
  143. break;
  144. case LONG :
  145. c = 'J';
  146. break;
  147. case FLOAT :
  148. c = 'F';
  149. break;
  150. case DOUBLE :
  151. c = 'D';
  152. break;
  153. case VOID :
  154. c = 'V';
  155. break;
  156. }
  157. StringBuffer sbuf = new StringBuffer();
  158. while (dim-- > 0)
  159. sbuf.append('[');
  160. sbuf.append(c);
  161. return sbuf.toString();
  162. }
  163. protected static int jvmTypeNameToExprType(char type) {
  164. switch(type) {
  165. case 'Z' :
  166. return BOOLEAN;
  167. case 'B' :
  168. return BYTE;
  169. case 'C' :
  170. return CHAR;
  171. case 'S' :
  172. return SHORT;
  173. case 'I' :
  174. return INT;
  175. case 'J' :
  176. return LONG;
  177. case 'F' :
  178. return FLOAT;
  179. case 'D' :
  180. return DOUBLE;
  181. case 'V' :
  182. return VOID;
  183. default :
  184. return CLASS;
  185. }
  186. }
  187. public void atASTList(ASTList n) throws CompileError { fatal(); }
  188. public void atPair(Pair n) throws CompileError { fatal(); }
  189. public void atSymbol(Symbol n) throws CompileError { fatal(); }
  190. public void atFieldDecl(FieldDecl field) throws CompileError {
  191. field.getInit().accept(this);
  192. }
  193. public void atMethodDecl(MethodDecl method) throws CompileError {
  194. ASTList mods = method.getModifiers();
  195. setMaxLocals(1);
  196. while (mods != null) {
  197. Keyword k = (Keyword)mods.head();
  198. mods = mods.tail();
  199. if (k.get() == STATIC) {
  200. setMaxLocals(0);
  201. inStaticMethod = true;
  202. }
  203. }
  204. ASTList params = method.getParams();
  205. while (params != null) {
  206. atDeclarator((Declarator)params.head());
  207. params = params.tail();
  208. }
  209. Stmnt s = method.getBody();
  210. atMethodBody(s, method.isConstructor(),
  211. method.getReturn().getType() == VOID);
  212. }
  213. public void atMethodBody(Stmnt s, boolean isCons, boolean isVoid)
  214. throws CompileError
  215. {
  216. if (s == null)
  217. return;
  218. if (isCons && needsSuperCall(s))
  219. insertDefaultSuperCall();
  220. hasReturned = false;
  221. s.accept(this);
  222. if (isVoid && !hasReturned) {
  223. bytecode.addOpcode(Opcode.RETURN);
  224. hasReturned = true;
  225. }
  226. }
  227. private boolean needsSuperCall(Stmnt body) {
  228. if (body.getOperator() == BLOCK) {
  229. Stmnt first = (Stmnt)body.head();
  230. if (first != null && first.getOperator() == EXPR) {
  231. ASTree expr = first.head();
  232. if (expr != null && expr instanceof Expr
  233. && ((Expr)expr).getOperator() == CALL) {
  234. ASTree target = ((Expr)expr).head();
  235. if (target instanceof Keyword) {
  236. int token = ((Keyword)target).get();
  237. return token != THIS && token != SUPER;
  238. }
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. protected abstract void insertDefaultSuperCall() throws CompileError;
  245. public void atStmnt(Stmnt st) throws CompileError {
  246. if (st == null)
  247. return; // empty
  248. int op = st.getOperator();
  249. if (op == EXPR) {
  250. ASTree expr = st.getLeft();
  251. if (expr instanceof AssignExpr)
  252. atAssignExpr((AssignExpr)expr, false);
  253. else if (isPlusPlusExpr(expr)) {
  254. Expr e = (Expr)expr;
  255. atPlusPlus(e.getOperator(), e.oprand1(), e, false);
  256. }
  257. else {
  258. expr.accept(this);
  259. if (is2word(exprType, arrayDim))
  260. bytecode.addOpcode(POP2);
  261. else if (exprType != VOID)
  262. bytecode.addOpcode(POP);
  263. }
  264. }
  265. else if (op == DECL || op == BLOCK) {
  266. ASTList list = st;
  267. while (list != null) {
  268. ASTree h = list.head();
  269. list = list.tail();
  270. if (h != null)
  271. h.accept(this);
  272. }
  273. }
  274. else if (op == IF)
  275. atIfStmnt(st);
  276. else if (op == WHILE || op == DO)
  277. atWhileStmnt(st, op == WHILE);
  278. else if (op == FOR)
  279. atForStmnt(st);
  280. else if (op == BREAK || op == CONTINUE)
  281. atBreakStmnt(st, op == BREAK);
  282. else if (op == TokenId.RETURN)
  283. atReturnStmnt(st);
  284. else if (op == THROW)
  285. atThrowStmnt(st);
  286. else if (op == TRY)
  287. atTryStmnt(st);
  288. else {
  289. // LABEL, SWITCH label stament might be null?.
  290. hasReturned = false;
  291. throw new CompileError(
  292. "sorry, not supported statement: TokenId " + op);
  293. }
  294. }
  295. private void atIfStmnt(Stmnt st) throws CompileError {
  296. ASTree expr = st.head();
  297. Stmnt thenp = (Stmnt)st.tail().head();
  298. Stmnt elsep = (Stmnt)st.tail().tail().head();
  299. booleanExpr(false, expr);
  300. int pc = bytecode.currentPc();
  301. int pc2 = 0;
  302. bytecode.addIndex(0); // correct later
  303. hasReturned = false;
  304. if (thenp != null)
  305. thenp.accept(this);
  306. boolean thenHasReturned = hasReturned;
  307. hasReturned = false;
  308. if (elsep != null && !thenHasReturned) {
  309. bytecode.addOpcode(Opcode.GOTO);
  310. pc2 = bytecode.currentPc();
  311. bytecode.addIndex(0);
  312. }
  313. bytecode.write16bit(pc, bytecode.currentPc() - pc + 1);
  314. if (elsep != null) {
  315. elsep.accept(this);
  316. if (!thenHasReturned)
  317. bytecode.write16bit(pc2, bytecode.currentPc() - pc2 + 1);
  318. hasReturned = thenHasReturned && hasReturned;
  319. }
  320. }
  321. private void atWhileStmnt(Stmnt st, boolean notDo) throws CompileError {
  322. ArrayList prevBreakList = breakList;
  323. ArrayList prevContList = continueList;
  324. breakList = new ArrayList();
  325. continueList = new ArrayList();
  326. ASTree expr = st.head();
  327. Stmnt body = (Stmnt)st.tail();
  328. int pc = 0;
  329. if (notDo) {
  330. bytecode.addOpcode(Opcode.GOTO);
  331. pc = bytecode.currentPc();
  332. bytecode.addIndex(0);
  333. }
  334. int pc2 = bytecode.currentPc();
  335. if (body != null)
  336. body.accept(this);
  337. int pc3 = bytecode.currentPc();
  338. if (notDo)
  339. bytecode.write16bit(pc, pc3 - pc + 1);
  340. booleanExpr(true, expr);
  341. bytecode.addIndex(pc2 - bytecode.currentPc() + 1);
  342. patchGoto(breakList, bytecode.currentPc());
  343. patchGoto(continueList, pc3);
  344. continueList = prevContList;
  345. breakList = prevBreakList;
  346. hasReturned = false;
  347. }
  348. private void patchGoto(ArrayList list, int targetPc) {
  349. int n = list.size();
  350. for (int i = 0; i < n; ++i) {
  351. int pc = ((Integer)list.get(i)).intValue();
  352. bytecode.write16bit(pc, targetPc - pc + 1);
  353. }
  354. }
  355. private void atForStmnt(Stmnt st) throws CompileError {
  356. ArrayList prevBreakList = breakList;
  357. ArrayList prevContList = continueList;
  358. breakList = new ArrayList();
  359. continueList = new ArrayList();
  360. Stmnt init = (Stmnt)st.head();
  361. ASTList p = st.tail();
  362. ASTree expr = p.head();
  363. p = p.tail();
  364. Stmnt update = (Stmnt)p.head();
  365. Stmnt body = (Stmnt)p.tail();
  366. if (init != null)
  367. init.accept(this);
  368. int pc = bytecode.currentPc();
  369. int pc2 = 0;
  370. if (expr != null) {
  371. booleanExpr(false, expr);
  372. pc2 = bytecode.currentPc();
  373. bytecode.addIndex(0);
  374. }
  375. if (body != null)
  376. body.accept(this);
  377. int pc3 = bytecode.currentPc();
  378. if (update != null)
  379. update.accept(this);
  380. bytecode.addOpcode(Opcode.GOTO);
  381. bytecode.addIndex(pc - bytecode.currentPc() + 1);
  382. int pc4 = bytecode.currentPc();
  383. if (expr != null)
  384. bytecode.write16bit(pc2, pc4 - pc2 + 1);
  385. patchGoto(breakList, pc4);
  386. patchGoto(continueList, pc3);
  387. continueList = prevContList;
  388. breakList = prevBreakList;
  389. hasReturned = false;
  390. }
  391. private void atBreakStmnt(Stmnt st, boolean notCont)
  392. throws CompileError
  393. {
  394. if (st.head() != null)
  395. throw new CompileError(
  396. "sorry, not support labeled break or continue");
  397. bytecode.addOpcode(Opcode.GOTO);
  398. Integer pc = new Integer(bytecode.currentPc());
  399. bytecode.addIndex(0);
  400. if (notCont)
  401. breakList.add(pc);
  402. else
  403. continueList.add(pc);
  404. }
  405. protected void atReturnStmnt(Stmnt st) throws CompileError {
  406. atReturnStmnt2(st.getLeft());
  407. }
  408. protected final void atReturnStmnt2(ASTree result) throws CompileError {
  409. int op;
  410. if (result == null)
  411. op = Opcode.RETURN;
  412. else {
  413. result.accept(this);
  414. if (arrayDim > 0)
  415. op = ARETURN;
  416. else {
  417. int type = exprType;
  418. if (type == DOUBLE)
  419. op = DRETURN;
  420. else if (type == FLOAT)
  421. op = FRETURN;
  422. else if (type == LONG)
  423. op = LRETURN;
  424. else if (isRefType(type))
  425. op = ARETURN;
  426. else
  427. op = IRETURN;
  428. }
  429. }
  430. bytecode.addOpcode(op);
  431. hasReturned = true;
  432. }
  433. private void atThrowStmnt(Stmnt st) throws CompileError {
  434. ASTree e = st.getLeft();
  435. e.accept(this);
  436. if (exprType != CLASS || arrayDim > 0)
  437. throw new CompileError("bad throw statement");
  438. bytecode.addOpcode(ATHROW);
  439. hasReturned = true;
  440. }
  441. protected void atTryStmnt(Stmnt st) throws CompileError {
  442. hasReturned = false;
  443. }
  444. private static boolean isPlusPlusExpr(ASTree expr) {
  445. if (expr instanceof Expr) {
  446. int op = ((Expr)expr).getOperator();
  447. return op == PLUSPLUS || op == MINUSMINUS;
  448. }
  449. return false;
  450. }
  451. public void atDeclarator(Declarator d) throws CompileError {
  452. d.setLocalVar(getMaxLocals());
  453. d.setClassName(resolveClassName(d.getClassName()));
  454. int size;
  455. if (is2word(d.getType(), d.getArrayDim()))
  456. size = 2;
  457. else
  458. size = 1;
  459. incMaxLocals(size);
  460. /* NOTE: Array initializers has not been supported.
  461. */
  462. ASTree init = d.getInitializer();
  463. if (init != null)
  464. atVariableAssign(null, '=', null, d, init, false);
  465. }
  466. public abstract void atNewExpr(NewExpr n) throws CompileError;
  467. public void atAssignExpr(AssignExpr expr) throws CompileError {
  468. atAssignExpr(expr, true);
  469. }
  470. protected void atAssignExpr(AssignExpr expr, boolean doDup)
  471. throws CompileError
  472. {
  473. // =, %=, &=, *=, /=, +=, -=, ^=, |=, <<=, >>=, >>>=
  474. int op = expr.getOperator();
  475. ASTree left = expr.oprand1();
  476. ASTree right = expr.oprand2();
  477. if (left instanceof Variable)
  478. atVariableAssign(expr, op, (Variable)left,
  479. ((Variable)left).getDeclarator(),
  480. right, doDup);
  481. else {
  482. if (left instanceof Expr) {
  483. Expr e = (Expr)left;
  484. if (e.getOperator() == ARRAY) {
  485. atArrayAssign(expr, op, (Expr)left, right, doDup);
  486. return;
  487. }
  488. }
  489. atFieldAssign(expr, op, left, right, doDup);
  490. }
  491. }
  492. protected static void badAssign(Expr expr) throws CompileError {
  493. String msg;
  494. if (expr == null)
  495. msg = "incompatible type for assignment";
  496. else
  497. msg = "incompatible type for " + expr.getName();
  498. throw new CompileError(msg);
  499. }
  500. /* op is either =, %=, &=, *=, /=, +=, -=, ^=, |=, <<=, >>=, or >>>=.
  501. *
  502. * expr and var can be null.
  503. */
  504. private void atVariableAssign(Expr expr, int op, Variable var,
  505. Declarator d, ASTree right,
  506. boolean doDup) throws CompileError
  507. {
  508. int varType = d.getType();
  509. int varArray = d.getArrayDim();
  510. String varClass = d.getClassName();
  511. int varNo = getLocalVar(d);
  512. if (op != '=')
  513. atVariable(var);
  514. atAssignCore(expr, op, right, varType, varArray, varClass);
  515. if (doDup)
  516. if (is2word(varType, varArray))
  517. bytecode.addOpcode(DUP2);
  518. else
  519. bytecode.addOpcode(DUP);
  520. if (varArray > 0)
  521. bytecode.addAstore(varNo);
  522. else if (varType == DOUBLE)
  523. bytecode.addDstore(varNo);
  524. else if (varType == FLOAT)
  525. bytecode.addFstore(varNo);
  526. else if (varType == LONG)
  527. bytecode.addLstore(varNo);
  528. else if (isRefType(varType))
  529. bytecode.addAstore(varNo);
  530. else
  531. bytecode.addIstore(varNo);
  532. exprType = varType;
  533. arrayDim = varArray;
  534. className = varClass;
  535. }
  536. private void atArrayAssign(Expr expr, int op, Expr array,
  537. ASTree right, boolean doDup) throws CompileError
  538. {
  539. arrayAccess(array.oprand1(), array.oprand2());
  540. if (op != '=') {
  541. bytecode.addOpcode(DUP2);
  542. bytecode.addOpcode(getArrayReadOp(exprType, arrayDim));
  543. }
  544. int aType = exprType;
  545. int aDim = arrayDim;
  546. String cname = className;
  547. atAssignCore(expr, op, right, aType, aDim, cname);
  548. if (doDup)
  549. if (is2word(aType, aDim))
  550. bytecode.addOpcode(DUP2_X2);
  551. else
  552. bytecode.addOpcode(DUP_X2);
  553. bytecode.addOpcode(getArrayWriteOp(aType, aDim));
  554. exprType = aType;
  555. arrayDim = aDim;
  556. className = cname;
  557. }
  558. protected abstract void atFieldAssign(Expr expr, int op, ASTree left,
  559. ASTree right, boolean doDup) throws CompileError;
  560. protected void atAssignCore(Expr expr, int op, ASTree right,
  561. int type, int dim, String cname)
  562. throws CompileError
  563. {
  564. right.accept(this);
  565. if (invalidDim(exprType, arrayDim, className, type, dim, cname, false)
  566. || (op != '=' && dim > 0))
  567. badAssign(expr);
  568. if (op == PLUS_E && dim == 0 && type == CLASS)
  569. atStringConcatExpr(expr, type, dim, cname);
  570. else if (op != '=') {
  571. int token = assignOps[op - MOD_E];
  572. int k = lookupBinOp(token);
  573. if (k < 0)
  574. fatal();
  575. atArithBinExpr(expr, token, k, type);
  576. }
  577. if (op != '=' || (dim == 0 && !isRefType(type)))
  578. atNumCastExpr(exprType, type);
  579. // type check should be done here.
  580. }
  581. private boolean invalidDim(int srcType, int srcDim, String srcClass,
  582. int destType, int destDim, String destClass,
  583. boolean isCast)
  584. {
  585. if (srcDim != destDim)
  586. if (srcType == NULL)
  587. return false;
  588. else if (destDim == 0 && destType == CLASS
  589. && jvmJavaLangObject.equals(destClass))
  590. return false;
  591. else if (isCast && srcDim == 0 && srcType == CLASS
  592. && jvmJavaLangObject.equals(srcClass))
  593. return false;
  594. else
  595. return true;
  596. return false;
  597. }
  598. public void atCondExpr(CondExpr expr) throws CompileError {
  599. booleanExpr(false, expr.condExpr());
  600. int pc = bytecode.currentPc();
  601. bytecode.addIndex(0); // correct later
  602. expr.thenExpr().accept(this);
  603. bytecode.addOpcode(Opcode.GOTO);
  604. int pc2 = bytecode.currentPc();
  605. bytecode.addIndex(0);
  606. bytecode.write16bit(pc, bytecode.currentPc() - pc + 1);
  607. expr.elseExpr().accept(this);
  608. bytecode.write16bit(pc2, bytecode.currentPc() - pc2 + 1);
  609. }
  610. private final int[] binOp = {
  611. '+', DADD, FADD, LADD, IADD,
  612. '-', DSUB, FSUB, LSUB, ISUB,
  613. '*', DMUL, FMUL, LMUL, IMUL,
  614. '/', DDIV, FDIV, LDIV, IDIV,
  615. '%', DREM, FREM, LREM, IREM,
  616. '|', NOP, NOP, LOR, IOR,
  617. '^', NOP, NOP, LXOR, IXOR,
  618. '&', NOP, NOP, LAND, IAND,
  619. LSHIFT, NOP, NOP, LSHL, ISHL,
  620. RSHIFT, NOP, NOP, LSHR, ISHR,
  621. ARSHIFT, NOP, NOP, LUSHR, IUSHR };
  622. private int lookupBinOp(int token) {
  623. int[] code = binOp;
  624. int s = code.length;
  625. for (int k = 0; k < s; k = k + 5)
  626. if (code[k] == token)
  627. return k;
  628. return -1;
  629. }
  630. public void atBinExpr(BinExpr expr) throws CompileError {
  631. int token = expr.getOperator();
  632. /* arithmetic operators: +, -, *, /, %, |, ^, &, <<, >>, >>>
  633. */
  634. int k = lookupBinOp(token);
  635. if (k >= 0) {
  636. expr.oprand1().accept(this);
  637. int type1 = exprType;
  638. int dim1 = arrayDim;
  639. String cname1 = className;
  640. expr.oprand2().accept(this);
  641. if (dim1 != arrayDim)
  642. throw new CompileError("incompatible array types");
  643. if (token == '+' && dim1 == 0
  644. && (type1 == CLASS || exprType == CLASS))
  645. atStringConcatExpr(expr, type1, dim1, cname1);
  646. else
  647. atArithBinExpr(expr, token, k, type1);
  648. return;
  649. }
  650. /* equation: &&, ||, ==, !=, <=, >=, <, >
  651. */
  652. booleanExpr(true, expr);
  653. bytecode.addIndex(7);
  654. bytecode.addIconst(0); // false
  655. bytecode.addOpcode(Opcode.GOTO);
  656. bytecode.addIndex(4);
  657. bytecode.addIconst(1); // true
  658. }
  659. /* arrayDim values of the two oprands must be equal.
  660. * If an oprand type is not a numeric type, this method
  661. * throws an exception.
  662. */
  663. private void atArithBinExpr(Expr expr, int token,
  664. int index, int type1) throws CompileError
  665. {
  666. if (arrayDim != 0)
  667. badTypes(expr);
  668. int type2 = exprType;
  669. if (token == LSHIFT || token == RSHIFT || token == ARSHIFT)
  670. if (type2 == INT || type2 == SHORT
  671. || type2 == CHAR || type2 == BYTE)
  672. exprType = type1;
  673. else
  674. badTypes(expr);
  675. else
  676. convertOprandTypes(type1, type2, expr);
  677. int p = typePrecedence(exprType);
  678. if (p >= 0) {
  679. int op = binOp[index + p + 1];
  680. if (op != NOP) {
  681. if (p == P_INT)
  682. exprType = INT; // type1 may be BYTE, ...
  683. bytecode.addOpcode(op);
  684. return;
  685. }
  686. }
  687. badTypes(expr);
  688. }
  689. private void atStringConcatExpr(Expr expr, int type1, int dim1,
  690. String cname1) throws CompileError
  691. {
  692. int type2 = exprType;
  693. int dim2 = arrayDim;
  694. boolean type2Is2 = is2word(type2, dim2);
  695. boolean type2IsString
  696. = (type2 == CLASS && jvmJavaLangString.equals(className));
  697. if (type2Is2)
  698. convToString(type2, dim2);
  699. if (is2word(type1, dim1)) {
  700. bytecode.addOpcode(DUP_X2);
  701. bytecode.addOpcode(POP);
  702. }
  703. else
  704. bytecode.addOpcode(SWAP);
  705. convToString(type1, dim1);
  706. bytecode.addOpcode(SWAP);
  707. if (!type2Is2 && !type2IsString)
  708. convToString(type2, dim2);
  709. bytecode.addInvokevirtual(javaLangString, "concat",
  710. "(Ljava/lang/String;)Ljava/lang/String;");
  711. exprType = CLASS;
  712. arrayDim = 0;
  713. className = jvmJavaLangString;
  714. }
  715. private void convToString(int type, int dim) throws CompileError {
  716. final String method = "valueOf";
  717. if (isRefType(type) || dim > 0)
  718. bytecode.addInvokestatic(javaLangString, method,
  719. "(Ljava/lang/Object;)Ljava/lang/String;");
  720. else if (type == DOUBLE)
  721. bytecode.addInvokestatic(javaLangString, method,
  722. "(D)Ljava/lang/String;");
  723. else if (type == FLOAT)
  724. bytecode.addInvokestatic(javaLangString, method,
  725. "(F)Ljava/lang/String;");
  726. else if (type == LONG)
  727. bytecode.addInvokestatic(javaLangString, method,
  728. "(J)Ljava/lang/String;");
  729. else if (type == BOOLEAN)
  730. bytecode.addInvokestatic(javaLangString, method,
  731. "(Z)Ljava/lang/String;");
  732. else if (type == CHAR)
  733. bytecode.addInvokestatic(javaLangString, method,
  734. "(C)Ljava/lang/String;");
  735. else if (type == VOID)
  736. throw new CompileError("void type expression");
  737. else /* INT, BYTE, SHORT */
  738. bytecode.addInvokestatic(javaLangString, method,
  739. "(I)Ljava/lang/String;");
  740. }
  741. /* Produces the opcode to branch if the condition is true.
  742. * The oprand is not produced.
  743. *
  744. * If false is returned, the branch occurs if the condition is
  745. * false.
  746. */
  747. private void booleanExpr(boolean branchIf, ASTree expr)
  748. throws CompileError
  749. {
  750. boolean isAndAnd;
  751. int op = getCompOperator(expr);
  752. if (op == EQ) { // ==, !=, ...
  753. BinExpr bexpr = (BinExpr)expr;
  754. int type1 = compileOprands(bexpr);
  755. compareExpr(branchIf, bexpr.getOperator(), type1, bexpr);
  756. }
  757. else if (op == '!')
  758. booleanExpr(!branchIf, ((Expr)expr).oprand1());
  759. else if ((isAndAnd = (op == ANDAND)) || op == OROR) {
  760. BinExpr bexpr = (BinExpr)expr;
  761. booleanExpr(!isAndAnd, bexpr.oprand1());
  762. int pc = bytecode.currentPc();
  763. bytecode.addIndex(0); // correct later
  764. booleanExpr(isAndAnd, bexpr.oprand2());
  765. bytecode.write16bit(pc, bytecode.currentPc() - pc + 3);
  766. if (branchIf != isAndAnd) {
  767. bytecode.addIndex(6); // skip GOTO instruction
  768. bytecode.addOpcode(Opcode.GOTO);
  769. }
  770. }
  771. else { // others
  772. expr.accept(this);
  773. bytecode.addOpcode(branchIf ? IFNE : IFEQ);
  774. }
  775. exprType = BOOLEAN;
  776. arrayDim = 0;
  777. }
  778. private static int getCompOperator(ASTree expr) throws CompileError {
  779. if (expr instanceof Expr) {
  780. Expr bexpr = (Expr)expr;
  781. int token = bexpr.getOperator();
  782. if (token == '!')
  783. return '!';
  784. else if ((bexpr instanceof BinExpr)
  785. && token != OROR && token != ANDAND
  786. && token != '&' && token != '|')
  787. return EQ; // ==, !=, ...
  788. else
  789. return token;
  790. }
  791. return ' '; // others
  792. }
  793. private int compileOprands(BinExpr expr) throws CompileError {
  794. expr.oprand1().accept(this);
  795. int type1 = exprType;
  796. int dim1 = arrayDim;
  797. expr.oprand2().accept(this);
  798. if (dim1 != arrayDim)
  799. throw new CompileError("incompatible array types");
  800. return type1;
  801. }
  802. private final int ifOp[] = { EQ, IF_ICMPEQ, IF_ICMPNE,
  803. NEQ, IF_ICMPNE, IF_ICMPEQ,
  804. LE, IF_ICMPLE, IF_ICMPGT,
  805. GE, IF_ICMPGE, IF_ICMPLT,
  806. '<', IF_ICMPLT, IF_ICMPGE,
  807. '>', IF_ICMPGT, IF_ICMPLE };
  808. private final int ifOp2[] = { EQ, IFEQ, IFNE,
  809. NEQ, IFNE, IFEQ,
  810. LE, IFLE, IFGT,
  811. GE, IFGE, IFLT,
  812. '<', IFLT, IFGE,
  813. '>', IFGT, IFLE };
  814. /* Produces the opcode to branch if the condition is true.
  815. * The oprands are not produced.
  816. *
  817. * Parameter expr - compare expression ==, !=, <=, >=, <, >
  818. */
  819. private void compareExpr(boolean branchIf,
  820. int token, int type1, BinExpr expr)
  821. throws CompileError
  822. {
  823. if (arrayDim == 0)
  824. convertOprandTypes(type1, exprType, expr);
  825. int p = typePrecedence(exprType);
  826. if (p == P_OTHER || arrayDim > 0)
  827. if (token == EQ)
  828. bytecode.addOpcode(branchIf ? IF_ACMPEQ : IF_ACMPNE);
  829. else if (token == NEQ)
  830. bytecode.addOpcode(branchIf ? IF_ACMPNE : IF_ACMPEQ);
  831. else
  832. badTypes(expr);
  833. else
  834. if (p == P_INT) {
  835. int op[] = ifOp;
  836. for (int i = 0; i < op.length; i += 3)
  837. if (op[i] == token) {
  838. bytecode.addOpcode(op[i + (branchIf ? 1 : 2)]);
  839. return;
  840. }
  841. badTypes(expr);
  842. }
  843. else {
  844. if (p == P_DOUBLE)
  845. if (token == '<' || token == LE)
  846. bytecode.addOpcode(DCMPG);
  847. else
  848. bytecode.addOpcode(DCMPL);
  849. else if (p == P_FLOAT)
  850. if (token == '<' || token == LE)
  851. bytecode.addOpcode(FCMPG);
  852. else
  853. bytecode.addOpcode(FCMPL);
  854. else if (p == P_LONG)
  855. bytecode.addOpcode(LCMP); // 1: >, 0: =, -1: <
  856. else
  857. fatal();
  858. int[] op = ifOp2;
  859. for (int i = 0; i < op.length; i += 3)
  860. if (op[i] == token) {
  861. bytecode.addOpcode(op[i + (branchIf ? 1 : 2)]);
  862. return;
  863. }
  864. badTypes(expr);
  865. }
  866. }
  867. protected static void badTypes(Expr expr) throws CompileError {
  868. throw new CompileError("invalid types for " + expr.getName());
  869. }
  870. private static final int P_DOUBLE = 0;
  871. private static final int P_FLOAT = 1;
  872. private static final int P_LONG = 2;
  873. private static final int P_INT = 3;
  874. private static final int P_OTHER = -1;
  875. protected static boolean isRefType(int type) {
  876. return type == CLASS || type == NULL;
  877. }
  878. private static int typePrecedence(int type) {
  879. if (type == DOUBLE)
  880. return P_DOUBLE;
  881. else if (type == FLOAT)
  882. return P_FLOAT;
  883. else if (type == LONG)
  884. return P_LONG;
  885. else if (isRefType(type))
  886. return P_OTHER;
  887. else if (type == VOID)
  888. return P_OTHER; // this is wrong, but ...
  889. else
  890. return P_INT; // BOOLEAN, BYTE, CHAR, SHORT, INT
  891. }
  892. private static final int[] castOp = {
  893. /* D F L I */
  894. /* double */ NOP, D2F, D2L, D2I,
  895. /* float */ F2D, NOP, F2L, F2I,
  896. /* long */ L2D, L2F, NOP, L2I,
  897. /* other */ I2D, I2F, I2L, NOP };
  898. /* do implicit type conversion.
  899. * arrayDim values of the two oprands must be zero.
  900. */
  901. private void convertOprandTypes(int type1, int type2, Expr expr)
  902. throws CompileError
  903. {
  904. boolean rightStrong;
  905. int type1_p = typePrecedence(type1);
  906. int type2_p = typePrecedence(type2);
  907. if (type2_p < 0 && type1_p < 0) // not primitive types
  908. return;
  909. if (type2_p < 0 || type1_p < 0) // either is not a primitive type
  910. badTypes(expr);
  911. int op, result_type;
  912. if (type1_p <= type2_p) {
  913. rightStrong = false;
  914. exprType = type1;
  915. op = castOp[type2_p * 4 + type1_p];
  916. result_type = type1_p;
  917. }
  918. else {
  919. rightStrong = true;
  920. op = castOp[type1_p * 4 + type2_p];
  921. result_type = type2_p;
  922. }
  923. if (rightStrong) {
  924. if (result_type == P_DOUBLE || result_type == P_LONG) {
  925. if (type1_p == P_DOUBLE || type1_p == P_LONG)
  926. bytecode.addOpcode(DUP2_X2);
  927. else
  928. bytecode.addOpcode(DUP2_X1);
  929. bytecode.addOpcode(POP2);
  930. bytecode.addOpcode(op);
  931. bytecode.addOpcode(DUP2_X2);
  932. bytecode.addOpcode(POP2);
  933. }
  934. else if (result_type == P_FLOAT) {
  935. bytecode.addOpcode(SWAP);
  936. bytecode.addOpcode(op);
  937. bytecode.addOpcode(SWAP);
  938. }
  939. else
  940. fatal();
  941. }
  942. else if (op != NOP)
  943. bytecode.addOpcode(op);
  944. }
  945. public void atCastExpr(CastExpr expr) throws CompileError {
  946. String cname = resolveClassName(expr.getClassName());
  947. String toClass = checkCastExpr(expr, cname);
  948. int srcType = exprType;
  949. exprType = expr.getType();
  950. arrayDim = expr.getArrayDim();
  951. className = cname;
  952. if (toClass == null)
  953. atNumCastExpr(srcType, exprType); // built-in type
  954. else
  955. bytecode.addCheckcast(toClass);
  956. }
  957. public void atInstanceOfExpr(InstanceOfExpr expr) throws CompileError {
  958. String cname = resolveClassName(expr.getClassName());
  959. String toClass = checkCastExpr(expr, cname);
  960. bytecode.addInstanceof(toClass);
  961. exprType = BOOLEAN;
  962. arrayDim = 0;
  963. }
  964. private String checkCastExpr(CastExpr expr, String name)
  965. throws CompileError
  966. {
  967. final String msg = "invalid cast";
  968. ASTree oprand = expr.getOprand();
  969. int dim = expr.getArrayDim();
  970. int type = expr.getType();
  971. oprand.accept(this);
  972. int srcType = exprType;
  973. if (invalidDim(srcType, arrayDim, className, type, dim, name, true)
  974. || srcType == VOID || type == VOID)
  975. throw new CompileError(msg);
  976. if (type == CLASS) {
  977. if (!isRefType(srcType))
  978. throw new CompileError(msg);
  979. return toJvmArrayName(name, dim);
  980. }
  981. else
  982. if (dim > 0)
  983. return toJvmTypeName(type, dim);
  984. else
  985. return null; // built-in type
  986. }
  987. void atNumCastExpr(int srcType, int destType)
  988. throws CompileError
  989. {
  990. if (srcType == destType)
  991. return;
  992. int op, op2;
  993. int stype = typePrecedence(srcType);
  994. int dtype = typePrecedence(destType);
  995. if (0 <= stype && stype < 3)
  996. op = castOp[stype * 4 + dtype];
  997. else
  998. op = NOP;
  999. if (destType == DOUBLE)
  1000. op2 = I2D;
  1001. else if (destType == FLOAT)
  1002. op2 = I2F;
  1003. else if (destType == LONG)
  1004. op2 = I2L;
  1005. else if (destType == SHORT)
  1006. op2 = I2S;
  1007. else if (destType == CHAR)
  1008. op2 = I2C;
  1009. else if (destType == BYTE)
  1010. op2 = I2B;
  1011. else
  1012. op2 = NOP;
  1013. if (op != NOP)
  1014. bytecode.addOpcode(op);
  1015. if (op == NOP || op == L2I || op == F2I || op == D2I)
  1016. if (op2 != NOP)
  1017. bytecode.addOpcode(op2);
  1018. }
  1019. public void atExpr(Expr expr) throws CompileError {
  1020. // method call, array access, member access,
  1021. // (unary) +, (unary) -, ++, --, !, ~
  1022. int token = expr.getOperator();
  1023. ASTree oprand = expr.oprand1();
  1024. if (token == CALL) // method call
  1025. atMethodCall(expr);
  1026. else if (token == '.')
  1027. if (((Symbol)expr.oprand2()).get().equals("length"))
  1028. atArrayLength(expr);
  1029. else
  1030. atFieldRead(expr);
  1031. else if (token == MEMBER) { // field read
  1032. if (!atClassObject(expr)) // .class
  1033. atFieldRead(expr);
  1034. }
  1035. else if (token == ARRAY)
  1036. atArrayRead(oprand, expr.oprand2());
  1037. else if (token == PLUSPLUS || token == MINUSMINUS)
  1038. atPlusPlus(token, oprand, expr, true);
  1039. else if (token == '!') {
  1040. booleanExpr(false, expr);
  1041. bytecode.addIndex(7);
  1042. bytecode.addIconst(1);
  1043. bytecode.addOpcode(Opcode.GOTO);
  1044. bytecode.addIndex(4);
  1045. bytecode.addIconst(0);
  1046. }
  1047. else {
  1048. expr.oprand1().accept(this);
  1049. int type = typePrecedence(exprType);
  1050. if (arrayDim > 0)
  1051. badType(expr);
  1052. if (token == '-') {
  1053. if (type == P_DOUBLE)
  1054. bytecode.addOpcode(DNEG);
  1055. else if (type == P_FLOAT)
  1056. bytecode.addOpcode(FNEG);
  1057. else if (type == P_LONG)
  1058. bytecode.addOpcode(LNEG);
  1059. else if (type == P_INT) {
  1060. bytecode.addOpcode(INEG);
  1061. exprType = INT; // type may be BYTE, ...
  1062. }
  1063. else
  1064. badType(expr);
  1065. }
  1066. else if (token == '~') {
  1067. if (type == P_INT) {
  1068. bytecode.addIconst(-1);
  1069. bytecode.addOpcode(IXOR);
  1070. exprType = INT; // type may be BYTE. ...
  1071. }
  1072. else if (type == P_LONG) {
  1073. bytecode.addLconst(-1);
  1074. bytecode.addOpcode(LXOR);
  1075. }
  1076. else
  1077. badType(expr);
  1078. }
  1079. else if (token == '+') {
  1080. if (type == P_OTHER)
  1081. badType(expr);
  1082. // do nothing. ignore.
  1083. }
  1084. else
  1085. fatal();
  1086. }
  1087. }
  1088. protected static void badType(Expr expr) throws CompileError {
  1089. throw new CompileError("invalid type for " + expr.getName());
  1090. }
  1091. protected abstract void atMethodCall(Expr expr) throws CompileError;
  1092. protected abstract void atFieldRead(ASTree expr) throws CompileError;
  1093. public boolean atClassObject(Expr expr) throws CompileError {
  1094. if (!((Symbol)expr.oprand2()).get().equals("class"))
  1095. return false;
  1096. if (resolveClassName((ASTList)expr.oprand1()) == null)
  1097. return false;
  1098. throw new CompileError(".class is not supported");
  1099. }
  1100. public void atArrayLength(Expr expr) throws CompileError {
  1101. expr.oprand1().accept(this);
  1102. if (arrayDim == 0)
  1103. throw new CompileError(".length applied to a non array");
  1104. bytecode.addOpcode(ARRAYLENGTH);
  1105. exprType = INT;
  1106. arrayDim = 0;
  1107. }
  1108. public void atArrayRead(ASTree array, ASTree index)
  1109. throws CompileError
  1110. {
  1111. int op;
  1112. arrayAccess(array, index);
  1113. bytecode.addOpcode(getArrayReadOp(exprType, arrayDim));
  1114. }
  1115. protected void arrayAccess(ASTree array, ASTree index)
  1116. throws CompileError
  1117. {
  1118. array.accept(this);
  1119. int type = exprType;
  1120. int dim = arrayDim;
  1121. if (dim == 0)
  1122. throw new CompileError("bad array access");
  1123. String cname = className;
  1124. index.accept(this);
  1125. if (typePrecedence(exprType) != P_INT || arrayDim > 0)
  1126. throw new CompileError("bad array index");
  1127. exprType = type;
  1128. arrayDim = dim - 1;
  1129. className = cname;
  1130. }
  1131. protected static int getArrayReadOp(int type, int dim) {
  1132. int op;
  1133. if (dim > 0)
  1134. return AALOAD;
  1135. switch (type) {
  1136. case DOUBLE :
  1137. return DALOAD;
  1138. case FLOAT :
  1139. return FALOAD;
  1140. case LONG :
  1141. return LALOAD;
  1142. case INT :
  1143. return IALOAD;
  1144. case SHORT :
  1145. return SALOAD;
  1146. case CHAR :
  1147. return CALOAD;
  1148. case BYTE :
  1149. case BOOLEAN :
  1150. return BALOAD;
  1151. default :
  1152. return AALOAD;
  1153. }
  1154. }
  1155. protected static int getArrayWriteOp(int type, int dim) {
  1156. int op;
  1157. if (dim > 0)
  1158. return AASTORE;
  1159. switch (type) {
  1160. case DOUBLE :
  1161. return DASTORE;
  1162. case FLOAT :
  1163. return FASTORE;
  1164. case LONG :
  1165. return LASTORE;
  1166. case INT :
  1167. return IASTORE;
  1168. case CHAR :
  1169. return CASTORE;
  1170. case BYTE :
  1171. case BOOLEAN :
  1172. return BASTORE;
  1173. default :
  1174. return AASTORE;
  1175. }
  1176. }
  1177. private void atPlusPlus(int token, ASTree oprand, Expr expr,
  1178. boolean doDup) throws CompileError
  1179. {
  1180. boolean isPost = oprand == null; // ++i or i++?
  1181. if (isPost)
  1182. oprand = expr.oprand2();
  1183. if (oprand instanceof Variable) {
  1184. Declarator d = ((Variable)oprand).getDeclarator();
  1185. int t = exprType = d.getType();
  1186. arrayDim = d.getArrayDim();
  1187. int var = getLocalVar(d);
  1188. if (arrayDim > 0)
  1189. badType(expr);
  1190. if (t == DOUBLE) {
  1191. bytecode.addDload(var);
  1192. if (doDup && isPost)
  1193. bytecode.addOpcode(DUP2);
  1194. bytecode.addDconst(1.0);
  1195. bytecode.addOpcode(token == PLUSPLUS ? DADD : DSUB);
  1196. if (doDup && !isPost)
  1197. bytecode.addOpcode(DUP2);
  1198. bytecode.addDstore(var);
  1199. }
  1200. else if (t == LONG) {
  1201. bytecode.addLload(var);
  1202. if (doDup && isPost)
  1203. bytecode.addOpcode(DUP2);
  1204. bytecode.addLconst((long)1);
  1205. bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB);
  1206. if (doDup && !isPost)
  1207. bytecode.addOpcode(DUP2);
  1208. bytecode.addLstore(var);
  1209. }
  1210. else if (t == FLOAT) {
  1211. bytecode.addFload(var);
  1212. if (doDup && isPost)
  1213. bytecode.addOpcode(DUP);
  1214. bytecode.addFconst(1.0f);
  1215. bytecode.addOpcode(token == PLUSPLUS ? FADD : FSUB);
  1216. if (doDup && !isPost)
  1217. bytecode.addOpcode(DUP);
  1218. bytecode.addFstore(var);
  1219. }
  1220. else if (t == BYTE || t == CHAR || t == SHORT || t == INT) {
  1221. if (doDup && isPost)
  1222. bytecode.addIload(var);
  1223. bytecode.addOpcode(IINC);
  1224. bytecode.add(var);
  1225. bytecode.add(token == PLUSPLUS ? 1 : -1);
  1226. if (doDup && !isPost)
  1227. bytecode.addIload(var);
  1228. }
  1229. else
  1230. badType(expr);
  1231. }
  1232. else {
  1233. if (oprand instanceof Expr) {
  1234. Expr e = (Expr)oprand;
  1235. if (e.getOperator() == ARRAY) {
  1236. atArrayPlusPlus(token, isPost, e, doDup);
  1237. return;
  1238. }
  1239. }
  1240. atFieldPlusPlus(token, isPost, oprand, expr, doDup);
  1241. }
  1242. }
  1243. public void atArrayPlusPlus(int token, boolean isPost,
  1244. Expr expr, boolean doDup) throws CompileError
  1245. {
  1246. arrayAccess(expr.oprand1(), expr.oprand2());
  1247. int t = exprType;
  1248. int dim = arrayDim;
  1249. if (dim > 0)
  1250. badType(expr);
  1251. bytecode.addOpcode(DUP2);
  1252. bytecode.addOpcode(getArrayReadOp(t, arrayDim));
  1253. int dup_code = is2word(t, dim) ? DUP2_X2 : DUP_X2;
  1254. atPlusPlusCore(dup_code, doDup, token, isPost, expr);
  1255. bytecode.addOpcode(getArrayWriteOp(t, dim));
  1256. }
  1257. protected void atPlusPlusCore(int dup_code, boolean doDup,
  1258. int token, boolean isPost,
  1259. Expr expr) throws CompileError
  1260. {
  1261. int t = exprType;
  1262. if (doDup && isPost)
  1263. bytecode.addOpcode(dup_code);
  1264. if (t == INT || t == BYTE || t == CHAR || t == SHORT) {
  1265. bytecode.addIconst(1);
  1266. bytecode.addOpcode(token == PLUSPLUS ? IADD : ISUB);
  1267. exprType = INT;
  1268. }
  1269. else if (t == LONG) {
  1270. bytecode.addLconst((long)1);
  1271. bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB);
  1272. }
  1273. else if (t == FLOAT) {
  1274. bytecode.addFconst(1.0f);
  1275. bytecode.addOpcode(token == PLUSPLUS ? FADD : FSUB);
  1276. }
  1277. else if (t == DOUBLE) {
  1278. bytecode.addDconst(1.0);
  1279. bytecode.addOpcode(token == PLUSPLUS ? DADD : DSUB);
  1280. }
  1281. else
  1282. badType(expr);
  1283. if (doDup && !isPost)
  1284. bytecode.addOpcode(dup_code);
  1285. }
  1286. protected abstract void atFieldPlusPlus(int token, boolean isPost,
  1287. ASTree oprand, Expr expr, boolean doDup) throws CompileError;
  1288. public abstract void atMember(Member n) throws CompileError;
  1289. public void atVariable(Variable v) throws CompileError {
  1290. Declarator d = v.getDeclarator();
  1291. exprType = d.getType();
  1292. arrayDim = d.getArrayDim();
  1293. className = d.getClassName();
  1294. int var = getLocalVar(d);
  1295. if (arrayDim > 0)
  1296. bytecode.addAload(var);
  1297. else
  1298. switch (exprType) {
  1299. case CLASS :
  1300. bytecode.addAload(var);
  1301. break;
  1302. case LONG :
  1303. bytecode.addLload(var);
  1304. break;
  1305. case FLOAT :
  1306. bytecode.addFload(var);
  1307. break;
  1308. case DOUBLE :
  1309. bytecode.addDload(var);
  1310. break;
  1311. default : // BOOLEAN, BYTE, CHAR, SHORT, INT
  1312. bytecode.addIload(var);
  1313. break;
  1314. }
  1315. }
  1316. public void atKeyword(Keyword k) throws CompileError {
  1317. arrayDim = 0;
  1318. int token = k.get();
  1319. switch (token) {
  1320. case TRUE :
  1321. bytecode.addIconst(1);
  1322. exprType = BOOLEAN;
  1323. break;
  1324. case FALSE :
  1325. bytecode.addIconst(0);
  1326. exprType = BOOLEAN;
  1327. break;
  1328. case NULL :
  1329. bytecode.addOpcode(ACONST_NULL);
  1330. exprType = NULL;
  1331. break;
  1332. case THIS :
  1333. case SUPER :
  1334. if (inStaticMethod)
  1335. throw new CompileError("not-available: "
  1336. + (token == THIS ? "this" : "super"));
  1337. bytecode.addAload(0);
  1338. exprType = CLASS;
  1339. if (token == THIS)
  1340. className = getThisName();
  1341. else
  1342. className = getSuperName();
  1343. break;
  1344. default :
  1345. fatal();
  1346. }
  1347. }
  1348. public void atStringL(StringL s) throws CompileError {
  1349. exprType = CLASS;
  1350. arrayDim = 0;
  1351. className = "java/lang/String";
  1352. bytecode.addLdc(s.get());
  1353. }
  1354. public void atIntConst(IntConst i) throws CompileError {
  1355. arrayDim = 0;
  1356. long value = i.get();
  1357. int type = i.getType();
  1358. if (type == IntConstant || type == CharConstant) {
  1359. exprType = (type == IntConstant ? INT : CHAR);
  1360. bytecode.addIconst((int)value);
  1361. }
  1362. else {
  1363. exprType = LONG;
  1364. bytecode.addLconst(value);
  1365. }
  1366. }
  1367. public void atDoubleConst(DoubleConst d) throws CompileError {
  1368. arrayDim = 0;
  1369. if (d.getType() == DoubleConstant) {
  1370. exprType = DOUBLE;
  1371. bytecode.addDconst(d.get());
  1372. }
  1373. else {
  1374. exprType = FLOAT;
  1375. bytecode.addFconst((float)d.get());
  1376. }
  1377. }
  1378. }