2

As part of a group project I'm writing a compiler for a simplified language. As one of the optional features I thought I'd add a peephole optimizer to go over the codegen's output intel assembly code and optimize it.

Our compiler is done in java and it seems like it's going to be a lot of work to create this peephole optimizer using the java I've learned so far. Is there some sort of tool I should be using to make this possible, as pattern matching strings doesn't sound like a good approach in java.

thanks

Paul Sweatte
  • 22,871
  • 7
  • 116
  • 244
ddriver1
  • 703
  • 9
  • 16
  • Would it, perhaps, be more effective to use the optimizers for `gcc`, e.g. by making your codegen emit GENERIC and let gcc create the assembly…? – BRPocock Dec 14 '11 at 18:12

3 Answers3

3

Peephole optimization ought to be done on a binary representation of the parse tree, not on text intended as input to the assembler.

ddyer
  • 1,660
  • 18
  • 24
3

Hard to say without looking at the design of your compiler, but typically you would have an intermediate step between generating code and emitting it. For example, you could think of having the output of the code generation phase be e.g. a linked list of instructions, where each instruction object stores the kind of instruction, any arguments, label/branch destinations, and so on. Then each pattern would inspect the current node and its immediate successors (e.g. if (curr.isMov() && curr.next.isPush() && ...) and modify the list accordingly. Then your peephole optimizer starts with the codegen output, runs each pattern on it, and does this over and over until the list stops changing. Then you have a separate phase which just takes this list of instructions and outputs the actual assembly.

Ismail Badawi
  • 31,611
  • 6
  • 76
  • 92
2

I definitely wouldn't use strings for this. You might look at lex/yacc, and its ilk (e.g. Jack is one for Java, although I haven't used it) to generate the AST of the assembly, then run optimizations on the AST, and write out the assembly again … but you do realise this is a hard thing to do, right? :-)

BRPocock
  • 13,043
  • 3
  • 26
  • 47