hlcgobj.pas 77 KB

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