Advertisement
Guest User

ByteCode.cpp

a guest
May 29th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. //constructor------------------------------------------------------
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. void ByteCode::assign (int type, int index , float value){
  19. if( type == INT){
  20. int val = (int) value;
  21. printf("ldc %d\n", val);
  22. cout<<"istore "<<index<<endl;
  23. }else if(type == FLOAT){
  24. printf("ldc %f\n", value);
  25. cout<<"istore "<<index<<endl;
  26. }
  27. }
  28.  
  29. void ByteCode::arithmticOperation(int type1, int operation, int index1 , int index2, int index3){
  30. if(operation == 0 ){
  31. if(type1 == INT){
  32. cout<<"iload "<<index2<<endl;
  33. cout<<"iload "<<index3<<endl;
  34. cout<<"iadd"<<endl;
  35. cout<<"istore "<<index1;
  36. }else if(type1 == FLOAT){
  37. cout<<"fload "<<index2<<endl;
  38. cout<<"fload "<<index3<<endl;
  39. cout<<"fadd"<<endl;
  40. cout<<"fstore "<<index1;
  41. }
  42. }else if(operation == 1 ){
  43. if(type1 == INT){
  44. cout<<"iload "<<index2<<endl;
  45. cout<<"iload "<<index3<<endl;
  46. cout<<"isub"<<endl;
  47. cout<<"istore "<<index1;
  48. }else if(type1 == FLOAT){
  49. cout<<"fload "<<index2<<endl;
  50. cout<<"fload "<<index3<<endl;
  51. cout<<"fsub"<<endl;
  52. cout<<"fstore "<<index1;
  53. }
  54. }else if(operation == 2 ){
  55. if(type1 == INT){
  56. cout<<"iload "<<index2<<endl;
  57. cout<<"iload "<<index3<<endl;
  58. cout<<"imul"<<endl;
  59. cout<<"istore "<<index1;
  60. }else if(type1 == FLOAT){
  61. cout<<"fload "<<index2<<endl;
  62. cout<<"fload "<<index3<<endl;
  63. cout<<"fmul"<<endl;
  64. cout<<"fstore "<<index1;
  65. }
  66. }else {
  67. if(type1 == INT){
  68. cout<<"iload "<<index2<<endl;
  69. cout<<"iload "<<index3<<endl;
  70. cout<<"idiv"<<endl;
  71. cout<<"istore "<<index1;
  72. }else if(type1 == FLOAT){
  73. cout<<"fload "<<index2<<endl;
  74. cout<<"fload "<<index3<<endl;
  75. cout<<"fdiv"<<endl;
  76. cout<<"fstore "<<index1;
  77. }
  78. }
  79. }
  80.  
  81. //called at the begining of if statement.
  82. void ByteCode::beginIf(bool boolean){
  83. string newLabel = getLabel();
  84.  
  85. if(boolean ==true){
  86. cout<<"ldc 1"<<endl;
  87. cout<<"ifeq "<<newLabel<<endl;
  88. }else{
  89. //if(false)
  90. cout<<"ldc 0"<<endl;
  91. cout<<"ifeq "<<newLabel<<endl;
  92. }
  93. }
  94.  
  95.  
  96. //called at the begining of else.
  97. void ByteCode::beginElse(){
  98. string prevLabel = stack.pop();
  99. string newLabel= getLabel();
  100. cout<<"goto "<<newLabel<<endl;
  101. cout<<prevLabel<<":"<<endl;
  102. }
  103.  
  104. //called when if and (else) hava finished.
  105. void ByteCode::endIF(){
  106. string prevLabel = stack.pop();
  107. cout<<prevLabel<<":"<<endl;
  108. }
  109.  
  110. void ByteCode::beginWhile(bool boolean){
  111. string newLabel = getLabel();
  112. if(boolean =true){
  113. cout<<"ldc 1"<<endl;
  114. cout<<"ifeq "<<newLabel<<endl;
  115. }else{
  116. //if(false)
  117. cout<<"ldc 0"<<endl;
  118. cout<<"ifeq "<<newLabel<<endl;
  119. }
  120. }
  121.  
  122.  
  123. void ByteCode::endWhile(){
  124. string prevLabel = stack.pop();
  125. cout<<prevLabel<<":"<<endl;
  126. }
  127.  
  128. string ByteCode::getLabel(){
  129. string newLabel = label;
  130. label++;
  131. stack.push(newLabel);
  132. return newLabel;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement