hlcgobj.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit implements the basic high level code generator object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {# @abstract(Abstract code generator unit)
  19. Abstract high level code generator unit. This contains the base class
  20. that either lowers most code to the regular code generator, or that
  21. has to be implemented/overridden for higher level targets (such as LLVM).
  22. }
  23. unit hlcgobj;
  24. {$i fpcdefs.inc}
  25. { define hlcginline}
  26. interface
  27. uses
  28. cclasses,globtype,constexp,
  29. cpubase,cgbase,cgutils,parabase,
  30. aasmbase,aasmtai,aasmdata,aasmcpu,
  31. symconst,symtype,symdef,rgobj,
  32. node
  33. ;
  34. type
  35. {# @abstract(Abstract high level code generator)
  36. This class implements an abstract instruction generator. All
  37. methods of this class are generic and are mapped to low level code
  38. generator methods by default. They have to be overridden for higher
  39. level targets
  40. }
  41. { thlcgobj }
  42. thlcgobj = class
  43. public
  44. {************************************************}
  45. { basic routines }
  46. constructor create;
  47. {# Initialize the register allocators needed for the codegenerator.}
  48. procedure init_register_allocators;virtual;
  49. {# Clean up the register allocators needed for the codegenerator.}
  50. procedure done_register_allocators;virtual;
  51. {# Set whether live_start or live_end should be updated when allocating registers, needed when e.g. generating initcode after the rest of the code. }
  52. procedure set_regalloc_live_range_direction(dir: TRADirection);virtual;
  53. {# Gets a register suitable to do integer operations on.}
  54. function getintregister(list:TAsmList;size:tdef):Tregister;virtual;
  55. {# Gets a register suitable to do integer operations on.}
  56. function getaddressregister(list:TAsmList;size:tdef):Tregister;virtual;
  57. function getfpuregister(list:TAsmList;size:tdef):Tregister;virtual;
  58. // we don't have high level defs yet that translate into all mm cgsizes
  59. // function getmmregister(list:TAsmList;size:tdef):Tregister;virtual;
  60. function getflagregister(list:TAsmList;size:tdef):Tregister;virtual;
  61. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  62. the cpu specific child cg object have such a method?}
  63. function uses_registers(rt:Tregistertype):boolean; inline;
  64. procedure do_register_allocation(list:TAsmList;headertai:tai); inline;
  65. procedure translate_register(var reg : tregister); inline;
  66. {# Emit a label to the instruction stream. }
  67. procedure a_label(list : TAsmList;l : tasmlabel); inline;
  68. {# Allocates register r by inserting a pai_realloc record }
  69. procedure a_reg_alloc(list : TAsmList;r : tregister); inline;
  70. {# Deallocates register r by inserting a pa_regdealloc record}
  71. procedure a_reg_dealloc(list : TAsmList;r : tregister); inline;
  72. { Synchronize register, make sure it is still valid }
  73. procedure a_reg_sync(list : TAsmList;r : tregister); inline;
  74. {# Pass a parameter, which is located in a register, to a routine.
  75. This routine should push/send the parameter to the routine, as
  76. required by the specific processor ABI and routine modifiers.
  77. It must generate register allocation information for the cgpara in
  78. case it consists of cpuregisters.
  79. @param(size size of the operand in the register)
  80. @param(r register source of the operand)
  81. @param(cgpara where the parameter will be stored)
  82. }
  83. procedure a_load_reg_cgpara(list : TAsmList;size : tdef;r : tregister;const cgpara : TCGPara);virtual;
  84. {# Pass a parameter, which is a constant, to a routine.
  85. A generic version is provided. This routine should
  86. be overridden for optimization purposes if the cpu
  87. permits directly sending this type of parameter.
  88. It must generate register allocation information for the cgpara in
  89. case it consists of cpuregisters.
  90. @param(size size of the operand in constant)
  91. @param(a value of constant to send)
  92. @param(cgpara where the parameter will be stored)
  93. }
  94. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : aint;const cgpara : TCGPara);virtual;
  95. {# Pass the value of a parameter, which is located in memory, to a routine.
  96. A generic version is provided. This routine should
  97. be overridden for optimization purposes if the cpu
  98. permits directly sending this type of parameter.
  99. It must generate register allocation information for the cgpara in
  100. case it consists of cpuregisters.
  101. @param(size size of the operand in constant)
  102. @param(r Memory reference of value to send)
  103. @param(cgpara where the parameter will be stored)
  104. }
  105. procedure a_load_ref_cgpara(list : TAsmList;size : tdef;const r : treference;const cgpara : TCGPara);virtual;
  106. {# Pass the value of a parameter, which can be located either in a register or memory location,
  107. to a routine.
  108. A generic version is provided.
  109. @param(l location of the operand to send)
  110. @param(nr parameter number (starting from one) of routine (from left to right))
  111. @param(cgpara where the parameter will be stored)
  112. }
  113. procedure a_load_loc_cgpara(list : TAsmList;size : tdef; const l : tlocation;const cgpara : TCGPara);virtual;
  114. {# Pass the address of a reference to a routine. This routine
  115. will calculate the address of the reference, and pass this
  116. calculated address as a parameter.
  117. It must generate register allocation information for the cgpara in
  118. case it consists of cpuregisters.
  119. A generic version is provided. This routine should
  120. be overridden for optimization purposes if the cpu
  121. permits directly sending this type of parameter.
  122. @param(fromsize type of the reference we are taking the address of)
  123. @param(tosize type of the pointer that we get as a result)
  124. @param(r reference to get address from)
  125. }
  126. procedure a_loadaddr_ref_cgpara(list : TAsmList;fromsize, tosize : tdef;const r : treference;const cgpara : TCGPara);virtual;
  127. { Remarks:
  128. * If a method specifies a size you have only to take care
  129. of that number of bits, i.e. load_const_reg with OP_8 must
  130. only load the lower 8 bit of the specified register
  131. the rest of the register can be undefined
  132. if necessary the compiler will call a method
  133. to zero or sign extend the register
  134. * The a_load_XX_XX with OP_64 needn't to be
  135. implemented for 32 bit
  136. processors, the code generator takes care of that
  137. * the addr size is for work with the natural pointer
  138. size
  139. * the procedures without fpu/mm are only for integer usage
  140. * normally the first location is the source and the
  141. second the destination
  142. }
  143. {# Emits instruction to call the method specified by symbol name.
  144. This routine must be overridden for each new target cpu.
  145. }
  146. procedure a_call_name(list : TAsmList;pd : tprocdef;const s : string; weak: boolean);virtual;abstract;
  147. procedure a_call_reg(list : TAsmList;pd : tabstractprocdef;reg : tregister);virtual;abstract;
  148. procedure a_call_ref(list : TAsmList;pd : tabstractprocdef;ref : treference);virtual;abstract;
  149. { same as a_call_name, might be overridden on certain architectures to emit
  150. static calls without usage of a got trampoline }
  151. procedure a_call_name_static(list : TAsmList;pd : tprocdef;const s : string);virtual;
  152. { same as a_call_name, might be overridden on certain architectures to emit
  153. special static calls for inherited methods }
  154. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : string);virtual;
  155. { move instructions }
  156. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);virtual;abstract;
  157. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);virtual;
  158. procedure a_load_const_loc(list : TAsmList;tosize : tdef;a : aint;const loc : tlocation);virtual;
  159. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;abstract;
  160. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;
  161. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);virtual;abstract;
  162. procedure a_load_reg_loc(list : TAsmList;fromsize, tosize : tdef;reg : tregister;const loc: tlocation);virtual;
  163. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;abstract;
  164. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;
  165. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);virtual;
  166. procedure a_load_loc_reg(list : TAsmList;fromsize, tosize : tdef; const loc: tlocation; reg : tregister);virtual;
  167. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);virtual;
  168. procedure a_load_loc_subsetreg(list : TAsmList;fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg : tsubsetregister);virtual;
  169. procedure a_load_loc_subsetref(list : TAsmList;fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref : tsubsetreference);virtual;
  170. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);virtual;abstract;
  171. { The subset stuff still need a transformation to thlcgobj }
  172. procedure a_load_subsetreg_reg(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); virtual; abstract;
  173. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, tosize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister); virtual; abstract;
  174. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsreg, tosreg: tsubsetregister); virtual; abstract;
  175. procedure a_load_subsetreg_ref(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference); virtual; abstract;
  176. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, tosize,tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister); virtual; abstract;
  177. procedure a_load_const_subsetreg(list: TAsmlist; tosize, tosubsetsize: tdef; a: aint; const sreg: tsubsetregister); virtual; abstract;
  178. procedure a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation); virtual;
  179. procedure a_load_subsetref_reg(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister); virtual; abstract;
  180. procedure a_load_reg_subsetref(list : TAsmList; fromsize, tosubsetsize, tosize: tdef; fromreg: tregister; const sref: tsubsetreference); virtual; abstract;
  181. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsref, tosref: tsubsetreference); virtual; abstract;
  182. procedure a_load_subsetref_ref(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference); virtual; abstract;
  183. procedure a_load_ref_subsetref(list : TAsmList; fromsize, tosize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference); virtual; abstract;
  184. procedure a_load_const_subsetref(list: TAsmlist; tosize, tosubsetsize: tdef; a: aint; const sref: tsubsetreference); virtual; abstract;
  185. procedure a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation); virtual;
  186. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual; abstract;
  187. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual; abstract;
  188. { bit test instructions (still need transformation to thlcgobj) }
  189. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tdef;bitnumber,value,destreg: tregister); virtual; abstract;
  190. procedure a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister); virtual; abstract;
  191. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister); virtual; abstract;
  192. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; fromsize, fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister); virtual; abstract;
  193. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister); virtual; abstract;
  194. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);virtual;
  195. procedure a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);virtual;
  196. { bit set/clear instructions (still need transformation to thlcgobj) }
  197. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber,dest: tregister); virtual; abstract;
  198. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tdef; bitnumber: aint; const ref: treference); virtual; abstract;
  199. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister); virtual; abstract;
  200. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize, destsubsetsize: tdef; bitnumber: aint; const destreg: tsubsetregister); virtual; abstract;
  201. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference); virtual; abstract;
  202. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);virtual;
  203. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);virtual;
  204. { bit scan instructions (still need transformation to thlcgobj) }
  205. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tdef; src, dst: tregister); virtual; abstract;
  206. { fpu move instructions }
  207. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  208. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  209. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  210. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  211. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  212. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  213. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  214. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  215. { vector register move instructions }
  216. // we don't have high level defs yet that translate into all mm cgsizes
  217. {
  218. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  219. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  220. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  221. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);virtual;
  222. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  223. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  224. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  225. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  226. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual;
  227. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  228. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  229. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  230. }
  231. // we don't have high level defs yet that translate into all mm cgsizes
  232. // procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  233. // procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  234. { basic arithmetic operations }
  235. { note: for operators which require only one argument (not, neg), use }
  236. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  237. { that in this case the *second* operand is used as both source and }
  238. { destination (JM) }
  239. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); virtual; abstract;
  240. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); virtual;
  241. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sreg: tsubsetregister); virtual;
  242. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sref: tsubsetreference); virtual;
  243. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);virtual;
  244. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  245. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  246. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  247. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  248. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  249. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  250. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  251. { trinary operations for processors that support them, 'emulated' }
  252. { on others. None with "ref" arguments since I don't think there }
  253. { are any processors that support it (JM) }
  254. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); virtual;
  255. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  256. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  257. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  258. { comparison operations }
  259. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;reg : tregister;
  260. l : tasmlabel);virtual;
  261. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;const ref : treference;
  262. l : tasmlabel); virtual;
  263. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: aint; const loc: tlocation;
  264. l : tasmlabel);virtual;
  265. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  266. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  267. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  268. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  269. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  270. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  271. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);virtual;
  272. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  273. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  274. {$ifdef cpuflags}
  275. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  276. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  277. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  278. }
  279. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  280. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  281. {$endif cpuflags}
  282. // procedure g_maybe_testself(list : TAsmList;reg:tregister);
  283. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  284. {# This should emit the opcode to copy len bytes from the source
  285. to destination.
  286. It must be overridden for each new target processor.
  287. @param(source Source reference of copy)
  288. @param(dest Destination reference of copy)
  289. }
  290. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  291. {# This should emit the opcode to copy len bytes from the an unaligned source
  292. to destination.
  293. It must be overridden for each new target processor.
  294. @param(source Source reference of copy)
  295. @param(dest Destination reference of copy)
  296. }
  297. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  298. {# This should emit the opcode to a shortrstring from the source
  299. to destination.
  300. @param(source Source reference of copy)
  301. @param(dest Destination reference of copy)
  302. }
  303. // procedure g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  304. // procedure g_copyvariant(list : TAsmList;const source,dest : treference);
  305. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  306. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  307. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  308. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  309. {# Generates range checking code. It is to note
  310. that this routine does not need to be overridden,
  311. as it takes care of everything.
  312. @param(p Node which contains the value to check)
  313. @param(todef Type definition of node to range check)
  314. }
  315. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual; abstract;
  316. {# Generates overflow checking code for a node }
  317. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  318. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  319. // procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);virtual;
  320. // procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);virtual;
  321. {# Emits instructions when compilation is done in profile
  322. mode (this is set as a command line option). The default
  323. behavior does nothing, should be overridden as required.
  324. }
  325. procedure g_profilecode(list : TAsmList);virtual;
  326. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  327. @param(size Number of bytes to allocate)
  328. }
  329. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  330. {# Emits instruction for allocating the locals in entry
  331. code of a routine. This is one of the first
  332. routine called in @var(genentrycode).
  333. @param(localsize Number of bytes to allocate as locals)
  334. }
  335. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  336. {# Emits instructions for returning from a subroutine.
  337. Should also restore the framepointer and stack.
  338. @param(parasize Number of bytes of parameters to deallocate from stack)
  339. }
  340. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  341. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  342. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  343. function g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;virtual; abstract;
  344. { generate a stub which only purpose is to pass control the given external method,
  345. setting up any additional environment before doing so (if required).
  346. The default implementation issues a jump instruction to the external name. }
  347. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  348. { routines migrated from ncgutil }
  349. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;abstract;
  350. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;abstract;
  351. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;abstract;
  352. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  353. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  354. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;abstract;
  355. procedure gen_proc_symbol(list:TAsmList);virtual;
  356. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  357. { extras refactored from other units }
  358. { queue the code/data generated for a procedure for writing out to
  359. the assembler/object file }
  360. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  361. end;
  362. var
  363. {# Main high level code generator class }
  364. hlcg : thlcgobj;
  365. procedure destroy_hlcodegen;
  366. implementation
  367. uses
  368. globals,options,systems,
  369. fmodule,export,
  370. verbose,defutil,paramgr,symsym,
  371. cpuinfo,cgobj,tgobj,cutils,procinfo,
  372. ncgutil;
  373. procedure destroy_hlcodegen;
  374. begin
  375. hlcg.free;
  376. hlcg:=nil;
  377. end;
  378. { thlcgobj }
  379. constructor thlcgobj.create;
  380. begin
  381. end;
  382. procedure thlcgobj.init_register_allocators;
  383. begin
  384. cg.init_register_allocators;
  385. end;
  386. procedure thlcgobj.done_register_allocators;
  387. begin
  388. cg.done_register_allocators;
  389. end;
  390. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  391. begin
  392. cg.set_regalloc_live_range_direction(dir);
  393. end;
  394. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  395. begin
  396. result:=cg.getintregister(list,def_cgsize(size));
  397. end;
  398. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  399. begin
  400. result:=cg.getaddressregister(list);
  401. end;
  402. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  403. begin
  404. result:=cg.getfpuregister(list,def_cgsize(size));
  405. end;
  406. (*
  407. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  408. begin
  409. result:=cg.getmmregister(list,def_cgsize(size));
  410. end;
  411. *)
  412. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  413. begin
  414. result:=cg.getflagregister(list,def_cgsize(size));
  415. end;
  416. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  417. begin
  418. result:=cg.uses_registers(rt);
  419. end;
  420. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  421. begin
  422. cg.do_register_allocation(list,headertai);
  423. end;
  424. procedure thlcgobj.translate_register(var reg: tregister);
  425. begin
  426. cg.translate_register(reg);
  427. end;
  428. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  429. begin
  430. cg.a_label(list,l);
  431. end;
  432. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  433. begin
  434. cg.a_reg_alloc(list,r);
  435. end;
  436. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  437. begin
  438. cg.a_reg_dealloc(list,r);
  439. end;
  440. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  441. begin
  442. cg.a_reg_sync(list,r);
  443. end;
  444. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  445. var
  446. ref: treference;
  447. begin
  448. cgpara.check_simple_location;
  449. paramanager.alloccgpara(list,cgpara);
  450. case cgpara.location^.loc of
  451. LOC_REGISTER,LOC_CREGISTER:
  452. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  453. LOC_REFERENCE,LOC_CREFERENCE:
  454. begin
  455. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  456. a_load_reg_ref(list,size,cgpara.def,r,ref);
  457. end;
  458. (*
  459. LOC_MMREGISTER,LOC_CMMREGISTER:
  460. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  461. *)
  462. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  463. begin
  464. tg.GetTemp(list,size.size,size.alignment,tt_normal,ref);
  465. a_load_reg_ref(list,size,size,r,ref);
  466. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  467. tg.Ungettemp(list,ref);
  468. end
  469. else
  470. internalerror(2010120415);
  471. end;
  472. end;
  473. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  474. var
  475. ref : treference;
  476. begin
  477. cgpara.check_simple_location;
  478. paramanager.alloccgpara(list,cgpara);
  479. case cgpara.location^.loc of
  480. LOC_REGISTER,LOC_CREGISTER:
  481. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  482. LOC_REFERENCE,LOC_CREFERENCE:
  483. begin
  484. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  485. a_load_const_ref(list,cgpara.def,a,ref);
  486. end
  487. else
  488. internalerror(2010120416);
  489. end;
  490. end;
  491. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  492. var
  493. ref: treference;
  494. begin
  495. cgpara.check_simple_location;
  496. paramanager.alloccgpara(list,cgpara);
  497. case cgpara.location^.loc of
  498. LOC_REGISTER,LOC_CREGISTER:
  499. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  500. LOC_REFERENCE,LOC_CREFERENCE:
  501. begin
  502. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  503. a_load_ref_ref(list,size,cgpara.def,r,ref);
  504. end
  505. (*
  506. LOC_MMREGISTER,LOC_CMMREGISTER:
  507. begin
  508. case location^.size of
  509. OS_F32,
  510. OS_F64,
  511. OS_F128:
  512. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  513. OS_M8..OS_M128,
  514. OS_MS8..OS_MS128:
  515. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  516. else
  517. internalerror(2010120417);
  518. end;
  519. end
  520. *)
  521. else
  522. internalerror(2010120418);
  523. end;
  524. end;
  525. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  526. begin
  527. case l.loc of
  528. LOC_REGISTER,
  529. LOC_CREGISTER :
  530. a_load_reg_cgpara(list,size,l.register,cgpara);
  531. LOC_CONSTANT :
  532. a_load_const_cgpara(list,size,l.value,cgpara);
  533. LOC_CREFERENCE,
  534. LOC_REFERENCE :
  535. a_load_ref_cgpara(list,size,l.reference,cgpara);
  536. else
  537. internalerror(2010120419);
  538. end;
  539. end;
  540. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize, tosize: tdef; const r: treference; const cgpara: TCGPara);
  541. var
  542. hr : tregister;
  543. begin
  544. cgpara.check_simple_location;
  545. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  546. begin
  547. paramanager.allocparaloc(list,cgpara.location);
  548. a_loadaddr_ref_reg(list,fromsize,tosize,r,cgpara.location^.register)
  549. end
  550. else
  551. begin
  552. hr:=getaddressregister(list,tosize);
  553. a_loadaddr_ref_reg(list,fromsize,tosize,r,hr);
  554. a_load_reg_cgpara(list,tosize,hr,cgpara);
  555. end;
  556. end;
  557. procedure thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: string);
  558. begin
  559. a_call_name(list,pd,s,false);
  560. end;
  561. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: string);
  562. begin
  563. a_call_name(list,pd,s,false);
  564. end;
  565. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  566. var
  567. tmpreg: tregister;
  568. begin
  569. tmpreg:=getintregister(list,tosize);
  570. a_load_const_reg(list,tosize,a,tmpreg);
  571. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  572. end;
  573. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  574. begin
  575. case loc.loc of
  576. LOC_REFERENCE,LOC_CREFERENCE:
  577. a_load_const_ref(list,tosize,a,loc.reference);
  578. LOC_REGISTER,LOC_CREGISTER:
  579. a_load_const_reg(list,tosize,a,loc.register);
  580. { we don't have enough type information to handle these here
  581. LOC_SUBSETREG,LOC_CSUBSETREG:
  582. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  583. LOC_SUBSETREF,LOC_CSUBSETREF:
  584. a_load_const_subsetref(list,loc.size,a,loc.sref);
  585. }
  586. else
  587. internalerror(2010120401);
  588. end;
  589. end;
  590. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  591. begin
  592. a_load_reg_ref(list,fromsize,tosize,register,ref);
  593. end;
  594. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  595. begin
  596. case loc.loc of
  597. LOC_REFERENCE,LOC_CREFERENCE:
  598. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  599. LOC_REGISTER,LOC_CREGISTER:
  600. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  601. { we don't have enough type information to handle these here
  602. LOC_SUBSETREG,LOC_CSUBSETREG:
  603. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  604. LOC_SUBSETREF,LOC_CSUBSETREF:
  605. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  606. LOC_MMREGISTER,LOC_CMMREGISTER:
  607. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  608. }
  609. else
  610. internalerror(2010120402);
  611. end;
  612. end;
  613. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  614. begin
  615. a_load_ref_reg(list,fromsize,tosize,ref,register);
  616. end;
  617. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  618. var
  619. tmpreg: tregister;
  620. begin
  621. { verify if we have the same reference }
  622. if references_equal(sref,dref) then
  623. exit;
  624. tmpreg:=getintregister(list,tosize);
  625. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  626. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  627. end;
  628. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  629. begin
  630. case loc.loc of
  631. LOC_REFERENCE,LOC_CREFERENCE:
  632. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  633. LOC_REGISTER,LOC_CREGISTER:
  634. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  635. LOC_CONSTANT:
  636. a_load_const_reg(list,tosize,loc.value,reg);
  637. { we don't have enough type information to handle these here
  638. LOC_SUBSETREG,LOC_CSUBSETREG:
  639. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  640. LOC_SUBSETREF,LOC_CSUBSETREF:
  641. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  642. }
  643. else
  644. internalerror(2010120201);
  645. end;
  646. end;
  647. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  648. begin
  649. case loc.loc of
  650. LOC_REFERENCE,LOC_CREFERENCE:
  651. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  652. LOC_REGISTER,LOC_CREGISTER:
  653. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  654. LOC_CONSTANT:
  655. a_load_const_ref(list,tosize,loc.value,ref);
  656. { we don't have enough type information to handle these here
  657. LOC_SUBSETREG,LOC_CSUBSETREG:
  658. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  659. LOC_SUBSETREF,LOC_CSUBSETREF:
  660. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  661. }
  662. else
  663. internalerror(2010120403);
  664. end;
  665. end;
  666. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  667. begin
  668. case loc.loc of
  669. LOC_REFERENCE,LOC_CREFERENCE:
  670. a_load_ref_subsetreg(list,fromsize,tosize,tosubsetsize,loc.reference,sreg);
  671. LOC_REGISTER,LOC_CREGISTER:
  672. a_load_reg_subsetreg(list,fromsize,tosize,tosubsetsize,loc.register,sreg);
  673. LOC_CONSTANT:
  674. a_load_const_subsetreg(list,tosize,tosubsetsize,loc.value,sreg);
  675. { we don't have enough type information to handle these here
  676. LOC_SUBSETREG,LOC_CSUBSETREG:
  677. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  678. LOC_SUBSETREF,LOC_CSUBSETREF:
  679. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  680. }
  681. else
  682. internalerror(2010120404);
  683. end;
  684. end;
  685. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  686. begin
  687. case loc.loc of
  688. LOC_REFERENCE,LOC_CREFERENCE:
  689. a_load_ref_subsetref(list,fromsize,tosize,tosubsetsize,loc.reference,sref);
  690. LOC_REGISTER,LOC_CREGISTER:
  691. a_load_reg_subsetref(list,fromsize,tosize,tosubsetsize,loc.register,sref);
  692. LOC_CONSTANT:
  693. a_load_const_subsetref(list,tosize,tosubsetsize,loc.value,sref);
  694. { we don't have enough type information to handle these here
  695. LOC_SUBSETREG,LOC_CSUBSETREG:
  696. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  697. LOC_SUBSETREF,LOC_CSUBSETREF:
  698. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  699. }
  700. else
  701. internalerror(2010120405);
  702. end;
  703. end;
  704. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  705. begin
  706. case loc.loc of
  707. LOC_REFERENCE,LOC_CREFERENCE:
  708. a_load_subsetreg_ref(list,fromsize,fromsubsetsize,tosize,sreg,loc.reference);
  709. LOC_REGISTER,LOC_CREGISTER:
  710. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,tosize,sreg,loc.register);
  711. { we don't have enough type information to handle these here
  712. LOC_SUBSETREG,LOC_CSUBSETREG:
  713. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  714. LOC_SUBSETREF,LOC_CSUBSETREF:
  715. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  716. }
  717. else
  718. internalerror(2010120406);
  719. end;
  720. end;
  721. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  722. begin
  723. case loc.loc of
  724. LOC_REFERENCE,LOC_CREFERENCE:
  725. a_load_subsetref_ref(list,fromsize,fromsubsetsize,tosize,sref,loc.reference);
  726. LOC_REGISTER,LOC_CREGISTER:
  727. a_load_subsetref_reg(list,fromsize,fromsubsetsize,tosize,sref,loc.register);
  728. { we don't have enough type information to handle these here
  729. LOC_SUBSETREG,LOC_CSUBSETREG:
  730. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  731. LOC_SUBSETREF,LOC_CSUBSETREF:
  732. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  733. }
  734. else
  735. internalerror(2010120407);
  736. end;
  737. end;
  738. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  739. var
  740. tmpreg: tregister;
  741. begin
  742. case loc.loc of
  743. LOC_REFERENCE,LOC_CREFERENCE:
  744. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  745. LOC_REGISTER,LOC_CREGISTER,
  746. LOC_SUBSETREG,LOC_CSUBSETREG,
  747. LOC_CONSTANT:
  748. begin
  749. case loc.loc of
  750. LOC_REGISTER,LOC_CREGISTER:
  751. tmpreg:=loc.register;
  752. (* we don't have enough type information to handle this here
  753. LOC_SUBSETREG,LOC_CSUBSETREG:
  754. begin
  755. tmpreg:=getintregister(list,loc.size);
  756. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  757. end;
  758. *)
  759. LOC_CONSTANT:
  760. begin
  761. tmpreg:=getintregister(list,locsize);
  762. a_load_const_reg(list,locsize,loc.value,tmpreg);
  763. end;
  764. end;
  765. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  766. end;
  767. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  768. else
  769. internalerror(2010120411);
  770. end;
  771. end;
  772. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  773. begin
  774. case loc.loc of
  775. LOC_REFERENCE,LOC_CREFERENCE:
  776. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  777. LOC_REGISTER,LOC_CREGISTER:
  778. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  779. (* we don't have enough type information to handle this here
  780. LOC_SUBSETREG,LOC_CSUBSETREG:
  781. a_bit_test_const_subsetreg_reg(list,loc.size,destsize,bitnumber,loc.sreg,destreg);
  782. *)
  783. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  784. else
  785. internalerror(2010120410);
  786. end;
  787. end;
  788. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  789. var
  790. tmpreg: tregister;
  791. begin
  792. case loc.loc of
  793. LOC_REFERENCE:
  794. a_bit_set_reg_ref(list,doset,fromsize,tosize,bitnumber,loc.reference);
  795. LOC_CREGISTER:
  796. a_bit_set_reg_reg(list,doset,fromsize,tosize,bitnumber,loc.register);
  797. (* we don't have enough type information to handle this here
  798. { e.g. a 2-byte set in a record regvar }
  799. LOC_CSUBSETREG:
  800. begin
  801. { hard to do in-place in a generic way, so operate on a copy }
  802. tmpreg:=getintregister(list,loc.size);
  803. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  804. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,tmpreg);
  805. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  806. end;
  807. *)
  808. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  809. else
  810. internalerror(2010120408)
  811. end;
  812. end;
  813. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  814. begin
  815. case loc.loc of
  816. LOC_REFERENCE:
  817. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  818. LOC_CREGISTER:
  819. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  820. (* we don't have enough type information to handle this here
  821. LOC_CSUBSETREG:
  822. a_bit_set_const_subsetreg(list,doset,loc.size,bitnumber,loc.sreg);
  823. *)
  824. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  825. else
  826. internalerror(2010120409)
  827. end;
  828. end;
  829. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  830. var
  831. reg: tregister;
  832. regsize: tdef;
  833. begin
  834. if (fromsize.size>=tosize.size) then
  835. regsize:=fromsize
  836. else
  837. regsize:=tosize;
  838. reg:=getfpuregister(list,regsize);
  839. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  840. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  841. end;
  842. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  843. begin
  844. case loc.loc of
  845. LOC_REFERENCE, LOC_CREFERENCE:
  846. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  847. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  848. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  849. else
  850. internalerror(2010120412);
  851. end;
  852. end;
  853. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  854. begin
  855. case loc.loc of
  856. LOC_REFERENCE, LOC_CREFERENCE:
  857. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  858. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  859. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  860. else
  861. internalerror(2010120413);
  862. end;
  863. end;
  864. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  865. var
  866. ref : treference;
  867. begin
  868. paramanager.alloccgpara(list,cgpara);
  869. case cgpara.location^.loc of
  870. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  871. begin
  872. cgpara.check_simple_location;
  873. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  874. end;
  875. LOC_REFERENCE,LOC_CREFERENCE:
  876. begin
  877. cgpara.check_simple_location;
  878. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  879. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  880. end;
  881. LOC_REGISTER,LOC_CREGISTER:
  882. begin
  883. { paramfpu_ref does the check_simpe_location check here if necessary }
  884. tg.GetTemp(list,fromsize.size,fromsize.alignment,tt_normal,ref);
  885. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  886. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  887. tg.Ungettemp(list,ref);
  888. end;
  889. else
  890. internalerror(2010120422);
  891. end;
  892. end;
  893. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  894. var
  895. href : treference;
  896. // hsize: tcgsize;
  897. begin
  898. case cgpara.location^.loc of
  899. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  900. begin
  901. cgpara.check_simple_location;
  902. paramanager.alloccgpara(list,cgpara);
  903. a_loadfpu_ref_reg(list,fromsize,cgpara.def,ref,cgpara.location^.register);
  904. end;
  905. LOC_REFERENCE,LOC_CREFERENCE:
  906. begin
  907. cgpara.check_simple_location;
  908. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  909. { concatcopy should choose the best way to copy the data }
  910. g_concatcopy(list,fromsize,ref,href);
  911. end;
  912. (* not yet supported
  913. LOC_REGISTER,LOC_CREGISTER:
  914. begin
  915. { force integer size }
  916. hsize:=int_cgsize(tcgsize2size[size]);
  917. {$ifndef cpu64bitalu}
  918. if (hsize in [OS_S64,OS_64]) then
  919. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  920. else
  921. {$endif not cpu64bitalu}
  922. begin
  923. cgpara.check_simple_location;
  924. a_load_ref_cgpara(list,hsize,ref,cgpara)
  925. end;
  926. end
  927. *)
  928. else
  929. internalerror(2010120423);
  930. end;
  931. end;
  932. (*
  933. procedure thlcgobj.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  934. begin
  935. cg.a_loadmm_reg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),reg1,reg2,shuffle);
  936. end;
  937. procedure thlcgobj.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  938. begin
  939. cg.a_loadmm_ref_reg(list,def_cgsize(fromsize),def_cgsize(tosize),ref,reg,shuffle);
  940. end;
  941. procedure thlcgobj.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  942. begin
  943. cg.a_loadmm_reg_ref(list,def_cgsize(fromsize),def_cgsize(tosize),reg,ref,shuffle);
  944. end;
  945. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  946. begin
  947. case loc.loc of
  948. LOC_MMREGISTER,LOC_CMMREGISTER:
  949. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  950. LOC_REFERENCE,LOC_CREFERENCE:
  951. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  952. LOC_REGISTER,LOC_CREGISTER:
  953. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  954. else
  955. internalerror(2010120414);
  956. end;
  957. end;
  958. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  959. begin
  960. case loc.loc of
  961. LOC_MMREGISTER,LOC_CMMREGISTER:
  962. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  963. LOC_REFERENCE,LOC_CREFERENCE:
  964. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  965. else
  966. internalerror(2010120415);
  967. end;
  968. end;
  969. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  970. var
  971. href : treference;
  972. begin
  973. cgpara.check_simple_location;
  974. paramanager.alloccgpara(list,cgpara);
  975. case cgpara.location^.loc of
  976. LOC_MMREGISTER,LOC_CMMREGISTER:
  977. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  978. LOC_REFERENCE,LOC_CREFERENCE:
  979. begin
  980. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  981. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  982. end;
  983. LOC_REGISTER,LOC_CREGISTER:
  984. begin
  985. if assigned(shuffle) and
  986. not shufflescalar(shuffle) then
  987. internalerror(2009112510);
  988. a_loadmm_reg_intreg(list,deomsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  989. end
  990. else
  991. internalerror(2010120427);
  992. end;
  993. end;
  994. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  995. var
  996. hr : tregister;
  997. hs : tmmshuffle;
  998. begin
  999. cgpara.check_simple_location;
  1000. hr:=cg.getmmregister(list,cgpara.size);
  1001. a_loadmm_ref_reg(list,deomsize,cgpara.def,ref,hr,shuffle);
  1002. if realshuffle(shuffle) then
  1003. begin
  1004. hs:=shuffle^;
  1005. removeshuffles(hs);
  1006. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  1007. end
  1008. else
  1009. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  1010. end;
  1011. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  1012. begin
  1013. {$ifdef extdebug}
  1014. if def_cgsize(fromsize)<>loc.size then
  1015. internalerror(2010112105);
  1016. {$endif}
  1017. cg.a_loadmm_loc_cgpara(list,loc,cgpara,shuffle);
  1018. end;
  1019. procedure thlcgobj.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1020. begin
  1021. cg.a_opmm_reg_reg(list,op,def_cgsize(size),src,dst,shuffle);
  1022. end;
  1023. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1024. begin
  1025. cg.a_opmm_ref_reg(list,op,def_cgsize(size),ref,reg,shuffle)
  1026. end;
  1027. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  1028. begin
  1029. cg.a_opmm_loc_reg(list,op,def_cgsize(size),loc,reg,shuffle);
  1030. end;
  1031. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1032. begin
  1033. cg.a_opmm_reg_ref(list,op,def_cgsize(size),reg,ref,shuffle);
  1034. end;
  1035. *)
  1036. (*
  1037. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1038. begin
  1039. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  1040. end;
  1041. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1042. begin
  1043. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  1044. end;
  1045. *)
  1046. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  1047. var
  1048. tmpreg : tregister;
  1049. begin
  1050. tmpreg:=getintregister(list,size);
  1051. a_load_ref_reg(list,size,size,ref,tmpreg);
  1052. a_op_const_reg(list,op,size,a,tmpreg);
  1053. a_load_reg_ref(list,size,size,tmpreg,ref);
  1054. end;
  1055. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sreg: tsubsetregister);
  1056. var
  1057. tmpreg: tregister;
  1058. begin
  1059. tmpreg:=getintregister(list,size);
  1060. a_load_subsetreg_reg(list,size,subsetsize,size,sreg,tmpreg);
  1061. a_op_const_reg(list,op,size,a,tmpreg);
  1062. a_load_reg_subsetreg(list,size,size,subsetsize,tmpreg,sreg);
  1063. end;
  1064. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sref: tsubsetreference);
  1065. var
  1066. tmpreg: tregister;
  1067. begin
  1068. tmpreg:=getintregister(list,size);
  1069. a_load_subsetref_reg(list,size,subsetsize,size,sref,tmpreg);
  1070. a_op_const_reg(list,op,size,a,tmpreg);
  1071. a_load_reg_subsetref(list,size,size,subsetsize,tmpreg,sref);
  1072. end;
  1073. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);
  1074. begin
  1075. case loc.loc of
  1076. LOC_REGISTER, LOC_CREGISTER:
  1077. a_op_const_reg(list,op,size,a,loc.register);
  1078. LOC_REFERENCE, LOC_CREFERENCE:
  1079. a_op_const_ref(list,op,size,a,loc.reference);
  1080. { we don't have enough type information to handle these here
  1081. LOC_SUBSETREG, LOC_CSUBSETREG:
  1082. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  1083. LOC_SUBSETREF, LOC_CSUBSETREF:
  1084. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  1085. }
  1086. else
  1087. internalerror(2010120428);
  1088. end;
  1089. end;
  1090. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  1091. var
  1092. tmpreg: tregister;
  1093. begin
  1094. case op of
  1095. OP_NOT,OP_NEG:
  1096. { handle it as "load ref,reg; op reg" }
  1097. begin
  1098. a_load_ref_reg(list,size,size,ref,reg);
  1099. a_op_reg_reg(list,op,size,reg,reg);
  1100. end;
  1101. else
  1102. begin
  1103. tmpreg:=getintregister(list,size);
  1104. a_load_ref_reg(list,size,size,ref,tmpreg);
  1105. a_op_reg_reg(list,op,size,tmpreg,reg);
  1106. end;
  1107. end;
  1108. end;
  1109. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1110. var
  1111. tmpreg: tregister;
  1112. begin
  1113. case op of
  1114. OP_NOT,OP_NEG:
  1115. { handle it as "load ref,reg; op reg" }
  1116. begin
  1117. a_load_ref_reg(list,size,size,ref,reg);
  1118. a_op_reg_reg(list,op,size,reg,reg);
  1119. end;
  1120. else
  1121. begin
  1122. tmpreg:=getintregister(list,size);
  1123. a_load_ref_reg(list,size,size,ref,tmpreg);
  1124. a_op_reg_reg(list,op,size,tmpreg,reg);
  1125. end;
  1126. end;
  1127. end;
  1128. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  1129. var
  1130. tmpreg: tregister;
  1131. begin
  1132. tmpreg:=getintregister(list,opsize);
  1133. a_load_subsetreg_reg(list,destsize,destsubsetsize,opsize,sreg,tmpreg);
  1134. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1135. a_load_reg_subsetreg(list,opsize,destsize,destsubsetsize,tmpreg,sreg);
  1136. end;
  1137. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  1138. var
  1139. tmpreg: tregister;
  1140. begin
  1141. tmpreg:=getintregister(list,opsize);
  1142. a_load_subsetref_reg(list,destsize,destsubsetsize,opsize,sref,tmpreg);
  1143. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1144. a_load_reg_subsetref(list,opsize,destsize,destsubsetsize,tmpreg,sref);
  1145. end;
  1146. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  1147. begin
  1148. case loc.loc of
  1149. LOC_REGISTER, LOC_CREGISTER:
  1150. a_op_reg_reg(list,op,size,reg,loc.register);
  1151. LOC_REFERENCE, LOC_CREFERENCE:
  1152. a_op_reg_ref(list,op,size,reg,loc.reference);
  1153. { we don't have enough type information to handle these here
  1154. LOC_SUBSETREG, LOC_CSUBSETREG:
  1155. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  1156. LOC_SUBSETREF, LOC_CSUBSETREF:
  1157. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  1158. }
  1159. else
  1160. internalerror(2010120429);
  1161. end;
  1162. end;
  1163. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  1164. var
  1165. tmpreg: tregister;
  1166. begin
  1167. case loc.loc of
  1168. LOC_REGISTER,LOC_CREGISTER:
  1169. a_op_ref_reg(list,op,size,ref,loc.register);
  1170. LOC_REFERENCE,LOC_CREFERENCE:
  1171. begin
  1172. tmpreg:=getintregister(list,size);
  1173. a_load_ref_reg(list,size,size,ref,tmpreg);
  1174. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  1175. end;
  1176. { we don't have enough type information to handle these here
  1177. LOC_SUBSETREG, LOC_CSUBSETREG:
  1178. begin
  1179. tmpreg:=getintregister(list,loc.size);
  1180. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1181. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1182. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  1183. end;
  1184. LOC_SUBSETREF, LOC_CSUBSETREF:
  1185. begin
  1186. tmpreg:=getintregister(list,loc.size);
  1187. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  1188. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1189. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  1190. end;
  1191. }
  1192. else
  1193. internalerror(2010120429);
  1194. end;
  1195. end;
  1196. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  1197. begin
  1198. a_load_reg_reg(list,size,size,src,dst);
  1199. a_op_const_reg(list,op,size,a,dst);
  1200. end;
  1201. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1202. var
  1203. tmpreg: tregister;
  1204. begin
  1205. if (dst<>src1) then
  1206. begin
  1207. a_load_reg_reg(list,size,size,src2,dst);
  1208. a_op_reg_reg(list,op,size,src1,dst);
  1209. end
  1210. else
  1211. begin
  1212. { can we do a direct operation on the target register ? }
  1213. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1214. a_op_reg_reg(list,op,size,src2,dst)
  1215. else
  1216. begin
  1217. tmpreg:=getintregister(list,size);
  1218. a_load_reg_reg(list,size,size,src2,tmpreg);
  1219. a_op_reg_reg(list,op,size,src1,tmpreg);
  1220. a_load_reg_reg(list,size,size,tmpreg,dst);
  1221. end;
  1222. end;
  1223. end;
  1224. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1225. begin
  1226. if not setflags then
  1227. a_op_const_reg_reg(list,op,size,a,src,dst)
  1228. else
  1229. internalerror(2010122910);
  1230. end;
  1231. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1232. begin
  1233. if not setflags then
  1234. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  1235. else
  1236. internalerror(2010122911);
  1237. end;
  1238. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1239. var
  1240. tmpreg: tregister;
  1241. begin
  1242. tmpreg:=getintregister(list,size);
  1243. a_load_const_reg(list,size,a,tmpreg);
  1244. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1245. end;
  1246. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  1247. var
  1248. tmpreg: tregister;
  1249. begin
  1250. tmpreg:=getintregister(list,size);
  1251. a_load_ref_reg(list,size,size,ref,tmpreg);
  1252. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1253. end;
  1254. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  1255. var
  1256. tmpreg : tregister;
  1257. begin
  1258. case loc.loc of
  1259. LOC_REGISTER,LOC_CREGISTER:
  1260. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1261. LOC_REFERENCE,LOC_CREFERENCE:
  1262. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1263. { we don't have enough type information to handle these here
  1264. LOC_SUBSETREG, LOC_CSUBSETREG:
  1265. begin
  1266. tmpreg:=getintregister(list,size);
  1267. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  1268. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1269. end;
  1270. LOC_SUBSETREF, LOC_CSUBSETREF:
  1271. begin
  1272. tmpreg:=getintregister(list,size);
  1273. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  1274. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1275. end;
  1276. }
  1277. else
  1278. internalerror(2010120430);
  1279. end;
  1280. end;
  1281. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1282. var
  1283. tmpreg: tregister;
  1284. begin
  1285. tmpreg:=getintregister(list,size);
  1286. a_load_ref_reg(list,size,size,ref,tmpreg);
  1287. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1288. end;
  1289. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1290. var
  1291. tmpreg: tregister;
  1292. begin
  1293. tmpreg:=getintregister(list,size);
  1294. a_load_ref_reg(list,size,size,ref,tmpreg);
  1295. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1296. end;
  1297. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  1298. var
  1299. tmpreg: tregister;
  1300. begin
  1301. tmpreg:=getintregister(list,cmpsize);
  1302. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,cmpsize,sreg,tmpreg);
  1303. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1304. end;
  1305. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  1306. var
  1307. tmpreg: tregister;
  1308. begin
  1309. tmpreg:=getintregister(list,cmpsize);
  1310. a_load_subsetref_reg(list,fromsize,fromsubsetsize,cmpsize,sref,tmpreg);
  1311. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1312. end;
  1313. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  1314. begin
  1315. case loc.loc of
  1316. LOC_REGISTER,
  1317. LOC_CREGISTER:
  1318. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1319. LOC_REFERENCE,
  1320. LOC_CREFERENCE :
  1321. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1322. LOC_CONSTANT:
  1323. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1324. { we don't have enough type information to handle these here
  1325. LOC_SUBSETREG,
  1326. LOC_CSUBSETREG:
  1327. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  1328. LOC_SUBSETREF,
  1329. LOC_CSUBSETREF:
  1330. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  1331. }
  1332. else
  1333. internalerror(2010120431);
  1334. end;
  1335. end;
  1336. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  1337. begin
  1338. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1339. end;
  1340. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  1341. var
  1342. tmpreg: tregister;
  1343. begin
  1344. case loc.loc of
  1345. LOC_REGISTER,LOC_CREGISTER:
  1346. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1347. LOC_REFERENCE,LOC_CREFERENCE:
  1348. begin
  1349. tmpreg:=getintregister(list,size);
  1350. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1351. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1352. end;
  1353. LOC_CONSTANT:
  1354. begin
  1355. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  1356. end
  1357. { we don't have enough type information to handle these here
  1358. LOC_SUBSETREG, LOC_CSUBSETREG:
  1359. begin
  1360. tmpreg:=getintregister(list, size);
  1361. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1362. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  1363. end;
  1364. LOC_SUBSETREF, LOC_CSUBSETREF:
  1365. begin
  1366. tmpreg:=getintregister(list, size);
  1367. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1368. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  1369. end;
  1370. }
  1371. else
  1372. internalerror(2010120432);
  1373. end;
  1374. end;
  1375. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1376. begin
  1377. {
  1378. if use_vectorfpu(size) then
  1379. a_loadmm_ref_ref()
  1380. else
  1381. }
  1382. if size.typ<>floatdef then
  1383. a_load_ref_ref(list,size,size,source,dest)
  1384. else
  1385. a_loadfpu_ref_ref(list,size,size,source,dest);
  1386. end;
  1387. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  1388. begin
  1389. g_concatcopy(list,size,source,dest);
  1390. end;
  1391. procedure thlcgobj.g_profilecode(list: TAsmList);
  1392. begin
  1393. end;
  1394. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  1395. var
  1396. item,
  1397. previtem : TCmdStrListItem;
  1398. begin
  1399. previtem:=nil;
  1400. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1401. while assigned(item) do
  1402. begin
  1403. {$ifdef arm}
  1404. if current_settings.cputype in cpu_thumb2 then
  1405. list.concat(tai_thumb_func.create);
  1406. {$endif arm}
  1407. { "double link" all procedure entry symbols via .reference }
  1408. { directives on darwin, because otherwise the linker }
  1409. { sometimes strips the procedure if only on of the symbols }
  1410. { is referenced }
  1411. if assigned(previtem) and
  1412. (target_info.system in systems_darwin) then
  1413. list.concat(tai_directive.create(asd_reference,item.str));
  1414. if (cs_profile in current_settings.moduleswitches) or
  1415. (po_global in current_procinfo.procdef.procoptions) then
  1416. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1417. else
  1418. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1419. if assigned(previtem) and
  1420. (target_info.system in systems_darwin) then
  1421. list.concat(tai_directive.create(asd_reference,previtem.str));
  1422. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1423. list.concat(Tai_function_name.create(item.str));
  1424. previtem:=item;
  1425. item := TCmdStrListItem(item.next);
  1426. end;
  1427. current_procinfo.procdef.procstarttai:=tai(list.last);
  1428. end;
  1429. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  1430. begin
  1431. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname(true)));
  1432. current_procinfo.procdef.procendtai:=tai(list.last);
  1433. if (current_module.islibrary) then
  1434. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1435. { setinitname may generate a new section -> don't add to the
  1436. current list, because we assume this remains a text section }
  1437. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname(false));
  1438. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1439. begin
  1440. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1441. not(current_module.islibrary) then
  1442. begin
  1443. new_section(list,sec_code,'',4);
  1444. list.concat(tai_symbol.createname_global(
  1445. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1446. { keep argc, argv and envp properly on the stack }
  1447. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1448. end;
  1449. end;
  1450. end;
  1451. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1452. begin
  1453. { add the procedure to the al_procedures }
  1454. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  1455. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname(true)),getprocalign);
  1456. current_asmdata.asmlists[al_procedures].concatlist(code);
  1457. { save local data (casetable) also in the same file }
  1458. if assigned(data) and
  1459. (not data.empty) then
  1460. current_asmdata.asmlists[al_procedures].concatlist(data);
  1461. end;
  1462. end.