home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / linux / extra / docs / maillist / text / archive.94 / text0119.txt < prev    next >
Encoding:
Text File  |  1996-04-02  |  15.8 KB  |  447 lines

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