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

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