Author Topic: Code generation and block linking  (Read 17297 times)

Offline gljiva

  • New Member
  • Posts: 1
Code generation and block linking
« on: October 10, 2010, 05:55:24 PM »
Hi, I have a question about algorithm used in nasm to implement basic block linking and choosing appropriate  jmp/jcc/call opcode (short/near/far).
Lets say I have a list of basic blocks and jmp/jcc/call instructions that point to destination blocks based on block name and not on address. Now when i start organizing blocks in memory i dont know if i will have to use jmp short or near, because destination block still hasn't been laid at specific address. Currently I leave 6 free bytes for all jmp/jcc instructions that don't have their destinations resolved, and after laying all blocks I populate jmp/jcc references with appropriate opcodes. In most cases I can manage with short memory reference which leaves 4 bytes useless. I fill them with nops but that seems ugly solution.
I'm interested how is this properly done in a compiler and looking for a good algorithm to use, so if anyone can suggest solutions/tips I'd be grateful.