xeye_ atsu / Mbed 2 deprecated FRDM_RaVem_JVM

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <stdio.h>
00002 #include <string.h>
00003 
00004 #include "ravem.h"
00005 
00006 ///* Main Program */
00007 int main (void) {
00008     
00009     hardware_init();    
00010     uart_init(9600);
00011     bc_init();
00012     pool_init();
00013     
00014     uart_print("LPC11U24 mbed JVM Start\r\n");
00015     uart_print("please wait...\r\n\r\n");
00016 
00017     int32_t thread_count = 0,lp,method_all_end;
00018     // Initialize Operand Stack and local Register  
00019     class_st *p;
00020 
00021     p = (class_st *)pool_alloc(sizeof(class_st) * 1);
00022     if(p == NULL)uart_print("error!");
00023     p[0] = seekCodeArrtibute(p[0], "main", 4);  //main_attribute
00024     p[0].field_mem_reg = NULL;
00025     p[0].field_num = 0;
00026     p[0].local_reg = (int *)pool_alloc(sizeof(int) * p[0].local_num);
00027     p[0].op_stack_type = (int *)pool_alloc(sizeof(int) * p[0].stack_num);
00028     p[0].op_stack = (int *)pool_alloc(sizeof(int) * p[0].stack_num);    
00029     p[0].op_stack_type[0] = 0;
00030     p[0].threadCommand = Thread_Active;
00031     p[0].myThreadNum   = 0;
00032     
00033     
00034     while(1){
00035         for(lp = 0 ; lp < thread_count + 1 ;lp++){
00036             if((p[lp].threadCommand == Thread_Active)||
00037             (p[lp].threadCommand == Thread_inSleep)){
00038                 p[lp] = decodeVM(p[lp]);
00039             }
00040         }           
00041         switch(p[0].threadCommand){
00042         case Thread_getInitMethodWithStack:
00043             thread_count++;
00044         
00045             p = (class_st *)pool_realloc((int *)p,sizeof(class_st)*(1 + thread_count));
00046             if(p == NULL)uart_print("error!");  
00047             p[thread_count] = seekCodeArrtibute(p[thread_count], "<init>", 6);  // init
00048             p[thread_count].field_mem_reg = NULL;
00049             p[thread_count].field_num = 0;
00050             p[thread_count].local_reg = (int *)pool_alloc(sizeof(int) * p[thread_count].local_num);
00051             p[thread_count].op_stack_type = (int *)pool_alloc(sizeof(int) * p[thread_count].stack_num);
00052             p[thread_count].op_stack = (int *)pool_alloc(sizeof(int) * p[thread_count].stack_num);  
00053             p[thread_count].op_stack_type[0] = 0;           
00054             p[thread_count].threadCommand = Thread_init;
00055             
00056             // copy stack -> local variable
00057             for(lp = p[0].threadArg+1; lp != 0 ; lp--){
00058                 p[thread_count].local_reg[lp] = getIntegerFromOperandStack(p[0]);
00059             }
00060                         
00061             while(p[thread_count].threadCommand != Thread_returned){
00062                 p[thread_count] = decodeVM(p[thread_count]);
00063             }
00064             
00065             p[thread_count].threadCommand = Thread_initIsDone;
00066             p[0].threadCommand = Thread_Active;    
00067             break;
00068             
00069         case Thread_getStartMethod:
00070             for(lp = 0 ; lp <= thread_count + 1 ; lp++){
00071                 if(p[lp].threadCommand == Thread_initIsDone)break;
00072             }
00073             if(p[lp].threadCommand != Thread_initIsDone){
00074                 thread_count++;
00075                 p = (class_st *)pool_realloc((int *)p,sizeof(class_st)*(1+thread_count));
00076                 if(p == NULL)uart_print("error!");  
00077                 lp = thread_count;
00078                 
00079                 p[lp] = seekCodeArrtibute(p[lp], "run", 3); // run method(start() calls this method)
00080                         
00081                 p[lp].local_reg = (int *)pool_alloc(sizeof(int) * p[lp].local_num);
00082                 if(p[lp].local_reg == NULL)uart_print("error!");
00083                 p[lp].op_stack_type = (int *)pool_alloc(sizeof(int) * p[lp].stack_num);
00084                 if(p[lp].op_stack_type == NULL)uart_print("error!");
00085                 p[lp].op_stack = (int *)pool_alloc( sizeof(int) * p[lp].stack_num);
00086                 if(p[lp].op_stack == NULL)uart_print("error!");             
00087                 p[lp].op_stack_type[0] = 0;
00088                 p[lp].threadCommand = Thread_Active;
00089                 p[lp].myThreadNum = lp;
00090                 p[0].threadCommand = Thread_Active;
00091                 break;
00092             }
00093                     
00094             p[lp] = seekCodeArrtibute(p[lp], "run", 3); // run method(start() calls this method)
00095             if(p[lp].local_reg == NULL)uart_print("pointr error!\r\n");     
00096             p[lp].local_reg = (int *)pool_realloc(p[lp].local_reg, sizeof(int) * p[lp].local_num);
00097             if(p[lp].local_reg == NULL)uart_print("pointr error!\r\n");
00098             p[lp].op_stack_type = (int *)pool_realloc(p[lp].op_stack_type,sizeof(int) * p[lp].stack_num);
00099             if(p[lp].op_stack_type == NULL)uart_print("pointr error!\r\n");
00100             p[lp].op_stack = (int *)pool_realloc(p[lp].op_stack, sizeof(int) * p[lp].stack_num);
00101             if(p[lp].op_stack == NULL)uart_print("pointr error!\r\n");          
00102             p[lp].op_stack_type[0] = 0;
00103             p[lp].threadCommand = Thread_Active;
00104             p[lp].myThreadNum = lp;
00105             p[0].threadCommand = Thread_Active;
00106             
00107             break;              
00108         }
00109         method_all_end = 0;
00110         for(lp = 0 ; lp < thread_count + 1 ; lp++){
00111             if(p[lp].threadCommand != Thread_returned) method_all_end = method_all_end + 1;
00112         }
00113         if(method_all_end == 0) break;
00114     }
00115     
00116     // end
00117     for(lp = 0; lp < thread_count + 1; lp++){   
00118         pool_free(p[lp].local_reg);
00119         pool_free(p[lp].op_stack_type);
00120         pool_free(p[lp].op_stack);
00121     }
00122     pool_free((int *)&p);
00123     
00124     p = NULL;
00125     
00126     uart_print("\r\n\r\nJVM Fin\r\n");
00127     while(1);
00128     
00129 }