home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / linux / extra / docs / SynPaper < prev   
Encoding:
Text File  |  1996-07-25  |  15.6 KB  |  427 lines

  1.          Syn68k:  ARDI's dynamically compiling 68LC040 emulator
  2.  
  3.                       Mat Hostetter <mat@ardi.com>
  4.  
  5.  
  6. I.  Overview
  7.  
  8. This document is meant to give a concise technical summary of how
  9. syn68k works.
  10.  
  11. "Syn68k", ARDI's 68LC040 emulator, is both highly portable and
  12. fast.  The portable core of syn68k, which works by dynamically
  13. compiling 680x0 code into an efficient interpreted form, was designed
  14. to run on all major CPU's.  On supported architectures, syn68k can
  15. also translate 680x0 code into native code that the host processor
  16. can run directly.
  17.  
  18.  
  19. II.  Syngen
  20.  
  21. ARDI's "syngen" system analyzes a lisp-like file describing the
  22. bit patterns and semantics of the 680x0 instruction set and produces
  23. lookup tables and C code for the runtime system to use.  This
  24. process takes place only when syn68k is built, so we can afford
  25. extensive analysis here.  The code and tables generated by syngen
  26. depend somewhat on the characteristics of the host processor; for
  27. example, on a little endian machine it is advantageous to byte swap
  28. some extracted 680x0 operands at compile time instead of at runtime.
  29.  
  30. The 680x0 description file can describe multiple ways to emulate
  31. any particular 680x0 opcode.  The runtime system looks at what CC
  32. bits are live after the instruction and chooses the fastest variant
  33. it can legally use.  In the following example, we have two CC
  34. variants of lsrw; one computes no CC bits, and the other computes
  35. all of them:
  36.  
  37.  
  38. (defopcode lsrw_ea
  39.   (list 68000 amode_alterable_memory () (list "1110001011mmmmmm"))
  40.   (list "-----" "-----" dont_expand
  41.     (assign $1.muw (>> $1.muw 1)))
  42.   (list "CN0XZ" "-----" dont_expand
  43.     (list
  44.      (assign ccx (assign ccc (& $1.muw 1)))
  45.      (ASSIGN_NNZ_WORD (assign $1.muw (>> $1.muw 1))))))
  46.  
  47.  
  48. The 680x0 description file can also specify which 680x0 operands
  49. should be "expanded" to become implicitly known by the corresponding
  50. synthetic opcode.  For example, fully expanding out "addl dx,dy"
  51. would result in 64 synthetic opcodes, one for each combination of
  52. data register operands.  This results in smaller and faster synthetic
  53. opcodes at the expense of increasing the total number of synthetic
  54. opcodes.  To conserve space, we only expand out common 680x0 opcodes.
  55. On host architectures where we can compile to native code, we don't
  56. waste space by "expanding out" common synthetic opcodes.
  57.  
  58.  
  59. III.  Test suites
  60.  
  61. ARDI has a large set of test suites that try thousands upon thousands
  62. of variations of 680x0 opcodes and compare the results to those
  63. generated by a real 68040.  These test suites have proven to be an
  64. invaluable debugging tool, both as new features are added and as
  65. we have ported syn68k to other architectures (notably 80x86, 680x0,
  66. i860 and Alpha).  Our native code support is so recent that our
  67. test suites do not yet adequately test all of the situations that
  68. arise when generating native code, but we plan to extend them in
  69. the near future.
  70.  
  71.  
  72. IV.  Interpreted code
  73.  
  74. Our interpreted code consists of contiguous sequences of "synthetic
  75. opcodes" and their operands.  Syngen can generate ANSI C, but when
  76. compiled with gcc it uses C language extensions that make synthetic
  77. opcodes pointers to the C code responsible for interpreting that
  78. opcode.  This "threaded interpreting" entirely eliminates switch
  79. dispatch and loop overhead.
  80.  
  81. To illustrate the above points, here is the assembly language
  82. generated for the synthetic opcode that would handle a fully expanded
  83. "addl d0,d1" when no CC bit values are required.  This is what
  84. gcc's 80x86 output looks like (edited for readability) after we
  85. run our interpreter through ARDI's Pentium-specific instruction
  86. scheduling Perl script:
  87.  
  88.     movl    _d0,%eax    ; fetch d0
  89.     movl    (%esi),%edi    ; fetch next synthetic opcode
  90.     addl    %eax,_d1    ; do the add
  91.     addl    $4,%esi        ; increment synthetic PC
  92.     jmp    *%edi        ; jump to next synthetic opcode handler
  93.  
  94. We must emphasize that the preceding example is not native code
  95. generated by our emulator, but merely a snippet of what gcc generates
  96. for our interpreter.  This gives you some idea of the efficiency
  97. of the portable component of our emulator.
  98.  
  99.  
  100.  V.  Native code
  101.  
  102. Syn68k supports optional architecture-specific native code extensions.
  103. On systems where they are present, the runtime system tries to
  104. generate native code whenever possible.  In those rare cases when
  105. it cannot, it reverts to our interpreted code.  Since syn68k supports
  106. both native and synthetic code, the runtime system automatically
  107. inserts gateways between the two whenever there is a transition.
  108. This approach allows us to gradually phase in native code handlers
  109. for most 680x0 instructions while leaving tricky and unimportant
  110. rare cases alone.
  111.  
  112. Although our native code compilation engine is not architecture-specific,
  113. to date we have only implemented an 80x86 back end.  The 80x86
  114. architecture has achieved such important status in the industry
  115. that it makes sense for us to describe how we generate native code
  116. for it, even though many of these techniques would not be necessary
  117. on RISC architectures.
  118.  
  119. We are glad that we implemented the most difficult back end first.
  120. We believe that, were we to have started with a RISC back end, we
  121. would have quite possibly architected a system where retrofitting
  122. the exotic mechanisms necessary for efficient 80x86 support was
  123. difficult.
  124.  
  125. Three major problems make translating 680x0 code to 80x86 code difficult:
  126.  
  127. 1) The 80x86 has only 8 registers, while the 680x0 has 16.
  128. 2) The 80x86 is little endian, while the 680x0 is big endian.
  129. 3) The 80x86 does not have general-purpose postincrement and predecrement
  130.    operators, which are used frequently in 680x0 code.
  131.  
  132. On the other hand, several factors make the job easier:
  133.  
  134. 1) The 80x86 has all of the CISC addressing modes commonly used in 680x0 code.
  135. 2) The 80x86 has CC bits that map directly to their 680x0 counterparts
  136.    (except for the 680x0's X bit).
  137. 3) The 80x86 supports 8-, 16- and 32-bit operations, (although it can only
  138.    support 8 bit operations on four of its registers).
  139. 4) The 80x86 and 680x0 have analogous conditional branch instructions.
  140. 5) The 80x86 allows unaligned memory accesses without substantial overhead.
  141.  
  142. The toughest problem is the lack of registers.  On 32-register RISC
  143. architectures it's easy to allocate one RISC register for each
  144. 680x0 register, but on the 80x86 a different approach is needed.
  145. The obvious solution is to perform full-blown inter-block register
  146. allocation, but we fear that using traditional compiler techniques
  147. would be unacceptably slow.
  148.  
  149. For now, we have adopted a simple constraint: between basic blocks,
  150. all registers and live CC bits must reside in their canonical home
  151. in memory.  Within a block, anything goes.  So what liberties does
  152. syn68k take within a block?
  153.  
  154. The 80x86 register set is treated as a cache for recently used
  155. 680x0 registers, and the 80x86 CC bits are used as a cache for the
  156. 680x0 CC bits.  At any particular point within a block, each 680x0
  157. register is either sitting in its memory home or is cached in an
  158. 80x86 register, and each live 680x0 CC bit is either cached in its
  159. 80x86 equivalent or stored in its memory home.  Cached registers
  160. may be in canonical form, may be byte swapped, may have only their
  161. low two bytes swapped, or may be offset by a known constant from
  162. their actual value.
  163.  
  164. Each 680x0 instruction can require that 680x0 registers be cached
  165. in particular ways; the compilation engine generates the minimal
  166. code needed to satisfy those constraints and then calls a sequence
  167. of routines to generate the native code.  As each 680x0 instruction
  168. is processed, each 680x0 register's cache status is updated.  Dirty
  169. registers are canonicalized and spilled back to memory at the end
  170. of each block (or when we run out of 80x86 registers and we need
  171. to make room).
  172.  
  173. We allow 680x0 registers to be cached with varying byte orders and
  174. offsets so that we can perform the optimizations of lazy byte
  175. swapping and lazy constant offsetting.  If the 680x0 program loads
  176. a register from memory and then ends up writing it out later, we
  177. avoid unnecessary byte swaps by not canonicalizing the value
  178. immediately.  Lazy constant offsetting mitigates the overhead of
  179. postincrement and predecrement side effects.  For example, this
  180. 680x0 code:
  181.  
  182.     pea        0x1
  183.     pea        0x2
  184.     pea        0x3
  185.     pea        0x4
  186.     ...
  187.  
  188. becomes this 80x86 code:
  189.  
  190.     movl    _a7,%edi
  191.     movl    $0x01000000,-4(%edi)    ; "push" big-endian constant
  192.     movl    $0x02000000,-8(%edi)
  193.     movl    $0x03000000,-12(%edi)
  194.     movl    $0x04000000,-16(%edi)
  195.     ... <more uses of a7 may follow, and they'll use %edi>
  196.     subl    $16,%edi
  197.     movl    $edi,_a7
  198.     ...
  199.  
  200.  
  201. As mentioned above, we use the 80x86 condition code bits as a cache
  202. for the real 680x0 CC bits.  Although live cached CC bits are
  203. occasionally spilled back to memory because some 80x86 instruction
  204. is about to clobber them, this trick almost always works.  Using
  205. 80x86 CC bits, we can frequently get away with extremely concise
  206. code sequences; for example, a 680x0 compare and conditional branch
  207. becomes an 80x86 compare and conditional branch.
  208.  
  209.  
  210. VI.  Self-modifying code
  211.  
  212. Like most dynamically compiling emulators, syn68k doesn't detect
  213. self-modifying code; the overhead is too high.  Fortunately,
  214. self-modifying programs don't work on the real 68040 either.  We
  215. rely on the program making explicit system calls to flush the caches
  216. whenever 680x0 code may have been modified or created.  Some programs
  217. (like HyperCard) flush the caches very often, which can cause real
  218. performance headaches if code is continuously recompiled.  We have
  219. solved this problem by checksumming 680x0 blocks as they are compiled
  220. and only decompiling blocks which fail their checksums.  This
  221. optimization alone sped up some HyperCard stacks by a factor of
  222. three or so.
  223.  
  224.  
  225.  
  226. VII.  Responsiveness
  227.  
  228. Responsiveness is a concern for any dynamic compiler.  Fortunately,
  229. syn68k is largely driven by automatically generated lookup tables
  230. so compilation speed is good.  Like other dynamic compilers, syn68k
  231. only bothers to compile 680x0 code when it encounters it for the
  232. first time.
  233.  
  234. When syn68k encounters new code, it compiles other 680x0 code that
  235. it can reach from there but does not compile through jsr's.  Only
  236. when a jsr is actually executed does syn68k compile the target
  237. routine.  Once that target routine is compiled, syn68k modifies
  238. the jsr handler to point directly to the target routine so that
  239. the jsr will be extremely fast the second time it is executed.
  240. We've found that lazily compiling through jsr's does a good job of
  241. avoiding compilation lag that might annoy the user.
  242.  
  243. Syn68k does not attempt to generate native code for a basic block
  244. until that block (or a nearby one) has been executed 50 times.
  245. This saves memory and some compilation time, although we haven't
  246. noticed any particular sluggishness when compiling to native code.
  247.  
  248.  
  249. VIII.  Other optimizations
  250.  
  251. Syn68k maintains an internal "jsr stack" to speed up the common
  252. case of jsr/rts.  We realize that the rts address might have been
  253. fiddled with, so the rts handler verifies at runtime that the rts
  254. address matches the tag on top of the jsr stack.  If it matches,
  255. syn68k does a fast jump.  If it doesn't, syn68k looks up the code
  256. corresponding to the rts address in a hash table.
  257.  
  258.  
  259. IX.  Neat hacks
  260.  
  261. The low-level code generation routines for the 80x86 back end are
  262. machine generated from assembly language templates.  Thousands of
  263. operand permutations for 80x86 instructions of interest are run
  264. through the system's assembler and analyzed to derive the rules
  265. the assembler uses to create binaries.  Those rules are encapsulated
  266. into C code and compiled into syn68k so we can generate binaries
  267. on the fly.  Here is a sample template:
  268.  
  269.   { "i386_leal_indoff", "", "", "", "", "-",
  270.       "leal %0(%1),%2",
  271.       { "offset", "base", "dst" },
  272.       { { SIZE_32, CONSTANT, IN }, { SIZE_32, REGISTER, IN },
  273.         { SIZE_32, REGISTER, OUT } } },
  274.  
  275. This approach has saved us countless hours of debugging and allows
  276. our system to automatically perform the same optimizations as the
  277. host system's assembler.
  278.  
  279. We've annotated our 80x86 descriptions with information about
  280. Pentium pairability so that future versions of syn68k can schedule
  281. the native code we generate (we already schedule our main interpreter
  282. when we build syn68k).
  283.  
  284.  
  285. X.  Future optimizations
  286.  
  287. We are working on a simple inter-block register allocation algorithm.
  288.  
  289. By relocating most 680x0 register->80x86 register moves to the
  290. beginning of each block, we can improve Pentium pairability and
  291. reduce 80486 and Pentium address generation pipeline stalls.
  292.  
  293. Now that we compile to native code, A-line trap overhead is becoming
  294. significant.  We may soon compile A-line traps to native code that
  295. directly calls the appropriate ROMlib routine with the appropriate
  296. arguments (checking at runtime to make sure that neither the trap
  297. nor the A-line vector has been patched out, of course).
  298.  
  299.  
  300. XI.  Code examples
  301.  
  302. Here are two sample 680x0 code sequences from real applications,
  303. and the 80x86 code that syn68k generates for them.  We chose these
  304. code sequences specifically to showcase several of the techniques
  305. we use, so you shouldn't use them as a substitute for benchmarks.
  306. Not all 680x0 code translates as well as these examples do, but
  307. these examples are far from exotic.
  308.  
  309.  
  310. Example 1 (Solarian):
  311.  
  312. 680x0 code:
  313.  
  314.     addqb    #1,a4@(1)
  315.     movel    #0,d0
  316.     moveb    a4@,d0
  317.     swap    d0
  318.     clrw    d0
  319.     swap    d0
  320.     asll    #2,d0
  321.     lea    a5@(-13462),a0
  322.     addal    d0,a0
  323.     moveal    a0@,a0
  324.     movel    #0,d0
  325.     moveb    a4@(1),d0
  326.     cmpw    a0@,d0
  327.     bcs    0x3fffee2
  328.  
  329.  
  330. 80x86 code:
  331.  
  332.     movl    _a4,%edi        ; addqb #1,a4@(1)
  333.     addb    $0x1,0x1(%edi)
  334.     xorl    %ebx,%ebx        ; movel #0,d0
  335.     movb    (%edi),%bl        ; moveb a4@,d0
  336.     rorl    $0x10,%ebx        ; swap d0
  337.     xorw    %bx,%bx            ; clrw d0
  338.     rorl    $0x10,%ebx        ; swap d0
  339.     shll    $0x2,%ebx        ; asll #2,d0
  340.     movl    _a5,%esi        ; lea a5@(-13462),a0
  341.     leal    0xffffcb6a(%esi),%edx
  342.     addl    %ebx,%edx        ; addal d0,a0
  343.     movl    (%edx),%edx        ; moveal a0@,a0
  344.     xorl    %ebx,%ebx        ; movel #0,d0
  345.     movb    0x1(%edi),%bl        ; moveb a4@(1),d0
  346.     bswap    %edx            ; cmpw a0@,d0
  347.     movw    (%edx),%cx
  348.     rorw    $0x8,%cx
  349.     cmpw    %cx,%bx
  350.     movl    %edx,_a0        ; <spill dirty 68k
  351.     movl    %ebx,_d0        ;  registers back to memory>
  352.     jb    0x6fae0c        ; bcs 0x3fffee2
  353.     jmp    0x6faf0c        ; <go to "fall through" code>
  354.  
  355.  
  356.  
  357.  
  358. Example 2 (PageMaker):
  359.  
  360. 680x0 code:
  361.  
  362.     movel    #0,d2
  363.     moveb    d0,d2
  364.     lslw    #8,d0
  365.     orw    d0,d2
  366.     movel    d2,d0
  367.     swap    d2
  368.     orl    d2,d0
  369.     movel    a0,d2
  370.     lsrb    #1,d2
  371.     bcc    0x3fffed4
  372.  
  373. 80x86 code:
  374.  
  375.     xorl    %ebx,%ebx        ; movel #0,d2
  376.     movl    _d0,%edx        ; moveb d0,d2
  377.     movb    %dl,%bl
  378.     shlw    $0x8,%dx        ; lslw #8,d0
  379.     orw    %dx,%bx            ; orw d0,d2
  380.     movl    %ebx,%edx        ; movel d2,d0
  381.     rorl    $0x10,%ebx        ; swap d2
  382.     orl    %ebx,%edx        ; orl d2,d0
  383.     movl    _a0,%ecx        ; movel a0,d2
  384.     movl    %ecx,%ebx
  385.     shrb    %bl            ; lsrb #1,d2
  386.     movl    %ebx,_d2        ; <spill dirty 68k
  387.     movl    %edx,_d0        ;  registers back to memory>
  388.     jae    0x3b734c        ; bcc 0x3fffed4
  389.     jmp    0x43d48c        ; <go to "fall through" 68k code>
  390.  
  391.  
  392. XII.  Benchmarks
  393.  
  394. These performance numbers were computed with Speedometer 3.23.
  395. We've removed the floating point tests from the list since they do
  396. not measure syn68k's speed.  Syn68k contains no special provisions
  397. for Speedometer's benchmarks and we believe that these numbers are
  398. indicative of syn68k's performance for many other CPU-intensive
  399. programs.
  400.  
  401.  
  402.         Quadra    Pentium    486DX4    486DX/2
  403.                   610     90MHz     75MHz     66MHz
  404.         ------    ------    ------    ------
  405. CPU        16.018    28.833    15.727    13.840
  406.  
  407. Dhrystones    19.586    21.886    12.084     9.424
  408. Tower        18.909    27.130    12.235    11.556
  409. Quicksort    17.759    27.105    15.606    13.919
  410. Bubble sort    18.409    31.154    19.286    16.875
  411. Queens        19.083    38.167    19.083    18.320
  412. Puzzle        22.083    44.167    23.661    21.032
  413. Permutations    21.019    28.564    11.604    12.242
  414. Int. Matrix    24.200    26.469    19.369    16.608
  415. Sieve        23.362    60.290    33.982    30.145
  416.         ------    ------    ------    ------
  417. Average        20.490    33.881    18.582    16.680
  418.  
  419.  
  420.  
  421. Preliminary analysis suggests that we average a roughly 3:1
  422. instruction count increase when translating to 80x86 code.  We have
  423. not yet taken rigorous measurements, but the 3:1 figure is lent
  424. some credence by the fact that our 75MHz 486DX4 gets nearly the
  425. same performance as our Quadra 610.  We believe that inter-block
  426. register allocation will noticeably improve this ratio.
  427.