Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //constructor------------------------------------------------------
- void ByteCode::assign (int type, int index , float value){
- if( type == INT){
- int val = (int) value;
- printf("ldc %d\n", val);
- cout<<"istore "<<index<<endl;
- }else if(type == FLOAT){
- printf("ldc %f\n", value);
- cout<<"istore "<<index<<endl;
- }
- }
- void ByteCode::arithmticOperation(int type1, int operation, int index1 , int index2, int index3){
- if(operation == 0 ){
- if(type1 == INT){
- cout<<"iload "<<index2<<endl;
- cout<<"iload "<<index3<<endl;
- cout<<"iadd"<<endl;
- cout<<"istore "<<index1;
- }else if(type1 == FLOAT){
- cout<<"fload "<<index2<<endl;
- cout<<"fload "<<index3<<endl;
- cout<<"fadd"<<endl;
- cout<<"fstore "<<index1;
- }
- }else if(operation == 1 ){
- if(type1 == INT){
- cout<<"iload "<<index2<<endl;
- cout<<"iload "<<index3<<endl;
- cout<<"isub"<<endl;
- cout<<"istore "<<index1;
- }else if(type1 == FLOAT){
- cout<<"fload "<<index2<<endl;
- cout<<"fload "<<index3<<endl;
- cout<<"fsub"<<endl;
- cout<<"fstore "<<index1;
- }
- }else if(operation == 2 ){
- if(type1 == INT){
- cout<<"iload "<<index2<<endl;
- cout<<"iload "<<index3<<endl;
- cout<<"imul"<<endl;
- cout<<"istore "<<index1;
- }else if(type1 == FLOAT){
- cout<<"fload "<<index2<<endl;
- cout<<"fload "<<index3<<endl;
- cout<<"fmul"<<endl;
- cout<<"fstore "<<index1;
- }
- }else {
- if(type1 == INT){
- cout<<"iload "<<index2<<endl;
- cout<<"iload "<<index3<<endl;
- cout<<"idiv"<<endl;
- cout<<"istore "<<index1;
- }else if(type1 == FLOAT){
- cout<<"fload "<<index2<<endl;
- cout<<"fload "<<index3<<endl;
- cout<<"fdiv"<<endl;
- cout<<"fstore "<<index1;
- }
- }
- }
- //called at the begining of if statement.
- void ByteCode::beginIf(bool boolean){
- string newLabel = getLabel();
- if(boolean ==true){
- cout<<"ldc 1"<<endl;
- cout<<"ifeq "<<newLabel<<endl;
- }else{
- //if(false)
- cout<<"ldc 0"<<endl;
- cout<<"ifeq "<<newLabel<<endl;
- }
- }
- //called at the begining of else.
- void ByteCode::beginElse(){
- string prevLabel = stack.pop();
- string newLabel= getLabel();
- cout<<"goto "<<newLabel<<endl;
- cout<<prevLabel<<":"<<endl;
- }
- //called when if and (else) hava finished.
- void ByteCode::endIF(){
- string prevLabel = stack.pop();
- cout<<prevLabel<<":"<<endl;
- }
- void ByteCode::beginWhile(bool boolean){
- string newLabel = getLabel();
- if(boolean =true){
- cout<<"ldc 1"<<endl;
- cout<<"ifeq "<<newLabel<<endl;
- }else{
- //if(false)
- cout<<"ldc 0"<<endl;
- cout<<"ifeq "<<newLabel<<endl;
- }
- }
- void ByteCode::endWhile(){
- string prevLabel = stack.pop();
- cout<<prevLabel<<":"<<endl;
- }
- string ByteCode::getLabel(){
- string newLabel = label;
- label++;
- stack.push(newLabel);
- return newLabel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement