Компилятор написан на Java: реализация оптимизатора глазка

Error: User Rate Limit Exceeded

Peephole optimizer specification

Error: User Rate Limit Exceeded

The Encoder makes a call to an emit() function every time a machine instruction is to be generated. emit(Instruction currentInstr) checks a table of peephole optimizations: If the current instruction matches the tail of a pattern: Check previously emitted instructions for matching If all instructions matched the pattern, apply the optimization, modifying the tail end of the code store If no optimization was found, emit the instruction as usual Current design approach

Error: User Rate Limit ExceededInstructionError: User Rate Limit ExceededInstructionMatchError: User Rate Limit Exceededequals(Instruction instr)Error: User Rate Limit ExceededtrueError: User Rate Limit Exceededinstr.

Error: User Rate Limit ExceededapplyError: User Rate Limit Exceeded

Alternate approaches

Error: User Rate Limit Exceeded

Example patterns, pattern syntax
x: JUMP x+1; x+1: JUMP y  -->  x: JUMP y
LOADL x; LOADL y; add     -->  LOADL x+y
LOADA d[r]; STOREI (n)    -->  STORE (n) d[r]

Error: User Rate Limit Exceeded

op_code register n d

Error: User Rate Limit Exceededx: <instr>Error: User Rate Limit ExceededxError: User Rate Limit Exceeded

Error: User Rate Limit ExceededLOADL 17Error: User Rate Limit Exceeded5 0 0 17Error: User Rate Limit ExceededLOADLError: User Rate Limit ExceedednError: User Rate Limit ExceededrError: User Rate Limit Exceeded

My question

Error: User Rate Limit ExceededLOADL 1; addError: User Rate Limit Exceeded

editError: User Rate Limit ExceededInstructionError: User Rate Limit Exceeded

Ответы на вопрос(1)

Решение Вопроса

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededgeneratesError: User Rate Limit ExceededemitError: User Rate Limit ExceededemitError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

  • flush the leftmost 0 or more (call this k) instructions that this state represents, and transition to a next state, representing N-k+1, instructions that represents the additional capture of machine instruction I.
  • flush the leftmost k instructions this state represents, transition to the state that represents the remaining N-k instructions, and reprocess instruction I.
  • flush the state completely, and emit instruction I, too. [You can actually do this on the just the start state].

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

 PUSHVAR x
 PUSHK i
 ADD
 POPVAR x
 MOVE x,k

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

 PUSHK i, PUSHK j, ADD ==> PUSHK i+j
 PUSHK i, POPVAR x ==> MOVE x,i 

Error: User Rate Limit Exceeded

 PEEPHOLESTATE (an enum symbol, initialized to EMPTY)
 FIRSTCONSTANT (an int)
 SECONDCONSTANT (an int)

Error: User Rate Limit Exceeded

GeneratePUSHK:
    switch (PEEPHOLESTATE) {
        EMPTY: PEEPHOLESTATE=PUSHK;
               FIRSTCONSTANT=K;
               break;
        PUSHK: PEEPHOLESTATE=PUSHKPUSHK;
               SECONDCONSTANT=K;
               break;
        PUSHKPUSHK:
        #IF consumeEmitLoadK // flush state, transition and consume generated instruction
               emit(PUSHK,FIRSTCONSTANT);
               FIRSTCONSTANT=SECONDCONSTANT;
               SECONDCONSTANT=K;
               PEEPHOLESTATE=PUSHKPUSHK;
               break;
        #ELSE // flush state, transition, and reprocess generated instruction
               emit(PUSHK,FIRSTCONSTANT);
               FIRSTCONSTANT=SECONDCONSTANT;
               PEEPHOLESTATE=PUSHK;
               goto GeneratePUSHK;  // Java can't do this, but other langauges can.
        #ENDIF
     }

  GenerateADD:
    switch (PEEPHOLESTATE) {
        EMPTY: emit(ADD);
               break;
        PUSHK: emit(PUSHK,FIRSTCONSTANT);
               emit(ADD);
               PEEPHOLESTATE=EMPTY;
               break;
        PUSHKPUSHK:
               PEEPHOLESTATE=PUSHK;
               FIRSTCONSTANT+=SECONDCONSTANT;
               break:
     }  

  GeneratePOPX:
    switch (PEEPHOLESTATE) {
        EMPTY: emit(POP,X);
               break;
        PUSHK: emit(MOV,X,FIRSTCONSTANT);
               PEEPHOLESTATE=EMPTY;
               break;
        PUSHKPUSHK:
               emit(MOV,X,SECONDCONSTANT);
               PEEPHOLESTATE=PUSHK;
               break:
     }

GeneratePUSHVARX:
    switch (PEEPHOLESTATE) {
        EMPTY: emit(PUSHVAR,X);
               break;
        PUSHK: emit(PUSHK,FIRSTCONSTANT);
               PEEPHOLESTATE=EMPTY;
               goto GeneratePUSHVARX;
        PUSHKPUSHK:
               PEEPHOLESTATE=PUSHK;
               emit(PUSHK,FIRSTCONSTANT);
               FIRSTCONSTANT=SECONDCONSTANT;
               goto GeneratePUSHVARX;
     }

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

  flush() {
    switch (PEEPHOLESTATE) {
        EMPTY: break;
        PUSHK: emit(PUSHK,FIRSTCONSTANT);
               break;
        PUSHKPUSHK:
               emit(PUSHK,FIRSTCONSTANT),
               emit(PUSHK,SECONDCONSTANT),
               break:
      }
      PEEPHOLESTATE=EMPTY;
      return; }

Error: User Rate Limit ExceededgeneratedError: User Rate Limit Exceeded

      PUSHK  1
      PUSHK  2
      ADD
      PUSHK  5
      POPVAR X
      POPVAR Y

Error: User Rate Limit Exceeded

Ваш ответ на вопрос