cgobj.pas 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the basic 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. Abstreact code generator unit. This contains the base class
  20. to implement for all new supported processors.
  21. WARNING: None of the routines implemented in these modules,
  22. or their descendants, should use the temp. allocator, as
  23. these routines may be called inside genentrycode, and the
  24. stack frame is already setup!
  25. }
  26. unit cgobj;
  27. {$i fpcdefs.inc}
  28. interface
  29. uses
  30. cclasses,globtype,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symbase,symtype,symdef,symtable,rgobj
  34. ;
  35. type
  36. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  37. {# @abstract(Abstract code generator)
  38. This class implements an abstract instruction generator. Some of
  39. the methods of this class are generic, while others must
  40. be overriden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. sould be @link(tcg64f32) and not @var(tcg).
  43. }
  44. tcg = class
  45. public
  46. alignment : talignment;
  47. rg : array[tregistertype] of trgobj;
  48. t_times : longint;
  49. {$ifdef flowgraph}
  50. aktflownode:word;
  51. {$endif}
  52. {************************************************}
  53. { basic routines }
  54. constructor create;
  55. {# Initialize the register allocators needed for the codegenerator.}
  56. procedure init_register_allocators;virtual;
  57. {# Clean up the register allocators needed for the codegenerator.}
  58. procedure done_register_allocators;virtual;
  59. {# 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. }
  60. procedure set_regalloc_extend_backwards(b: boolean);
  61. {$ifdef flowgraph}
  62. procedure init_flowgraph;
  63. procedure done_flowgraph;
  64. {$endif}
  65. {# Gets a register suitable to do integer operations on.}
  66. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  67. {# Gets a register suitable to do integer operations on.}
  68. function getaddressregister(list:TAsmList):Tregister;virtual;
  69. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  70. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  71. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;abstract;
  72. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  73. the cpu specific child cg object have such a method?}
  74. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  75. procedure add_move_instruction(instr:Taicpu);virtual;
  76. function uses_registers(rt:Tregistertype):boolean;virtual;
  77. {# Get a specific register.}
  78. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  79. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  80. {# Get multiple registers specified.}
  81. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  82. {# Free multiple registers specified.}
  83. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  84. procedure allocallcpuregisters(list:TAsmList);virtual;
  85. procedure deallocallcpuregisters(list:TAsmList);virtual;
  86. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  87. procedure translate_register(var reg : tregister);
  88. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  89. {# Emit a label to the instruction stream. }
  90. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  91. {# Allocates register r by inserting a pai_realloc record }
  92. procedure a_reg_alloc(list : TAsmList;r : tregister);
  93. {# Deallocates register r by inserting a pa_regdealloc record}
  94. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  95. { Synchronize register, make sure it is still valid }
  96. procedure a_reg_sync(list : TAsmList;r : tregister);
  97. {# Pass a parameter, which is located in a register, to a routine.
  98. This routine should push/send the parameter to the routine, as
  99. required by the specific processor ABI and routine modifiers.
  100. This must be overriden for each CPU target.
  101. @param(size size of the operand in the register)
  102. @param(r register source of the operand)
  103. @param(cgpara where the parameter will be stored)
  104. }
  105. procedure a_param_reg(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  106. {# Pass a parameter, which is a constant, to a routine.
  107. A generic version is provided. This routine should
  108. be overriden for optimization purposes if the cpu
  109. permits directly sending this type of parameter.
  110. @param(size size of the operand in constant)
  111. @param(a value of constant to send)
  112. @param(cgpara where the parameter will be stored)
  113. }
  114. procedure a_param_const(list : TAsmList;size : tcgsize;a : aint;const cgpara : TCGPara);virtual;
  115. {# Pass the value of a parameter, which is located in memory, to a routine.
  116. A generic version is provided. This routine should
  117. be overriden for optimization purposes if the cpu
  118. permits directly sending this type of parameter.
  119. @param(size size of the operand in constant)
  120. @param(r Memory reference of value to send)
  121. @param(cgpara where the parameter will be stored)
  122. }
  123. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  124. {# Pass the value of a parameter, which can be located either in a register or memory location,
  125. to a routine.
  126. A generic version is provided.
  127. @param(l location of the operand to send)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. @param(cgpara where the parameter will be stored)
  130. }
  131. procedure a_param_loc(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  132. {# Pass the address of a reference to a routine. This routine
  133. will calculate the address of the reference, and pass this
  134. calculated address as a parameter.
  135. A generic version is provided. This routine should
  136. be overriden for optimization purposes if the cpu
  137. permits directly sending this type of parameter.
  138. @param(r reference to get address from)
  139. @param(nr parameter number (starting from one) of routine (from left to right))
  140. }
  141. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  142. { Remarks:
  143. * If a method specifies a size you have only to take care
  144. of that number of bits, i.e. load_const_reg with OP_8 must
  145. only load the lower 8 bit of the specified register
  146. the rest of the register can be undefined
  147. if necessary the compiler will call a method
  148. to zero or sign extend the register
  149. * The a_load_XX_XX with OP_64 needn't to be
  150. implemented for 32 bit
  151. processors, the code generator takes care of that
  152. * the addr size is for work with the natural pointer
  153. size
  154. * the procedures without fpu/mm are only for integer usage
  155. * normally the first location is the source and the
  156. second the destination
  157. }
  158. {# Emits instruction to call the method specified by symbol name.
  159. This routine must be overriden for each new target cpu.
  160. There is no a_call_ref because loading the reference will use
  161. a temp register on most cpu's resulting in conflicts with the
  162. registers used for the parameters (PFV)
  163. }
  164. procedure a_call_name(list : TAsmList;const s : string);virtual; abstract;
  165. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  166. procedure a_call_ref(list : TAsmList;ref : treference);virtual; abstract;
  167. { same as a_call_name, might be overriden on certain architectures to emit
  168. static calls without usage of a got trampoline }
  169. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  170. { move instructions }
  171. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : aint;register : tregister);virtual; abstract;
  172. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : aint;const ref : treference);virtual;
  173. procedure a_load_const_loc(list : TAsmList;a : aint;const loc : tlocation);
  174. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  175. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  176. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  177. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  178. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  179. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  180. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  181. procedure a_load_loc_subsetreg(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  182. procedure a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  183. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  184. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); virtual;
  185. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister); virtual;
  186. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister); virtual;
  187. procedure a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference); virtual;
  188. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister); virtual;
  189. procedure a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: aint; const sreg: tsubsetregister); virtual;
  190. procedure a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation); virtual;
  191. procedure a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister); virtual;
  192. procedure a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference); virtual;
  193. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference); virtual;
  194. procedure a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference); virtual;
  195. procedure a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference); virtual;
  196. procedure a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: aint; const sref: tsubsetreference); virtual;
  197. procedure a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation); virtual;
  198. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  199. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  200. { fpu move instructions }
  201. procedure a_loadfpu_reg_reg(list: TAsmList; size:tcgsize; reg1, reg2: tregister); virtual; abstract;
  202. procedure a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  203. procedure a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  204. procedure a_loadfpu_loc_reg(list: TAsmList; const loc: tlocation; const reg: tregister);
  205. procedure a_loadfpu_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation);
  206. procedure a_paramfpu_reg(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  207. procedure a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  208. { vector register move instructions }
  209. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  210. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  211. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  212. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  213. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  214. procedure a_parammm_reg(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  215. procedure a_parammm_ref(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  216. procedure a_parammm_loc(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  217. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;abstract;
  218. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  219. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  220. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  221. { basic arithmetic operations }
  222. { note: for operators which require only one argument (not, neg), use }
  223. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  224. { that in this case the *second* operand is used as both source and }
  225. { destination (JM) }
  226. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: Aint; reg: TRegister); virtual; abstract;
  227. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: Aint; const ref: TReference); virtual;
  228. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sreg: tsubsetregister); virtual;
  229. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sref: tsubsetreference); virtual;
  230. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: Aint; const loc: tlocation);
  231. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  232. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  233. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  234. procedure a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister); virtual;
  235. procedure a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference); virtual;
  236. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  237. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  238. { trinary operations for processors that support them, 'emulated' }
  239. { on others. None with "ref" arguments since I don't think there }
  240. { are any processors that support it (JM) }
  241. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister); virtual;
  242. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  243. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  244. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  245. { comparison operations }
  246. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  247. l : tasmlabel);virtual; abstract;
  248. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  249. l : tasmlabel); virtual;
  250. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: aint; const loc: tlocation;
  251. l : tasmlabel);
  252. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  253. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  254. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  255. procedure a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel); virtual;
  256. procedure a_cmp_subsetref_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel); virtual;
  257. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  258. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  259. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  260. l : tasmlabel);
  261. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  262. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  263. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  264. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  265. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  266. }
  267. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  268. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  269. {
  270. This routine tries to optimize the op_const_reg/ref opcode, and should be
  271. called at the start of a_op_const_reg/ref. It returns the actual opcode
  272. to emit, and the constant value to emit. This function can opcode OP_NONE to
  273. remove the opcode and OP_MOVE to replace it with a simple load
  274. @param(op The opcode to emit, returns the opcode which must be emitted)
  275. @param(a The constant which should be emitted, returns the constant which must
  276. be emitted)
  277. }
  278. procedure optimize_op_const(var op: topcg; var a : aint);virtual;
  279. {#
  280. This routine is used in exception management nodes. It should
  281. save the exception reason currently in the FUNCTION_RETURN_REG. The
  282. save should be done either to a temp (pointed to by href).
  283. or on the stack (pushing the value on the stack).
  284. The size of the value to save is OS_S32. The default version
  285. saves the exception reason to a temp. memory area.
  286. }
  287. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  288. {#
  289. This routine is used in exception management nodes. It should
  290. save the exception reason constant. The
  291. save should be done either to a temp (pointed to by href).
  292. or on the stack (pushing the value on the stack).
  293. The size of the value to save is OS_S32. The default version
  294. saves the exception reason to a temp. memory area.
  295. }
  296. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: aint);virtual;
  297. {#
  298. This routine is used in exception management nodes. It should
  299. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  300. should either be in the temp. area (pointed to by href , href should
  301. *NOT* be freed) or on the stack (the value should be popped).
  302. The size of the value to save is OS_S32. The default version
  303. saves the exception reason to a temp. memory area.
  304. }
  305. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  306. procedure g_maybe_testself(list : TAsmList;reg:tregister);
  307. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  308. {# This should emit the opcode to copy len bytes from the source
  309. to destination.
  310. It must be overriden for each new target processor.
  311. @param(source Source reference of copy)
  312. @param(dest Destination reference of copy)
  313. }
  314. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);virtual; abstract;
  315. {# This should emit the opcode to copy len bytes from the an unaligned source
  316. to destination.
  317. It must be overriden for each new target processor.
  318. @param(source Source reference of copy)
  319. @param(dest Destination reference of copy)
  320. }
  321. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);virtual;
  322. {# This should emit the opcode to a shortrstring from the source
  323. to destination.
  324. @param(source Source reference of copy)
  325. @param(dest Destination reference of copy)
  326. }
  327. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  328. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  329. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  330. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);
  331. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);
  332. {# Generates range checking code. It is to note
  333. that this routine does not need to be overriden,
  334. as it takes care of everything.
  335. @param(p Node which contains the value to check)
  336. @param(todef Type definition of node to range check)
  337. }
  338. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  339. {# Generates overflow checking code for a node }
  340. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  341. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  342. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);virtual;
  343. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);virtual;
  344. {# Emits instructions when compilation is done in profile
  345. mode (this is set as a command line option). The default
  346. behavior does nothing, should be overriden as required.
  347. }
  348. procedure g_profilecode(list : TAsmList);virtual;
  349. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  350. @param(size Number of bytes to allocate)
  351. }
  352. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  353. {# Emits instruction for allocating the locals in entry
  354. code of a routine. This is one of the first
  355. routine called in @var(genentrycode).
  356. @param(localsize Number of bytes to allocate as locals)
  357. }
  358. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  359. {# Emits instructions for returning from a subroutine.
  360. Should also restore the framepointer and stack.
  361. @param(parasize Number of bytes of parameters to deallocate from stack)
  362. }
  363. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  364. {# This routine is called when generating the code for the entry point
  365. of a routine. It should save all registers which are not used in this
  366. routine, and which should be declared as saved in the std_saved_registers
  367. set.
  368. This routine is mainly used when linking to code which is generated
  369. by ABI-compliant compilers (like GCC), to make sure that the reserved
  370. registers of that ABI are not clobbered.
  371. @param(usedinproc Registers which are used in the code of this routine)
  372. }
  373. procedure g_save_standard_registers(list:TAsmList);virtual;
  374. {# This routine is called when generating the code for the exit point
  375. of a routine. It should restore all registers which were previously
  376. saved in @var(g_save_standard_registers).
  377. @param(usedinproc Registers which are used in the code of this routine)
  378. }
  379. procedure g_restore_standard_registers(list:TAsmList);virtual;
  380. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  381. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual;
  382. function g_indirect_sym_load(list:TAsmList;const symname: string): tregister;virtual;
  383. protected
  384. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  385. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  386. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  387. end;
  388. {$ifndef cpu64bit}
  389. {# @abstract(Abstract code generator for 64 Bit operations)
  390. This class implements an abstract code generator class
  391. for 64 Bit operations.
  392. }
  393. tcg64 = class
  394. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  395. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  396. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  397. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  398. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  399. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  400. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  401. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  402. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  403. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  404. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  405. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  406. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  407. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  408. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  409. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  410. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  411. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  412. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  413. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  414. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  415. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  416. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  417. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  418. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  419. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  420. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  421. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  422. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  423. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  424. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  425. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  426. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  427. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  428. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  429. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  430. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  431. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  432. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  433. procedure a_param64_reg(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  434. procedure a_param64_const(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  435. procedure a_param64_ref(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  436. procedure a_param64_loc(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  437. {
  438. This routine tries to optimize the const_reg opcode, and should be
  439. called at the start of a_op64_const_reg. It returns the actual opcode
  440. to emit, and the constant value to emit. If this routine returns
  441. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  442. @param(op The opcode to emit, returns the opcode which must be emitted)
  443. @param(a The constant which should be emitted, returns the constant which must
  444. be emitted)
  445. @param(reg The register to emit the opcode with, returns the register with
  446. which the opcode will be emitted)
  447. }
  448. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  449. { override to catch 64bit rangechecks }
  450. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  451. end;
  452. {$endif cpu64bit}
  453. var
  454. {# Main code generator class }
  455. cg : tcg;
  456. {$ifndef cpu64bit}
  457. {# Code generator class for all operations working with 64-Bit operands }
  458. cg64 : tcg64;
  459. {$endif cpu64bit}
  460. implementation
  461. uses
  462. globals,options,systems,
  463. verbose,defutil,paramgr,symsym,
  464. tgobj,cutils,procinfo;
  465. {*****************************************************************************
  466. basic functionallity
  467. ******************************************************************************}
  468. constructor tcg.create;
  469. begin
  470. end;
  471. {*****************************************************************************
  472. register allocation
  473. ******************************************************************************}
  474. procedure tcg.init_register_allocators;
  475. begin
  476. fillchar(rg,sizeof(rg),0);
  477. add_reg_instruction_hook:=@add_reg_instruction;
  478. end;
  479. procedure tcg.done_register_allocators;
  480. begin
  481. { Safety }
  482. fillchar(rg,sizeof(rg),0);
  483. add_reg_instruction_hook:=nil;
  484. end;
  485. {$ifdef flowgraph}
  486. procedure Tcg.init_flowgraph;
  487. begin
  488. aktflownode:=0;
  489. end;
  490. procedure Tcg.done_flowgraph;
  491. begin
  492. end;
  493. {$endif}
  494. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  495. begin
  496. if not assigned(rg[R_INTREGISTER]) then
  497. internalerror(200312122);
  498. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(size));
  499. end;
  500. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  501. begin
  502. if not assigned(rg[R_FPUREGISTER]) then
  503. internalerror(200312123);
  504. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(size));
  505. end;
  506. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  507. begin
  508. if not assigned(rg[R_MMREGISTER]) then
  509. internalerror(2003121214);
  510. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(size));
  511. end;
  512. function tcg.getaddressregister(list:TAsmList):Tregister;
  513. begin
  514. if assigned(rg[R_ADDRESSREGISTER]) then
  515. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  516. else
  517. begin
  518. if not assigned(rg[R_INTREGISTER]) then
  519. internalerror(200312121);
  520. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  521. end;
  522. end;
  523. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  524. var
  525. subreg:Tsubregister;
  526. begin
  527. subreg:=cgsize2subreg(size);
  528. result:=reg;
  529. setsubreg(result,subreg);
  530. { notify RA }
  531. if result<>reg then
  532. list.concat(tai_regalloc.resize(result));
  533. end;
  534. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  535. begin
  536. if not assigned(rg[getregtype(r)]) then
  537. internalerror(200312125);
  538. rg[getregtype(r)].getcpuregister(list,r);
  539. end;
  540. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  541. begin
  542. if not assigned(rg[getregtype(r)]) then
  543. internalerror(200312126);
  544. rg[getregtype(r)].ungetcpuregister(list,r);
  545. end;
  546. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  547. begin
  548. if assigned(rg[rt]) then
  549. rg[rt].alloccpuregisters(list,r)
  550. else
  551. internalerror(200310092);
  552. end;
  553. procedure tcg.allocallcpuregisters(list:TAsmList);
  554. begin
  555. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  556. {$ifndef i386}
  557. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  558. {$ifdef cpumm}
  559. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  560. {$endif cpumm}
  561. {$endif i386}
  562. end;
  563. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  564. begin
  565. if assigned(rg[rt]) then
  566. rg[rt].dealloccpuregisters(list,r)
  567. else
  568. internalerror(200310093);
  569. end;
  570. procedure tcg.deallocallcpuregisters(list:TAsmList);
  571. begin
  572. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  573. {$ifndef i386}
  574. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  575. {$ifdef cpumm}
  576. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  577. {$endif cpumm}
  578. {$endif i386}
  579. end;
  580. function tcg.uses_registers(rt:Tregistertype):boolean;
  581. begin
  582. if assigned(rg[rt]) then
  583. result:=rg[rt].uses_registers
  584. else
  585. result:=false;
  586. end;
  587. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  588. var
  589. rt : tregistertype;
  590. begin
  591. rt:=getregtype(r);
  592. { Only add it when a register allocator is configured.
  593. No IE can be generated, because the VMT is written
  594. without a valid rg[] }
  595. if assigned(rg[rt]) then
  596. rg[rt].add_reg_instruction(instr,r);
  597. end;
  598. procedure tcg.add_move_instruction(instr:Taicpu);
  599. var
  600. rt : tregistertype;
  601. begin
  602. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  603. if assigned(rg[rt]) then
  604. rg[rt].add_move_instruction(instr)
  605. else
  606. internalerror(200310095);
  607. end;
  608. procedure tcg.set_regalloc_extend_backwards(b: boolean);
  609. var
  610. rt : tregistertype;
  611. begin
  612. for rt:=low(rg) to high(rg) do
  613. begin
  614. if assigned(rg[rt]) then
  615. rg[rt].extend_live_range_backwards := b;;
  616. end;
  617. end;
  618. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  619. var
  620. rt : tregistertype;
  621. begin
  622. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  623. begin
  624. if assigned(rg[rt]) then
  625. rg[rt].do_register_allocation(list,headertai);
  626. end;
  627. { running the other register allocator passes could require addition int/addr. registers
  628. when spilling so run int/addr register allocation at the end }
  629. if assigned(rg[R_INTREGISTER]) then
  630. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  631. if assigned(rg[R_ADDRESSREGISTER]) then
  632. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  633. end;
  634. procedure tcg.translate_register(var reg : tregister);
  635. begin
  636. rg[getregtype(reg)].translate_register(reg);
  637. end;
  638. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  639. begin
  640. list.concat(tai_regalloc.alloc(r,nil));
  641. end;
  642. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  643. begin
  644. list.concat(tai_regalloc.dealloc(r,nil));
  645. end;
  646. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  647. var
  648. instr : tai;
  649. begin
  650. instr:=tai_regalloc.sync(r);
  651. list.concat(instr);
  652. add_reg_instruction(instr,r);
  653. end;
  654. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  655. begin
  656. list.concat(tai_label.create(l));
  657. end;
  658. {*****************************************************************************
  659. for better code generation these methods should be overridden
  660. ******************************************************************************}
  661. procedure tcg.a_param_reg(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  662. var
  663. ref : treference;
  664. begin
  665. cgpara.check_simple_location;
  666. case cgpara.location^.loc of
  667. LOC_REGISTER,LOC_CREGISTER:
  668. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  669. LOC_REFERENCE,LOC_CREFERENCE:
  670. begin
  671. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  672. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  673. end
  674. else
  675. internalerror(2002071004);
  676. end;
  677. end;
  678. procedure tcg.a_param_const(list : TAsmList;size : tcgsize;a : aint;const cgpara : TCGPara);
  679. var
  680. ref : treference;
  681. begin
  682. cgpara.check_simple_location;
  683. case cgpara.location^.loc of
  684. LOC_REGISTER,LOC_CREGISTER:
  685. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  686. LOC_REFERENCE,LOC_CREFERENCE:
  687. begin
  688. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  689. a_load_const_ref(list,cgpara.location^.size,a,ref);
  690. end
  691. else
  692. internalerror(2002071004);
  693. end;
  694. end;
  695. procedure tcg.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  696. var
  697. ref : treference;
  698. begin
  699. cgpara.check_simple_location;
  700. case cgpara.location^.loc of
  701. LOC_REGISTER,LOC_CREGISTER:
  702. a_load_ref_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  703. LOC_REFERENCE,LOC_CREFERENCE:
  704. begin
  705. reference_reset(ref);
  706. ref.base:=cgpara.location^.reference.index;
  707. ref.offset:=cgpara.location^.reference.offset;
  708. if (size <> OS_NO) and
  709. (tcgsize2size[size] < sizeof(aint)) then
  710. begin
  711. if (cgpara.size = OS_NO) or
  712. assigned(cgpara.location^.next) then
  713. internalerror(2006052401);
  714. a_load_ref_ref(list,size,cgpara.size,r,ref);
  715. end
  716. else
  717. { use concatcopy, because the parameter can be larger than }
  718. { what the OS_* constants can handle }
  719. g_concatcopy(list,r,ref,cgpara.intsize);
  720. end
  721. else
  722. internalerror(2002071004);
  723. end;
  724. end;
  725. procedure tcg.a_param_loc(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  726. begin
  727. case l.loc of
  728. LOC_REGISTER,
  729. LOC_CREGISTER :
  730. a_param_reg(list,l.size,l.register,cgpara);
  731. LOC_CONSTANT :
  732. a_param_const(list,l.size,l.value,cgpara);
  733. LOC_CREFERENCE,
  734. LOC_REFERENCE :
  735. a_param_ref(list,l.size,l.reference,cgpara);
  736. else
  737. internalerror(2002032211);
  738. end;
  739. end;
  740. procedure tcg.a_paramaddr_ref(list : TAsmList;const r : treference;const cgpara : TCGPara);
  741. var
  742. hr : tregister;
  743. begin
  744. cgpara.check_simple_location;
  745. hr:=getaddressregister(list);
  746. a_loadaddr_ref_reg(list,r,hr);
  747. a_param_reg(list,OS_ADDR,hr,cgpara);
  748. end;
  749. {****************************************************************************
  750. some generic implementations
  751. ****************************************************************************}
  752. {$ifopt r+}
  753. {$define rangeon}
  754. {$endif}
  755. {$ifopt q+}
  756. {$define overflowon}
  757. {$endif}
  758. procedure tcg.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  759. var
  760. bitmask: aint;
  761. tmpreg: tregister;
  762. stopbit: byte;
  763. begin
  764. tmpreg:=getintregister(list,sreg.subsetregsize);
  765. a_op_const_reg_reg(list,OP_SHR,sreg.subsetregsize,sreg.startbit,sreg.subsetreg,tmpreg);
  766. stopbit := sreg.startbit + sreg.bitlen;
  767. // on x86(64), 1 shl 32(64) = 1 instead of 0
  768. if (stopbit - sreg.startbit <> AIntBits) then
  769. bitmask := (aint(1) shl (stopbit - sreg.startbit)) - 1
  770. else
  771. bitmask := -1;
  772. a_op_const_reg(list,OP_AND,sreg.subsetregsize,bitmask,tmpreg);
  773. tmpreg := makeregsize(list,tmpreg,subsetsize);
  774. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,tmpreg,tmpreg);
  775. a_load_reg_reg(list,subsetsize,tosize,tmpreg,destreg);
  776. end;
  777. procedure tcg.a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister);
  778. var
  779. bitmask: aint;
  780. tmpreg: tregister;
  781. stopbit: byte;
  782. begin
  783. stopbit := sreg.startbit + sreg.bitlen;
  784. // on x86(64), 1 shl 32(64) = 1 instead of 0
  785. if (stopbit <> AIntBits) then
  786. bitmask := not(((aint(1) shl stopbit)-1) xor ((aint(1) shl sreg.startbit)-1))
  787. else
  788. bitmask := not(-1 xor ((aint(1) shl sreg.startbit)-1));
  789. tmpreg:=getintregister(list,sreg.subsetregsize);
  790. a_load_reg_reg(list,fromsize,sreg.subsetregsize,fromreg,tmpreg);
  791. a_op_const_reg(list,OP_SHL,sreg.subsetregsize,sreg.startbit,tmpreg);
  792. a_op_const_reg(list,OP_AND,sreg.subsetregsize,not(bitmask),tmpreg);
  793. a_op_const_reg(list,OP_AND,sreg.subsetregsize,bitmask,sreg.subsetreg);
  794. a_op_reg_reg(list,OP_OR,sreg.subsetregsize,tmpreg,sreg.subsetreg);
  795. end;
  796. procedure tcg.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister);
  797. var
  798. tmpreg: tregister;
  799. bitmask: aint;
  800. stopbit: byte;
  801. begin
  802. if (fromsreg.bitlen >= tosreg.bitlen) then
  803. begin
  804. tmpreg := getintregister(list,tosreg.subsetregsize);
  805. a_load_reg_reg(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tmpreg);
  806. if (fromsreg.startbit <= tosreg.startbit) then
  807. a_op_const_reg(list,OP_SHL,tosreg.subsetregsize,tosreg.startbit-fromsreg.startbit,tmpreg)
  808. else
  809. a_op_const_reg(list,OP_SHR,tosreg.subsetregsize,fromsreg.startbit-tosreg.startbit,tmpreg);
  810. stopbit := tosreg.startbit + tosreg.bitlen;
  811. // on x86(64), 1 shl 32(64) = 1 instead of 0
  812. if (stopbit <> AIntBits) then
  813. bitmask := not(((aint(1) shl stopbit)-1) xor ((aint(1) shl tosreg.startbit)-1))
  814. else
  815. bitmask := (aint(1) shl tosreg.startbit) - 1;
  816. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,bitmask,tosreg.subsetreg);
  817. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,not(bitmask),tmpreg);
  818. a_op_reg_reg(list,OP_OR,tosreg.subsetregsize,tmpreg,tosreg.subsetreg);
  819. end
  820. else
  821. begin
  822. tmpreg := getintregister(list,tosubsetsize);
  823. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  824. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  825. end;
  826. end;
  827. procedure tcg.a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference);
  828. var
  829. tmpreg: tregister;
  830. begin
  831. tmpreg := getintregister(list,tosize);
  832. a_load_subsetreg_reg(list,subsetsize,tosize,sreg,tmpreg);
  833. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  834. end;
  835. procedure tcg.a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister);
  836. var
  837. tmpreg: tregister;
  838. begin
  839. tmpreg := getintregister(list,subsetsize);
  840. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  841. a_load_reg_subsetreg(list,subsetsize,subsetsize,tmpreg,sreg);
  842. end;
  843. procedure tcg.a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: aint; const sreg: tsubsetregister);
  844. var
  845. bitmask: aint;
  846. stopbit: byte;
  847. begin
  848. stopbit := sreg.startbit + sreg.bitlen;
  849. // on x86(64), 1 shl 32(64) = 1 instead of 0
  850. if (stopbit <> AIntBits) then
  851. bitmask := not(((aint(1) shl stopbit)-1) xor ((aint(1) shl sreg.startbit)-1))
  852. else
  853. bitmask := (aint(1) shl sreg.startbit) - 1;
  854. a_op_const_reg(list,OP_AND,sreg.subsetregsize,bitmask,sreg.subsetreg);
  855. a_op_const_reg(list,OP_OR,sreg.subsetregsize,(a shl sreg.startbit) and not(bitmask),sreg.subsetreg);
  856. end;
  857. procedure tcg.a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  858. begin
  859. case loc.loc of
  860. LOC_REFERENCE,LOC_CREFERENCE:
  861. a_load_ref_subsetref(list,loc.size,subsetsize,loc.reference,sref);
  862. LOC_REGISTER,LOC_CREGISTER:
  863. a_load_reg_subsetref(list,loc.size,subsetsize,loc.register,sref);
  864. LOC_CONSTANT:
  865. a_load_const_subsetref(list,subsetsize,loc.value,sref);
  866. LOC_SUBSETREG,LOC_CSUBSETREG:
  867. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  868. LOC_SUBSETREF,LOC_CSUBSETREF:
  869. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  870. else
  871. internalerror(200608053);
  872. end;
  873. end;
  874. (*
  875. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  876. in memory. They are like a regular reference, but contain an extra bit
  877. offset (either constant -startbit- or variable -bitindexreg, always OS_INT)
  878. and a bit length (always constant).
  879. Bit packed values are stored differently in memory depending on whether we
  880. are on a big or a little endian system (compatible with at least GPC). The
  881. size of the basic working unit is always the smallest power-of-2 byte size
  882. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  883. bytes, 17..32 bits -> 4 bytes etc).
  884. On a big endian, 5-bit: values are stored like this:
  885. 11111222 22333334 44445555 56666677 77788888
  886. The leftmost bit of each 5-bit value corresponds to the most significant
  887. bit.
  888. On little endian, it goes like this:
  889. 22211111 43333322 55554444 77666665 88888777
  890. In this case, per byte the left-most bit is more significant than those on
  891. the right, but the bits in the next byte are all more significant than
  892. those in the previous byte (e.g., the 222 in the first byte are the low
  893. three bits of that value, while the 22 in the second byte are the upper
  894. three bits.
  895. Big endian, 9 bit values:
  896. 11111111 12222222 22333333 33344444 ...
  897. Little endian, 9 bit values:
  898. 11111111 22222221 33333322 44444333 ...
  899. This is memory representation and the 16 bit values are byteswapped.
  900. Similarly as in the previous case, the 2222222 string contains the lower
  901. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  902. registers (two 16 bit registers in the current implementation, although a
  903. single 32 bit register would be possible too, in particular if 32 bit
  904. alignment can be guaranteed), this becomes:
  905. 22222221 11111111 44444333 33333322 ...
  906. (l)ow u l l u l u
  907. The startbit/bitindex in a subsetreference always refers to
  908. a) on big endian: the most significant bit of the value
  909. (bits counted from left to right, both memory an registers)
  910. b) on little endia: the least significant bit when the value
  911. is loaded in a register (bit counted from right to left)
  912. Although a) results in more complex code for big endian systems, it's
  913. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  914. Apple's universal interfaces which depend on these layout differences).
  915. Note: when changing the loadsize calculated in get_subsetref_load_info,
  916. make sure the appropriate alignment is guaranteed, at least in case of
  917. {$defined cpurequiresproperalignment}.
  918. *)
  919. procedure tcg.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  920. var
  921. intloadsize: aint;
  922. begin
  923. intloadsize := packedbitsloadsize(sref.bitlen);
  924. {$ifdef cpurequiresproperalignment}
  925. { may need to be split into several smaller loads/stores }
  926. if intloadsize <> sref.ref.alignment then
  927. internalerror(2006082011);
  928. {$endif cpurequiresproperalignment}
  929. if (intloadsize = 0) then
  930. internalerror(2006081310);
  931. if (intloadsize > sizeof(aint)) then
  932. intloadsize := sizeof(aint);
  933. loadsize := int_cgsize(intloadsize);
  934. if (loadsize = OS_NO) then
  935. internalerror(2006081311);
  936. if (sref.bitlen > sizeof(aint)*8) then
  937. internalerror(2006081312);
  938. extra_load :=
  939. (intloadsize <> 1) and
  940. ((sref.bitindexreg <> NR_NO) or
  941. (byte(sref.startbit+sref.bitlen) > byte(intloadsize*8)));
  942. end;
  943. procedure tcg.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  944. var
  945. restbits: byte;
  946. begin
  947. if (target_info.endian = endian_big) then
  948. begin
  949. { valuereg contains the upper bits, extra_value_reg the lower }
  950. restbits := (sref.bitlen - (loadbitsize - sref.startbit));
  951. a_op_const_reg(list,OP_SHL,OS_INT,restbits,valuereg);
  952. { mask other bits }
  953. if (sref.bitlen <> AIntBits) then
  954. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,valuereg);
  955. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-restbits,extra_value_reg)
  956. end
  957. else
  958. begin
  959. { valuereg contains the lower bits, extra_value_reg the upper }
  960. a_op_const_reg(list,OP_SHR,OS_INT,sref.startbit,valuereg);
  961. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.startbit,extra_value_reg);
  962. { mask other bits }
  963. if (sref.bitlen <> AIntBits) then
  964. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,extra_value_reg);
  965. end;
  966. { merge }
  967. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  968. end;
  969. procedure tcg.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  970. var
  971. tmpreg: tregister;
  972. begin
  973. tmpreg := getintregister(list,OS_INT);
  974. if (target_info.endian = endian_big) then
  975. begin
  976. { since this is a dynamic index, it's possible that the value }
  977. { is entirely in valuereg. }
  978. { get the data in valuereg in the right place }
  979. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  980. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  981. if (loadbitsize <> AIntBits) then
  982. { mask left over bits }
  983. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,valuereg);
  984. tmpreg := getintregister(list,OS_INT);
  985. { the bits in extra_value_reg (if any) start at the most significant bit => }
  986. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  987. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  988. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  989. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  990. a_op_reg_reg(list,OP_SHR,OS_INT,tmpreg,extra_value_reg);
  991. { if there are no bits in extra_value_reg, then sref.bitindex was }
  992. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  993. { => extra_value_reg is now 0 }
  994. { merge }
  995. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  996. { no need to mask, necessary masking happened earlier on }
  997. end
  998. else
  999. begin
  1000. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1001. { Y-x = -(Y-x) }
  1002. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpreg);
  1003. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  1004. { tmpreg is in the range 1..<cpu_bitsize> -> will zero extra_value_reg }
  1005. { if all bits are in valuereg }
  1006. a_op_reg_reg(list,OP_SHL,OS_INT,tmpreg,extra_value_reg);
  1007. {$ifdef x86}
  1008. { on i386 "x shl 32 = x shl 0", on x86/64 "x shl 64 = x shl 0". Fix so it's 0. }
  1009. if (loadbitsize = AIntBits) then
  1010. begin
  1011. { if (tmpreg >= cpu_bit_size) then tmpreg := 1 else tmpreg := 0 }
  1012. a_op_const_reg(list,OP_SHR,OS_INT,{$ifdef cpu64bit}6{$else}5{$endif},tmpreg);
  1013. { if (tmpreg = cpu_bit_size) then tmpreg := 0 else tmpreg := -1 }
  1014. a_op_const_reg(list,OP_SUB,OS_INT,1,tmpreg);
  1015. { if (tmpreg = cpu_bit_size) then extra_value_reg := 0 }
  1016. a_op_reg_reg(list,OP_AND,OS_INT,tmpreg,extra_value_reg);
  1017. end;
  1018. {$endif x86}
  1019. { merge }
  1020. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1021. { mask other bits }
  1022. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,valuereg);
  1023. end;
  1024. end;
  1025. procedure tcg.a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister);
  1026. var
  1027. tmpref: treference;
  1028. valuereg,extra_value_reg: tregister;
  1029. tosreg: tsubsetregister;
  1030. loadsize: tcgsize;
  1031. loadbitsize: byte;
  1032. extra_load: boolean;
  1033. begin
  1034. get_subsetref_load_info(sref,loadsize,extra_load);
  1035. loadbitsize := tcgsize2size[loadsize]*8;
  1036. { load the (first part) of the bit sequence }
  1037. valuereg := cg.getintregister(list,OS_INT);
  1038. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1039. if not extra_load then
  1040. begin
  1041. { everything is guaranteed to be in a single register of loadsize }
  1042. if (sref.bitindexreg = NR_NO) then
  1043. begin
  1044. { use subsetreg routine, it may have been overridden with an optimized version }
  1045. tosreg.subsetreg := valuereg;
  1046. tosreg.subsetregsize := OS_INT;
  1047. { subsetregs always count bits from right to left }
  1048. if (target_info.endian = endian_big) then
  1049. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1050. else
  1051. tosreg.startbit := sref.startbit;
  1052. tosreg.bitlen := sref.bitlen;
  1053. a_load_subsetreg_reg(list,subsetsize,tosize,tosreg,destreg);
  1054. exit;
  1055. end
  1056. else
  1057. begin
  1058. if (sref.startbit <> 0) then
  1059. internalerror(2006081510);
  1060. if (target_info.endian = endian_big) then
  1061. begin
  1062. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  1063. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  1064. end
  1065. else
  1066. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1067. { mask other bits }
  1068. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,valuereg);
  1069. end
  1070. end
  1071. else
  1072. begin
  1073. { load next value as well }
  1074. extra_value_reg := getintregister(list,OS_INT);
  1075. tmpref := sref.ref;
  1076. inc(tmpref.offset,loadbitsize div 8);
  1077. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1078. if (sref.bitindexreg = NR_NO) then
  1079. { can be overridden to optimize }
  1080. a_load_subsetref_regs_noindex(list,subsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1081. else
  1082. begin
  1083. if (sref.startbit <> 0) then
  1084. internalerror(2006080610);
  1085. a_load_subsetref_regs_index(list,subsetsize,loadbitsize,sref,valuereg,extra_value_reg);
  1086. end;
  1087. end;
  1088. { store in destination }
  1089. { (types with a negative lower bound are always a base type (8, 16, 32, 64 bits) }
  1090. if ((sref.bitlen mod 8) = 0) then
  1091. begin
  1092. { since we know all necessary bits are already masked, avoid unnecessary }
  1093. { zero-extensions }
  1094. valuereg := makeregsize(list,valuereg,tosize);
  1095. a_load_reg_reg(list,tcgsize2unsigned[tosize],tosize,valuereg,destreg)
  1096. end
  1097. else
  1098. begin
  1099. { avoid unnecessary sign extension and zeroing }
  1100. valuereg := makeregsize(list,valuereg,OS_INT);
  1101. destreg := makeregsize(list,destreg,OS_INT);
  1102. a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1103. destreg := makeregsize(list,destreg,tosize);
  1104. end
  1105. end;
  1106. procedure tcg.a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference);
  1107. var
  1108. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1109. tosreg, fromsreg: tsubsetregister;
  1110. tmpref: treference;
  1111. loadsize: tcgsize;
  1112. loadbitsize: byte;
  1113. extra_load: boolean;
  1114. begin
  1115. { the register must be able to contain the requested value }
  1116. if (tcgsize2size[fromsize]*8 < sref.bitlen) then
  1117. internalerror(2006081613);
  1118. get_subsetref_load_info(sref,loadsize,extra_load);
  1119. loadbitsize := tcgsize2size[loadsize]*8;
  1120. { load the (first part) of the bit sequence }
  1121. valuereg := cg.getintregister(list,OS_INT);
  1122. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1123. { constant offset of bit sequence? }
  1124. if not extra_load then
  1125. begin
  1126. if (sref.bitindexreg = NR_NO) then
  1127. begin
  1128. { use subsetreg routine, it may have been overridden with an optimized version }
  1129. tosreg.subsetreg := valuereg;
  1130. tosreg.subsetregsize := OS_INT;
  1131. { subsetregs always count bits from right to left }
  1132. if (target_info.endian = endian_big) then
  1133. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1134. else
  1135. tosreg.startbit := sref.startbit;
  1136. tosreg.bitlen := sref.bitlen;
  1137. a_load_reg_subsetreg(list,fromsize,subsetsize,fromreg,tosreg);
  1138. end
  1139. else
  1140. begin
  1141. if (sref.startbit <> 0) then
  1142. internalerror(2006081710);
  1143. { should be handled by normal code and will give wrong result }
  1144. { on x86 for the '1 shl bitlen' below }
  1145. if (sref.bitlen = AIntBits) then
  1146. internalerror(2006081711);
  1147. { calculated correct shiftcount for big endian }
  1148. tmpindexreg := getintregister(list,OS_INT);
  1149. a_load_reg_reg(list,OS_INT,OS_INT,sref.bitindexreg,tmpindexreg);
  1150. if (target_info.endian = endian_big) then
  1151. begin
  1152. a_op_const_reg(list,OP_SUB,OS_INT,loadbitsize-sref.bitlen,tmpindexreg);
  1153. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1154. end;
  1155. { zero the bits we have to insert }
  1156. maskreg := getintregister(list,OS_INT);
  1157. a_load_const_reg(list,OS_INT,(aint(1) shl sref.bitlen)-1,maskreg);
  1158. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,maskreg);
  1159. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1160. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1161. { insert the value }
  1162. tmpreg := getintregister(list,OS_INT);
  1163. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg);
  1164. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,tmpreg);
  1165. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,tmpreg);
  1166. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1167. end;
  1168. { store back to memory }
  1169. valuereg := makeregsize(list,valuereg,loadsize);
  1170. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1171. exit;
  1172. end
  1173. else
  1174. begin
  1175. { load next value }
  1176. extra_value_reg := getintregister(list,OS_INT);
  1177. tmpref := sref.ref;
  1178. inc(tmpref.offset,loadbitsize div 8);
  1179. { should maybe be taken out too, can be done more efficiently }
  1180. { on e.g. i386 with shld/shrd }
  1181. if (sref.bitindexreg = NR_NO) then
  1182. begin
  1183. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1184. fromsreg.subsetreg := fromreg;
  1185. fromsreg.subsetregsize := fromsize;
  1186. tosreg.subsetreg := valuereg;
  1187. tosreg.subsetregsize := OS_INT;
  1188. { transfer first part }
  1189. fromsreg.bitlen := loadbitsize-sref.startbit;
  1190. tosreg.bitlen := fromsreg.bitlen;
  1191. if (target_info.endian = endian_big) then
  1192. begin
  1193. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1194. { upper bits of the value ... }
  1195. fromsreg.startbit := sref.bitlen-(loadbitsize-sref.startbit);
  1196. { ... to bit 0 }
  1197. tosreg.startbit := 0
  1198. end
  1199. else
  1200. begin
  1201. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1202. { lower bits of the value ... }
  1203. fromsreg.startbit := 0;
  1204. { ... to startbit }
  1205. tosreg.startbit := sref.startbit;
  1206. end;
  1207. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1208. valuereg := makeregsize(list,valuereg,loadsize);
  1209. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1210. { transfer second part }
  1211. if (target_info.endian = endian_big) then
  1212. begin
  1213. { extra_value_reg must contain the lower bits of the value at bits }
  1214. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1215. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1216. { - bitlen - startbit }
  1217. fromsreg.startbit := 0;
  1218. tosreg.startbit := 2*loadbitsize - sref.bitlen - sref.startbit
  1219. end
  1220. else
  1221. begin
  1222. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1223. fromsreg.startbit := fromsreg.bitlen;
  1224. tosreg.startbit := 0;
  1225. end;
  1226. tosreg.subsetreg := extra_value_reg;
  1227. fromsreg.bitlen := sref.bitlen-fromsreg.bitlen;
  1228. tosreg.bitlen := fromsreg.bitlen;
  1229. a_load_subsetreg_subsetreg(list,fromsize,subsetsize,fromsreg,tosreg);
  1230. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1231. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1232. exit;
  1233. end
  1234. else
  1235. begin
  1236. if (sref.startbit <> 0) then
  1237. internalerror(2006081812);
  1238. { should be handled by normal code and will give wrong result }
  1239. { on x86 for the '1 shl bitlen' below }
  1240. if (sref.bitlen = AIntBits) then
  1241. internalerror(2006081713);
  1242. { generate mask to zero the bits we have to insert }
  1243. maskreg := getintregister(list,OS_INT);
  1244. if (target_info.endian = endian_big) then
  1245. begin
  1246. a_load_const_reg(list,OS_INT,((aint(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1247. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,maskreg);
  1248. end
  1249. else
  1250. begin
  1251. a_load_const_reg(list,OS_INT,(aint(1) shl sref.bitlen)-1,maskreg);
  1252. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,maskreg);
  1253. end;
  1254. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1255. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1256. { insert the value }
  1257. tmpreg := getintregister(list,OS_INT);
  1258. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg);
  1259. if (target_info.endian = endian_big) then
  1260. begin
  1261. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.bitlen,tmpreg);
  1262. if (loadbitsize <> AIntBits) then
  1263. { mask left over bits }
  1264. a_op_const_reg(list,OP_AND,OS_INT,((aint(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),tmpreg);
  1265. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,tmpreg);
  1266. end
  1267. else
  1268. begin
  1269. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,tmpreg);
  1270. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,tmpreg);
  1271. end;
  1272. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1273. valuereg := makeregsize(list,valuereg,loadsize);
  1274. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1275. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1276. tmpindexreg := getintregister(list,OS_INT);
  1277. { load current array value }
  1278. tmpreg := getintregister(list,OS_INT);
  1279. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg);
  1280. { generate mask to zero the bits we have to insert }
  1281. maskreg := getintregister(list,OS_INT);
  1282. if (target_info.endian = endian_big) then
  1283. begin
  1284. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1285. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1286. a_load_const_reg(list,OS_INT,(aint(1) shl sref.bitlen)-1,maskreg);
  1287. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,maskreg);
  1288. end
  1289. else
  1290. begin
  1291. { Y-x = -(Y-x) }
  1292. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpindexreg);
  1293. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1294. a_load_const_reg(list,OS_INT,(aint(1) shl sref.bitlen)-1,maskreg);
  1295. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,maskreg);
  1296. {$ifdef x86}
  1297. { on i386 "x shl 32 = x shl 0", on x86/64 "x shl 64 = x shl 0". Fix so it's 0. }
  1298. if (loadbitsize = AIntBits) then
  1299. begin
  1300. valuereg := getintregister(list,OS_INT);
  1301. { if (tmpreg >= cpu_bit_size) then valuereg := 1 else valuereg := 0 }
  1302. a_op_const_reg_reg(list,OP_SHR,OS_INT,{$ifdef cpu64bit}6{$else}5{$endif},tmpindexreg,valuereg);
  1303. { if (tmpreg = cpu_bit_size) then valuereg := 0 else valuereg := -1 }
  1304. a_op_const_reg(list,OP_SUB,OS_INT,1,valuereg);
  1305. { if (tmpreg = cpu_bit_size) then tmpreg := maskreg := 0 }
  1306. a_op_reg_reg(list,OP_AND,OS_INT,valuereg,tmpreg);
  1307. a_op_reg_reg(list,OP_AND,OS_INT,valuereg,maskreg);
  1308. end;
  1309. {$endif x86}
  1310. end;
  1311. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1312. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,extra_value_reg);
  1313. if (target_info.endian = endian_big) then
  1314. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,tmpreg)
  1315. else
  1316. begin
  1317. a_op_const_reg(list,OP_AND,OS_INT,(aint(1) shl sref.bitlen)-1,tmpreg);
  1318. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,tmpreg);
  1319. end;
  1320. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,extra_value_reg);
  1321. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1322. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1323. end;
  1324. end;
  1325. end;
  1326. procedure tcg.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference);
  1327. var
  1328. tmpreg: tregister;
  1329. begin
  1330. tmpreg := getintregister(list,tosubsetsize);
  1331. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1332. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1333. end;
  1334. procedure tcg.a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference);
  1335. var
  1336. tmpreg: tregister;
  1337. begin
  1338. tmpreg := getintregister(list,tosize);
  1339. a_load_subsetref_reg(list,subsetsize,tosize,sref,tmpreg);
  1340. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1341. end;
  1342. procedure tcg.a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference);
  1343. var
  1344. tmpreg: tregister;
  1345. begin
  1346. tmpreg := getintregister(list,subsetsize);
  1347. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  1348. a_load_reg_subsetref(list,subsetsize,subsetsize,tmpreg,sref);
  1349. end;
  1350. procedure tcg.a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: aint; const sref: tsubsetreference);
  1351. var
  1352. tmpreg: tregister;
  1353. begin
  1354. tmpreg := getintregister(list,subsetsize);
  1355. a_load_const_reg(list,subsetsize,a,tmpreg);
  1356. a_load_reg_subsetref(list,subsetsize,subsetsize,tmpreg,sref);
  1357. end;
  1358. procedure tcg.a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation);
  1359. begin
  1360. case loc.loc of
  1361. LOC_REFERENCE,LOC_CREFERENCE:
  1362. a_load_subsetref_ref(list,subsetsize,loc.size,sref,loc.reference);
  1363. LOC_REGISTER,LOC_CREGISTER:
  1364. a_load_subsetref_reg(list,subsetsize,loc.size,sref,loc.register);
  1365. LOC_SUBSETREG,LOC_CSUBSETREG:
  1366. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  1367. LOC_SUBSETREF,LOC_CSUBSETREF:
  1368. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  1369. else
  1370. internalerror(200608054);
  1371. end;
  1372. end;
  1373. procedure tcg.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1374. var
  1375. tmpreg: tregister;
  1376. begin
  1377. tmpreg := getintregister(list,tosubsetsize);
  1378. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1379. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1380. end;
  1381. procedure tcg.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1382. var
  1383. tmpreg: tregister;
  1384. begin
  1385. tmpreg := getintregister(list,tosubsetsize);
  1386. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1387. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1388. end;
  1389. {$ifdef rangeon}
  1390. {$r+}
  1391. {$undef rangeon}
  1392. {$endif}
  1393. {$ifdef overflowon}
  1394. {$q+}
  1395. {$undef overflowon}
  1396. {$endif}
  1397. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1398. var
  1399. tmpreg: tregister;
  1400. begin
  1401. { verify if we have the same reference }
  1402. if references_equal(sref,dref) then
  1403. exit;
  1404. tmpreg:=getintregister(list,tosize);
  1405. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1406. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1407. end;
  1408. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : aint;const ref : treference);
  1409. var
  1410. tmpreg: tregister;
  1411. begin
  1412. tmpreg:=getintregister(list,size);
  1413. a_load_const_reg(list,size,a,tmpreg);
  1414. a_load_reg_ref(list,size,size,tmpreg,ref);
  1415. end;
  1416. procedure tcg.a_load_const_loc(list : TAsmList;a : aint;const loc: tlocation);
  1417. begin
  1418. case loc.loc of
  1419. LOC_REFERENCE,LOC_CREFERENCE:
  1420. a_load_const_ref(list,loc.size,a,loc.reference);
  1421. LOC_REGISTER,LOC_CREGISTER:
  1422. a_load_const_reg(list,loc.size,a,loc.register);
  1423. LOC_SUBSETREG,LOC_CSUBSETREG:
  1424. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  1425. LOC_SUBSETREF,LOC_CSUBSETREF:
  1426. a_load_const_subsetref(list,loc.size,a,loc.sref);
  1427. else
  1428. internalerror(200203272);
  1429. end;
  1430. end;
  1431. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1432. begin
  1433. case loc.loc of
  1434. LOC_REFERENCE,LOC_CREFERENCE:
  1435. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1436. LOC_REGISTER,LOC_CREGISTER:
  1437. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1438. LOC_SUBSETREG,LOC_CSUBSETREG:
  1439. a_load_reg_subsetreg(list,fromsize,loc.size,reg,loc.sreg);
  1440. LOC_SUBSETREF,LOC_CSUBSETREF:
  1441. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  1442. else
  1443. internalerror(200203271);
  1444. end;
  1445. end;
  1446. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1447. begin
  1448. case loc.loc of
  1449. LOC_REFERENCE,LOC_CREFERENCE:
  1450. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1451. LOC_REGISTER,LOC_CREGISTER:
  1452. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1453. LOC_CONSTANT:
  1454. a_load_const_reg(list,tosize,loc.value,reg);
  1455. LOC_SUBSETREG,LOC_CSUBSETREG:
  1456. a_load_subsetreg_reg(list,loc.size,tosize,loc.sreg,reg);
  1457. LOC_SUBSETREF,LOC_CSUBSETREF:
  1458. a_load_subsetref_reg(list,loc.size,tosize,loc.sref,reg);
  1459. else
  1460. internalerror(200109092);
  1461. end;
  1462. end;
  1463. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1464. begin
  1465. case loc.loc of
  1466. LOC_REFERENCE,LOC_CREFERENCE:
  1467. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1468. LOC_REGISTER,LOC_CREGISTER:
  1469. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1470. LOC_CONSTANT:
  1471. a_load_const_ref(list,tosize,loc.value,ref);
  1472. LOC_SUBSETREG,LOC_CSUBSETREG:
  1473. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  1474. LOC_SUBSETREF,LOC_CSUBSETREF:
  1475. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  1476. else
  1477. internalerror(200109302);
  1478. end;
  1479. end;
  1480. procedure tcg.a_load_loc_subsetreg(list : TAsmList; subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  1481. begin
  1482. case loc.loc of
  1483. LOC_REFERENCE,LOC_CREFERENCE:
  1484. a_load_ref_subsetreg(list,loc.size,subsetsize,loc.reference,sreg);
  1485. LOC_REGISTER,LOC_CREGISTER:
  1486. a_load_reg_subsetreg(list,loc.size,subsetsize,loc.register,sreg);
  1487. LOC_CONSTANT:
  1488. a_load_const_subsetreg(list,subsetsize,loc.value,sreg);
  1489. LOC_SUBSETREG,LOC_CSUBSETREG:
  1490. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  1491. LOC_SUBSETREF,LOC_CSUBSETREF:
  1492. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  1493. else
  1494. internalerror(2006052310);
  1495. end;
  1496. end;
  1497. procedure tcg.a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation);
  1498. begin
  1499. case loc.loc of
  1500. LOC_REFERENCE,LOC_CREFERENCE:
  1501. a_load_subsetreg_ref(list,subsetsize,loc.size,sreg,loc.reference);
  1502. LOC_REGISTER,LOC_CREGISTER:
  1503. a_load_subsetreg_reg(list,subsetsize,loc.size,sreg,loc.register);
  1504. LOC_SUBSETREG,LOC_CSUBSETREG:
  1505. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  1506. LOC_SUBSETREF,LOC_CSUBSETREF:
  1507. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  1508. else
  1509. internalerror(2006051510);
  1510. end;
  1511. end;
  1512. procedure tcg.optimize_op_const(var op: topcg; var a : aint);
  1513. var
  1514. powerval : longint;
  1515. begin
  1516. case op of
  1517. OP_OR :
  1518. begin
  1519. { or with zero returns same result }
  1520. if a = 0 then
  1521. op:=OP_NONE
  1522. else
  1523. { or with max returns max }
  1524. if a = -1 then
  1525. op:=OP_MOVE;
  1526. end;
  1527. OP_AND :
  1528. begin
  1529. { and with max returns same result }
  1530. if (a = -1) then
  1531. op:=OP_NONE
  1532. else
  1533. { and with 0 returns 0 }
  1534. if a=0 then
  1535. op:=OP_MOVE;
  1536. end;
  1537. OP_DIV :
  1538. begin
  1539. { division by 1 returns result }
  1540. if a = 1 then
  1541. op:=OP_NONE
  1542. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in aktlocalswitches) then
  1543. begin
  1544. a := powerval;
  1545. op:= OP_SHR;
  1546. end;
  1547. end;
  1548. OP_IDIV:
  1549. begin
  1550. if a = 1 then
  1551. op:=OP_NONE;
  1552. end;
  1553. OP_MUL,OP_IMUL:
  1554. begin
  1555. if a = 1 then
  1556. op:=OP_NONE
  1557. else
  1558. if a=0 then
  1559. op:=OP_MOVE
  1560. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in aktlocalswitches) then
  1561. begin
  1562. a := powerval;
  1563. op:= OP_SHL;
  1564. end;
  1565. end;
  1566. OP_ADD,OP_SUB:
  1567. begin
  1568. if a = 0 then
  1569. op:=OP_NONE;
  1570. end;
  1571. OP_SAR,OP_SHL,OP_SHR:
  1572. begin
  1573. if a = 0 then
  1574. op:=OP_NONE;
  1575. end;
  1576. end;
  1577. end;
  1578. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; const loc: tlocation; const reg: tregister);
  1579. begin
  1580. case loc.loc of
  1581. LOC_REFERENCE, LOC_CREFERENCE:
  1582. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  1583. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1584. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  1585. else
  1586. internalerror(200203301);
  1587. end;
  1588. end;
  1589. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation);
  1590. begin
  1591. case loc.loc of
  1592. LOC_REFERENCE, LOC_CREFERENCE:
  1593. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  1594. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1595. a_loadfpu_reg_reg(list,size,reg,loc.register);
  1596. else
  1597. internalerror(48991);
  1598. end;
  1599. end;
  1600. procedure tcg.a_paramfpu_reg(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1601. var
  1602. ref : treference;
  1603. begin
  1604. case cgpara.location^.loc of
  1605. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1606. begin
  1607. cgpara.check_simple_location;
  1608. a_loadfpu_reg_reg(list,size,r,cgpara.location^.register);
  1609. end;
  1610. LOC_REFERENCE,LOC_CREFERENCE:
  1611. begin
  1612. cgpara.check_simple_location;
  1613. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1614. a_loadfpu_reg_ref(list,size,r,ref);
  1615. end;
  1616. LOC_REGISTER,LOC_CREGISTER:
  1617. begin
  1618. { paramfpu_ref does the check_simpe_location check here if necessary }
  1619. tg.GetTemp(list,TCGSize2Size[size],tt_normal,ref);
  1620. a_loadfpu_reg_ref(list,size,r,ref);
  1621. a_paramfpu_ref(list,size,ref,cgpara);
  1622. tg.Ungettemp(list,ref);
  1623. end;
  1624. else
  1625. internalerror(2002071004);
  1626. end;
  1627. end;
  1628. procedure tcg.a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1629. var
  1630. href : treference;
  1631. begin
  1632. cgpara.check_simple_location;
  1633. case cgpara.location^.loc of
  1634. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1635. a_loadfpu_ref_reg(list,size,ref,cgpara.location^.register);
  1636. LOC_REFERENCE,LOC_CREFERENCE:
  1637. begin
  1638. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1639. { concatcopy should choose the best way to copy the data }
  1640. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1641. end;
  1642. else
  1643. internalerror(200402201);
  1644. end;
  1645. end;
  1646. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1647. var
  1648. tmpreg : tregister;
  1649. begin
  1650. tmpreg:=getintregister(list,size);
  1651. a_load_ref_reg(list,size,size,ref,tmpreg);
  1652. a_op_const_reg(list,op,size,a,tmpreg);
  1653. a_load_reg_ref(list,size,size,tmpreg,ref);
  1654. end;
  1655. procedure tcg.a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sreg: tsubsetregister);
  1656. var
  1657. tmpreg: tregister;
  1658. begin
  1659. tmpreg := cg.getintregister(list, size);
  1660. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  1661. a_op_const_reg(list,op,size,a,tmpreg);
  1662. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  1663. end;
  1664. procedure tcg.a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sref: tsubsetreference);
  1665. var
  1666. tmpreg: tregister;
  1667. begin
  1668. tmpreg := cg.getintregister(list, size);
  1669. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  1670. a_op_const_reg(list,op,size,a,tmpreg);
  1671. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  1672. end;
  1673. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: aint; const loc: tlocation);
  1674. begin
  1675. case loc.loc of
  1676. LOC_REGISTER, LOC_CREGISTER:
  1677. a_op_const_reg(list,op,loc.size,a,loc.register);
  1678. LOC_REFERENCE, LOC_CREFERENCE:
  1679. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1680. LOC_SUBSETREG, LOC_CSUBSETREG:
  1681. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  1682. LOC_SUBSETREF, LOC_CSUBSETREF:
  1683. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  1684. else
  1685. internalerror(200109061);
  1686. end;
  1687. end;
  1688. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1689. var
  1690. tmpreg : tregister;
  1691. begin
  1692. tmpreg:=getintregister(list,size);
  1693. a_load_ref_reg(list,size,size,ref,tmpreg);
  1694. a_op_reg_reg(list,op,size,reg,tmpreg);
  1695. a_load_reg_ref(list,size,size,tmpreg,ref);
  1696. end;
  1697. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1698. var
  1699. tmpreg: tregister;
  1700. begin
  1701. case op of
  1702. OP_NOT,OP_NEG:
  1703. { handle it as "load ref,reg; op reg" }
  1704. begin
  1705. a_load_ref_reg(list,size,size,ref,reg);
  1706. a_op_reg_reg(list,op,size,reg,reg);
  1707. end;
  1708. else
  1709. begin
  1710. tmpreg:=getintregister(list,size);
  1711. a_load_ref_reg(list,size,size,ref,tmpreg);
  1712. a_op_reg_reg(list,op,size,tmpreg,reg);
  1713. end;
  1714. end;
  1715. end;
  1716. procedure tcg.a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister);
  1717. var
  1718. tmpreg: tregister;
  1719. begin
  1720. tmpreg := cg.getintregister(list, opsize);
  1721. a_load_subsetreg_reg(list,subsetsize,opsize,sreg,tmpreg);
  1722. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1723. a_load_reg_subsetreg(list,opsize,subsetsize,tmpreg,sreg);
  1724. end;
  1725. procedure tcg.a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference);
  1726. var
  1727. tmpreg: tregister;
  1728. begin
  1729. tmpreg := cg.getintregister(list, opsize);
  1730. a_load_subsetref_reg(list,subsetsize,opsize,sref,tmpreg);
  1731. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1732. a_load_reg_subsetref(list,opsize,subsetsize,tmpreg,sref);
  1733. end;
  1734. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1735. begin
  1736. case loc.loc of
  1737. LOC_REGISTER, LOC_CREGISTER:
  1738. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1739. LOC_REFERENCE, LOC_CREFERENCE:
  1740. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1741. LOC_SUBSETREG, LOC_CSUBSETREG:
  1742. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  1743. LOC_SUBSETREF, LOC_CSUBSETREF:
  1744. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  1745. else
  1746. internalerror(200109061);
  1747. end;
  1748. end;
  1749. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1750. var
  1751. tmpreg: tregister;
  1752. begin
  1753. case loc.loc of
  1754. LOC_REGISTER,LOC_CREGISTER:
  1755. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1756. LOC_REFERENCE,LOC_CREFERENCE:
  1757. begin
  1758. tmpreg:=getintregister(list,loc.size);
  1759. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1760. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1761. end;
  1762. LOC_SUBSETREG, LOC_CSUBSETREG:
  1763. begin
  1764. tmpreg:=getintregister(list,loc.size);
  1765. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1766. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1767. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  1768. end;
  1769. LOC_SUBSETREF, LOC_CSUBSETREF:
  1770. begin
  1771. tmpreg:=getintregister(list,loc.size);
  1772. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  1773. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1774. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  1775. end;
  1776. else
  1777. internalerror(200109061);
  1778. end;
  1779. end;
  1780. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1781. a:aint;src,dst:Tregister);
  1782. begin
  1783. a_load_reg_reg(list,size,size,src,dst);
  1784. a_op_const_reg(list,op,size,a,dst);
  1785. end;
  1786. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1787. size: tcgsize; src1, src2, dst: tregister);
  1788. var
  1789. tmpreg: tregister;
  1790. begin
  1791. if (dst<>src1) then
  1792. begin
  1793. a_load_reg_reg(list,size,size,src2,dst);
  1794. a_op_reg_reg(list,op,size,src1,dst);
  1795. end
  1796. else
  1797. begin
  1798. tmpreg:=getintregister(list,size);
  1799. a_load_reg_reg(list,size,size,src2,tmpreg);
  1800. a_op_reg_reg(list,op,size,src1,tmpreg);
  1801. a_load_reg_reg(list,size,size,tmpreg,dst);
  1802. end;
  1803. end;
  1804. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1805. begin
  1806. a_op_const_reg_reg(list,op,size,a,src,dst);
  1807. ovloc.loc:=LOC_VOID;
  1808. end;
  1809. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1810. begin
  1811. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1812. ovloc.loc:=LOC_VOID;
  1813. end;
  1814. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1815. l : tasmlabel);
  1816. var
  1817. tmpreg: tregister;
  1818. begin
  1819. tmpreg:=getintregister(list,size);
  1820. a_load_ref_reg(list,size,size,ref,tmpreg);
  1821. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1822. end;
  1823. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const loc : tlocation;
  1824. l : tasmlabel);
  1825. var
  1826. tmpreg : tregister;
  1827. begin
  1828. case loc.loc of
  1829. LOC_REGISTER,LOC_CREGISTER:
  1830. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1831. LOC_REFERENCE,LOC_CREFERENCE:
  1832. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1833. LOC_SUBSETREG, LOC_CSUBSETREG:
  1834. begin
  1835. tmpreg:=getintregister(list,size);
  1836. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  1837. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1838. end;
  1839. LOC_SUBSETREF, LOC_CSUBSETREF:
  1840. begin
  1841. tmpreg:=getintregister(list,size);
  1842. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  1843. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1844. end;
  1845. else
  1846. internalerror(200109061);
  1847. end;
  1848. end;
  1849. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1850. var
  1851. tmpreg: tregister;
  1852. begin
  1853. tmpreg:=getintregister(list,size);
  1854. a_load_ref_reg(list,size,size,ref,tmpreg);
  1855. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1856. end;
  1857. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1858. var
  1859. tmpreg: tregister;
  1860. begin
  1861. tmpreg:=getintregister(list,size);
  1862. a_load_ref_reg(list,size,size,ref,tmpreg);
  1863. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1864. end;
  1865. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1866. begin
  1867. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1868. end;
  1869. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1870. begin
  1871. case loc.loc of
  1872. LOC_REGISTER,
  1873. LOC_CREGISTER:
  1874. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1875. LOC_REFERENCE,
  1876. LOC_CREFERENCE :
  1877. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1878. LOC_CONSTANT:
  1879. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1880. LOC_SUBSETREG,
  1881. LOC_CSUBSETREG:
  1882. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  1883. LOC_SUBSETREF,
  1884. LOC_CSUBSETREF:
  1885. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  1886. else
  1887. internalerror(200203231);
  1888. end;
  1889. end;
  1890. procedure tcg.a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize : tcgsize; cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel);
  1891. var
  1892. tmpreg: tregister;
  1893. begin
  1894. tmpreg:=getintregister(list, cmpsize);
  1895. a_load_subsetreg_reg(list,subsetsize,cmpsize,sreg,tmpreg);
  1896. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1897. end;
  1898. procedure tcg.a_cmp_subsetref_reg_label(list : TAsmList; subsetsize : tcgsize; cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel);
  1899. var
  1900. tmpreg: tregister;
  1901. begin
  1902. tmpreg:=getintregister(list, cmpsize);
  1903. a_load_subsetref_reg(list,subsetsize,cmpsize,sref,tmpreg);
  1904. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1905. end;
  1906. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1907. l : tasmlabel);
  1908. var
  1909. tmpreg: tregister;
  1910. begin
  1911. case loc.loc of
  1912. LOC_REGISTER,LOC_CREGISTER:
  1913. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1914. LOC_REFERENCE,LOC_CREFERENCE:
  1915. begin
  1916. tmpreg:=getintregister(list,size);
  1917. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1918. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1919. end;
  1920. LOC_SUBSETREG, LOC_CSUBSETREG:
  1921. begin
  1922. tmpreg:=getintregister(list, size);
  1923. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1924. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  1925. end;
  1926. LOC_SUBSETREF, LOC_CSUBSETREF:
  1927. begin
  1928. tmpreg:=getintregister(list, size);
  1929. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1930. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  1931. end;
  1932. else
  1933. internalerror(200109061);
  1934. end;
  1935. end;
  1936. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1937. begin
  1938. case loc.loc of
  1939. LOC_MMREGISTER,LOC_CMMREGISTER:
  1940. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1941. LOC_REFERENCE,LOC_CREFERENCE:
  1942. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1943. else
  1944. internalerror(200310121);
  1945. end;
  1946. end;
  1947. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1948. begin
  1949. case loc.loc of
  1950. LOC_MMREGISTER,LOC_CMMREGISTER:
  1951. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1952. LOC_REFERENCE,LOC_CREFERENCE:
  1953. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1954. else
  1955. internalerror(200310122);
  1956. end;
  1957. end;
  1958. procedure tcg.a_parammm_reg(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1959. var
  1960. href : treference;
  1961. begin
  1962. cgpara.check_simple_location;
  1963. case cgpara.location^.loc of
  1964. LOC_MMREGISTER,LOC_CMMREGISTER:
  1965. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1966. LOC_REFERENCE,LOC_CREFERENCE:
  1967. begin
  1968. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1969. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1970. end
  1971. else
  1972. internalerror(200310123);
  1973. end;
  1974. end;
  1975. procedure tcg.a_parammm_ref(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1976. var
  1977. hr : tregister;
  1978. hs : tmmshuffle;
  1979. begin
  1980. cgpara.check_simple_location;
  1981. hr:=getmmregister(list,cgpara.location^.size);
  1982. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1983. if realshuffle(shuffle) then
  1984. begin
  1985. hs:=shuffle^;
  1986. removeshuffles(hs);
  1987. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,@hs);
  1988. end
  1989. else
  1990. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,shuffle);
  1991. end;
  1992. procedure tcg.a_parammm_loc(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1993. begin
  1994. case loc.loc of
  1995. LOC_MMREGISTER,LOC_CMMREGISTER:
  1996. a_parammm_reg(list,loc.size,loc.register,cgpara,shuffle);
  1997. LOC_REFERENCE,LOC_CREFERENCE:
  1998. a_parammm_ref(list,loc.size,loc.reference,cgpara,shuffle);
  1999. else
  2000. internalerror(200310123);
  2001. end;
  2002. end;
  2003. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2004. var
  2005. hr : tregister;
  2006. hs : tmmshuffle;
  2007. begin
  2008. hr:=getmmregister(list,size);
  2009. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2010. if realshuffle(shuffle) then
  2011. begin
  2012. hs:=shuffle^;
  2013. removeshuffles(hs);
  2014. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2015. end
  2016. else
  2017. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2018. end;
  2019. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2020. var
  2021. hr : tregister;
  2022. hs : tmmshuffle;
  2023. begin
  2024. hr:=getmmregister(list,size);
  2025. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2026. if realshuffle(shuffle) then
  2027. begin
  2028. hs:=shuffle^;
  2029. removeshuffles(hs);
  2030. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2031. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2032. end
  2033. else
  2034. begin
  2035. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2036. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2037. end;
  2038. end;
  2039. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2040. begin
  2041. case loc.loc of
  2042. LOC_CMMREGISTER,LOC_MMREGISTER:
  2043. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2044. LOC_CREFERENCE,LOC_REFERENCE:
  2045. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2046. else
  2047. internalerror(200312232);
  2048. end;
  2049. end;
  2050. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);
  2051. begin
  2052. g_concatcopy(list,source,dest,len);
  2053. end;
  2054. procedure tcg.g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  2055. var
  2056. cgpara1,cgpara2,cgpara3 : TCGPara;
  2057. begin
  2058. cgpara1.init;
  2059. cgpara2.init;
  2060. cgpara3.init;
  2061. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2062. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2063. paramanager.getintparaloc(pocall_default,3,cgpara3);
  2064. paramanager.allocparaloc(list,cgpara3);
  2065. a_paramaddr_ref(list,dest,cgpara3);
  2066. paramanager.allocparaloc(list,cgpara2);
  2067. a_paramaddr_ref(list,source,cgpara2);
  2068. paramanager.allocparaloc(list,cgpara1);
  2069. a_param_const(list,OS_INT,len,cgpara1);
  2070. paramanager.freeparaloc(list,cgpara3);
  2071. paramanager.freeparaloc(list,cgpara2);
  2072. paramanager.freeparaloc(list,cgpara1);
  2073. allocallcpuregisters(list);
  2074. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  2075. deallocallcpuregisters(list);
  2076. cgpara3.done;
  2077. cgpara2.done;
  2078. cgpara1.done;
  2079. end;
  2080. procedure tcg.g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  2081. var
  2082. href : treference;
  2083. incrfunc : string;
  2084. cgpara1,cgpara2 : TCGPara;
  2085. begin
  2086. cgpara1.init;
  2087. cgpara2.init;
  2088. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2089. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2090. if is_interfacecom(t) then
  2091. incrfunc:='FPC_INTF_INCR_REF'
  2092. else if is_ansistring(t) then
  2093. incrfunc:='FPC_ANSISTR_INCR_REF'
  2094. else if is_widestring(t) then
  2095. incrfunc:='FPC_WIDESTR_INCR_REF'
  2096. else if is_dynamic_array(t) then
  2097. incrfunc:='FPC_DYNARRAY_INCR_REF'
  2098. else
  2099. incrfunc:='';
  2100. { call the special incr function or the generic addref }
  2101. if incrfunc<>'' then
  2102. begin
  2103. paramanager.allocparaloc(list,cgpara1);
  2104. { widestrings aren't ref. counted on all platforms so we need the address
  2105. to create a real copy }
  2106. if is_widestring(t) then
  2107. a_paramaddr_ref(list,ref,cgpara1)
  2108. else
  2109. { these functions get the pointer by value }
  2110. a_param_ref(list,OS_ADDR,ref,cgpara1);
  2111. paramanager.freeparaloc(list,cgpara1);
  2112. allocallcpuregisters(list);
  2113. a_call_name(list,incrfunc);
  2114. deallocallcpuregisters(list);
  2115. end
  2116. else
  2117. begin
  2118. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2119. paramanager.allocparaloc(list,cgpara2);
  2120. a_paramaddr_ref(list,href,cgpara2);
  2121. paramanager.allocparaloc(list,cgpara1);
  2122. a_paramaddr_ref(list,ref,cgpara1);
  2123. paramanager.freeparaloc(list,cgpara1);
  2124. paramanager.freeparaloc(list,cgpara2);
  2125. allocallcpuregisters(list);
  2126. a_call_name(list,'FPC_ADDREF');
  2127. deallocallcpuregisters(list);
  2128. end;
  2129. cgpara2.done;
  2130. cgpara1.done;
  2131. end;
  2132. procedure tcg.g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  2133. var
  2134. href : treference;
  2135. decrfunc : string;
  2136. needrtti : boolean;
  2137. cgpara1,cgpara2 : TCGPara;
  2138. tempreg1,tempreg2 : TRegister;
  2139. begin
  2140. cgpara1.init;
  2141. cgpara2.init;
  2142. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2143. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2144. needrtti:=false;
  2145. if is_interfacecom(t) then
  2146. decrfunc:='FPC_INTF_DECR_REF'
  2147. else if is_ansistring(t) then
  2148. decrfunc:='FPC_ANSISTR_DECR_REF'
  2149. else if is_widestring(t) then
  2150. decrfunc:='FPC_WIDESTR_DECR_REF'
  2151. else if is_dynamic_array(t) then
  2152. begin
  2153. decrfunc:='FPC_DYNARRAY_DECR_REF';
  2154. needrtti:=true;
  2155. end
  2156. else
  2157. decrfunc:='';
  2158. { call the special decr function or the generic decref }
  2159. if decrfunc<>'' then
  2160. begin
  2161. if needrtti then
  2162. begin
  2163. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2164. tempreg2:=getaddressregister(list);
  2165. a_loadaddr_ref_reg(list,href,tempreg2);
  2166. end;
  2167. tempreg1:=getaddressregister(list);
  2168. a_loadaddr_ref_reg(list,ref,tempreg1);
  2169. if needrtti then
  2170. begin
  2171. paramanager.allocparaloc(list,cgpara2);
  2172. a_param_reg(list,OS_ADDR,tempreg2,cgpara2);
  2173. paramanager.freeparaloc(list,cgpara2);
  2174. end;
  2175. paramanager.allocparaloc(list,cgpara1);
  2176. a_param_reg(list,OS_ADDR,tempreg1,cgpara1);
  2177. paramanager.freeparaloc(list,cgpara1);
  2178. allocallcpuregisters(list);
  2179. a_call_name(list,decrfunc);
  2180. deallocallcpuregisters(list);
  2181. end
  2182. else
  2183. begin
  2184. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2185. paramanager.allocparaloc(list,cgpara2);
  2186. a_paramaddr_ref(list,href,cgpara2);
  2187. paramanager.allocparaloc(list,cgpara1);
  2188. a_paramaddr_ref(list,ref,cgpara1);
  2189. paramanager.freeparaloc(list,cgpara1);
  2190. paramanager.freeparaloc(list,cgpara2);
  2191. allocallcpuregisters(list);
  2192. a_call_name(list,'FPC_DECREF');
  2193. deallocallcpuregisters(list);
  2194. end;
  2195. cgpara2.done;
  2196. cgpara1.done;
  2197. end;
  2198. procedure tcg.g_initialize(list : TAsmList;t : tdef;const ref : treference);
  2199. var
  2200. href : treference;
  2201. cgpara1,cgpara2 : TCGPara;
  2202. begin
  2203. cgpara1.init;
  2204. cgpara2.init;
  2205. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2206. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2207. if is_ansistring(t) or
  2208. is_widestring(t) or
  2209. is_interfacecom(t) or
  2210. is_dynamic_array(t) then
  2211. a_load_const_ref(list,OS_ADDR,0,ref)
  2212. else
  2213. begin
  2214. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2215. paramanager.allocparaloc(list,cgpara2);
  2216. a_paramaddr_ref(list,href,cgpara2);
  2217. paramanager.allocparaloc(list,cgpara1);
  2218. a_paramaddr_ref(list,ref,cgpara1);
  2219. paramanager.freeparaloc(list,cgpara1);
  2220. paramanager.freeparaloc(list,cgpara2);
  2221. allocallcpuregisters(list);
  2222. a_call_name(list,'FPC_INITIALIZE');
  2223. deallocallcpuregisters(list);
  2224. end;
  2225. cgpara1.done;
  2226. cgpara2.done;
  2227. end;
  2228. procedure tcg.g_finalize(list : TAsmList;t : tdef;const ref : treference);
  2229. var
  2230. href : treference;
  2231. cgpara1,cgpara2 : TCGPara;
  2232. begin
  2233. cgpara1.init;
  2234. cgpara2.init;
  2235. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2236. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2237. if is_ansistring(t) or
  2238. is_widestring(t) or
  2239. is_interfacecom(t) then
  2240. begin
  2241. g_decrrefcount(list,t,ref);
  2242. a_load_const_ref(list,OS_ADDR,0,ref);
  2243. end
  2244. else
  2245. begin
  2246. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2247. paramanager.allocparaloc(list,cgpara2);
  2248. a_paramaddr_ref(list,href,cgpara2);
  2249. paramanager.allocparaloc(list,cgpara1);
  2250. a_paramaddr_ref(list,ref,cgpara1);
  2251. paramanager.freeparaloc(list,cgpara1);
  2252. paramanager.freeparaloc(list,cgpara2);
  2253. allocallcpuregisters(list);
  2254. a_call_name(list,'FPC_FINALIZE');
  2255. deallocallcpuregisters(list);
  2256. end;
  2257. cgpara1.done;
  2258. cgpara2.done;
  2259. end;
  2260. procedure tcg.g_rangecheck(list: TAsmList; const l:tlocation;fromdef,todef: tdef);
  2261. { generate range checking code for the value at location p. The type }
  2262. { type used is checked against todefs ranges. fromdef (p.resultdef) }
  2263. { is the original type used at that location. When both defs are equal }
  2264. { the check is also insert (needed for succ,pref,inc,dec) }
  2265. const
  2266. aintmax=high(aint);
  2267. var
  2268. neglabel : tasmlabel;
  2269. hreg : tregister;
  2270. lto,hto,
  2271. lfrom,hfrom : TConstExprInt;
  2272. fromsize, tosize: cardinal;
  2273. from_signed, to_signed: boolean;
  2274. begin
  2275. { range checking on and range checkable value? }
  2276. if not(cs_check_range in aktlocalswitches) or
  2277. not(fromdef.deftype in [orddef,enumdef]) then
  2278. exit;
  2279. {$ifndef cpu64bit}
  2280. { handle 64bit rangechecks separate for 32bit processors }
  2281. if is_64bit(fromdef) or is_64bit(todef) then
  2282. begin
  2283. cg64.g_rangecheck64(list,l,fromdef,todef);
  2284. exit;
  2285. end;
  2286. {$endif cpu64bit}
  2287. { only check when assigning to scalar, subranges are different, }
  2288. { when todef=fromdef then the check is always generated }
  2289. getrange(fromdef,lfrom,hfrom);
  2290. getrange(todef,lto,hto);
  2291. from_signed := is_signed(fromdef);
  2292. to_signed := is_signed(todef);
  2293. { check the rangedef of the array, not the array itself }
  2294. { (only change now, since getrange needs the arraydef) }
  2295. if (todef.deftype = arraydef) then
  2296. todef := tarraydef(todef).rangedef;
  2297. { no range check if from and to are equal and are both longint/dword }
  2298. { no range check if from and to are equal and are both longint/dword }
  2299. { (if we have a 32bit processor) or int64/qword, since such }
  2300. { operations can at most cause overflows (JM) }
  2301. { Note that these checks are mostly processor independent, they only }
  2302. { have to be changed once we introduce 64bit subrange types }
  2303. {$ifdef cpu64bit}
  2304. if (fromdef = todef) and
  2305. (fromdef.deftype=orddef) and
  2306. (((((torddef(fromdef).typ = s64bit) and
  2307. (lfrom = low(int64)) and
  2308. (hfrom = high(int64))) or
  2309. ((torddef(fromdef).typ = u64bit) and
  2310. (lfrom = low(qword)) and
  2311. (hfrom = high(qword))) or
  2312. ((torddef(fromdef).typ = scurrency) and
  2313. (lfrom = low(int64)) and
  2314. (hfrom = high(int64)))))) then
  2315. exit;
  2316. {$else cpu64bit}
  2317. if (fromdef = todef) and
  2318. (fromdef.deftype=orddef) and
  2319. (((((torddef(fromdef).typ = s32bit) and
  2320. (lfrom = low(longint)) and
  2321. (hfrom = high(longint))) or
  2322. ((torddef(fromdef).typ = u32bit) and
  2323. (lfrom = low(cardinal)) and
  2324. (hfrom = high(cardinal)))))) then
  2325. exit;
  2326. {$endif cpu64bit}
  2327. { optimize some range checks away in safe cases }
  2328. fromsize := fromdef.size;
  2329. tosize := todef.size;
  2330. if ((from_signed = to_signed) or
  2331. (not from_signed)) and
  2332. (lto<=lfrom) and (hto>=hfrom) and
  2333. (fromsize <= tosize) then
  2334. begin
  2335. { if fromsize < tosize, and both have the same signed-ness or }
  2336. { fromdef is unsigned, then all bit patterns from fromdef are }
  2337. { valid for todef as well }
  2338. if (fromsize < tosize) then
  2339. exit;
  2340. if (fromsize = tosize) and
  2341. (from_signed = to_signed) then
  2342. { only optimize away if all bit patterns which fit in fromsize }
  2343. { are valid for the todef }
  2344. begin
  2345. {$ifopt Q+}
  2346. {$define overflowon}
  2347. {$Q-}
  2348. {$endif}
  2349. if to_signed then
  2350. begin
  2351. { calculation of the low/high ranges must not overflow 64 bit
  2352. otherwise we end up comparing with zero for 64 bit data types on
  2353. 64 bit processors }
  2354. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2355. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2356. exit
  2357. end
  2358. else
  2359. begin
  2360. { calculation of the low/high ranges must not overflow 64 bit
  2361. otherwise we end up having all zeros for 64 bit data types on
  2362. 64 bit processors }
  2363. if (lto = 0) and
  2364. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2365. exit
  2366. end;
  2367. {$ifdef overflowon}
  2368. {$Q+}
  2369. {$undef overflowon}
  2370. {$endif}
  2371. end
  2372. end;
  2373. { generate the rangecheck code for the def where we are going to }
  2374. { store the result }
  2375. { use the trick that }
  2376. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2377. { To be able to do that, we have to make sure however that either }
  2378. { fromdef and todef are both signed or unsigned, or that we leave }
  2379. { the parts < 0 and > maxlongint out }
  2380. if from_signed xor to_signed then
  2381. begin
  2382. if from_signed then
  2383. { from is signed, to is unsigned }
  2384. begin
  2385. { if high(from) < 0 -> always range error }
  2386. if (hfrom < 0) or
  2387. { if low(to) > maxlongint also range error }
  2388. (lto > aintmax) then
  2389. begin
  2390. a_call_name(list,'FPC_RANGEERROR');
  2391. exit
  2392. end;
  2393. { from is signed and to is unsigned -> when looking at to }
  2394. { as an signed value, it must be < maxaint (otherwise }
  2395. { it will become negative, which is invalid since "to" is unsigned) }
  2396. if hto > aintmax then
  2397. hto := aintmax;
  2398. end
  2399. else
  2400. { from is unsigned, to is signed }
  2401. begin
  2402. if (lfrom > aintmax) or
  2403. (hto < 0) then
  2404. begin
  2405. a_call_name(list,'FPC_RANGEERROR');
  2406. exit
  2407. end;
  2408. { from is unsigned and to is signed -> when looking at to }
  2409. { as an unsigned value, it must be >= 0 (since negative }
  2410. { values are the same as values > maxlongint) }
  2411. if lto < 0 then
  2412. lto := 0;
  2413. end;
  2414. end;
  2415. hreg:=getintregister(list,OS_INT);
  2416. a_load_loc_reg(list,OS_INT,l,hreg);
  2417. a_op_const_reg(list,OP_SUB,OS_INT,aint(lto),hreg);
  2418. current_asmdata.getjumplabel(neglabel);
  2419. {
  2420. if from_signed then
  2421. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2422. else
  2423. }
  2424. {$ifdef cpu64bit}
  2425. if qword(hto-lto)>qword(aintmax) then
  2426. a_cmp_const_reg_label(list,OS_INT,OC_BE,aintmax,hreg,neglabel)
  2427. else
  2428. {$endif cpu64bit}
  2429. a_cmp_const_reg_label(list,OS_INT,OC_BE,aint(hto-lto),hreg,neglabel);
  2430. a_call_name(list,'FPC_RANGEERROR');
  2431. a_label(list,neglabel);
  2432. end;
  2433. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2434. begin
  2435. g_overflowCheck(list,loc,def);
  2436. end;
  2437. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  2438. var
  2439. tmpreg : tregister;
  2440. begin
  2441. tmpreg:=getintregister(list,size);
  2442. g_flags2reg(list,size,f,tmpreg);
  2443. a_load_reg_ref(list,size,size,tmpreg,ref);
  2444. end;
  2445. procedure tcg.g_maybe_testself(list : TAsmList;reg:tregister);
  2446. var
  2447. OKLabel : tasmlabel;
  2448. cgpara1 : TCGPara;
  2449. begin
  2450. if (cs_check_object in aktlocalswitches) or
  2451. (cs_check_range in aktlocalswitches) then
  2452. begin
  2453. current_asmdata.getjumplabel(oklabel);
  2454. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  2455. cgpara1.init;
  2456. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2457. paramanager.allocparaloc(list,cgpara1);
  2458. a_param_const(list,OS_INT,210,cgpara1);
  2459. paramanager.freeparaloc(list,cgpara1);
  2460. a_call_name(list,'FPC_HANDLEERROR');
  2461. a_label(list,oklabel);
  2462. cgpara1.done;
  2463. end;
  2464. end;
  2465. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  2466. var
  2467. hrefvmt : treference;
  2468. cgpara1,cgpara2 : TCGPara;
  2469. begin
  2470. cgpara1.init;
  2471. cgpara2.init;
  2472. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2473. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2474. if (cs_check_object in aktlocalswitches) then
  2475. begin
  2476. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname),0);
  2477. paramanager.allocparaloc(list,cgpara2);
  2478. a_paramaddr_ref(list,hrefvmt,cgpara2);
  2479. paramanager.allocparaloc(list,cgpara1);
  2480. a_param_reg(list,OS_ADDR,reg,cgpara1);
  2481. paramanager.freeparaloc(list,cgpara1);
  2482. paramanager.freeparaloc(list,cgpara2);
  2483. allocallcpuregisters(list);
  2484. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  2485. deallocallcpuregisters(list);
  2486. end
  2487. else
  2488. if (cs_check_range in aktlocalswitches) then
  2489. begin
  2490. paramanager.allocparaloc(list,cgpara1);
  2491. a_param_reg(list,OS_ADDR,reg,cgpara1);
  2492. paramanager.freeparaloc(list,cgpara1);
  2493. allocallcpuregisters(list);
  2494. a_call_name(list,'FPC_CHECK_OBJECT');
  2495. deallocallcpuregisters(list);
  2496. end;
  2497. cgpara1.done;
  2498. cgpara2.done;
  2499. end;
  2500. {*****************************************************************************
  2501. Entry/Exit Code Functions
  2502. *****************************************************************************}
  2503. procedure tcg.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);
  2504. var
  2505. sizereg,sourcereg,lenreg : tregister;
  2506. cgpara1,cgpara2,cgpara3 : TCGPara;
  2507. begin
  2508. { because some abis don't support dynamic stack allocation properly
  2509. open array value parameters are copied onto the heap
  2510. }
  2511. { calculate necessary memory }
  2512. { read/write operations on one register make the life of the register allocator hard }
  2513. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  2514. begin
  2515. lenreg:=getintregister(list,OS_INT);
  2516. a_load_loc_reg(list,OS_INT,lenloc,lenreg);
  2517. end
  2518. else
  2519. lenreg:=lenloc.register;
  2520. sizereg:=getintregister(list,OS_INT);
  2521. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,lenreg,sizereg);
  2522. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  2523. { load source }
  2524. sourcereg:=getaddressregister(list);
  2525. a_loadaddr_ref_reg(list,ref,sourcereg);
  2526. { do getmem call }
  2527. cgpara1.init;
  2528. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2529. paramanager.allocparaloc(list,cgpara1);
  2530. a_param_reg(list,OS_INT,sizereg,cgpara1);
  2531. paramanager.freeparaloc(list,cgpara1);
  2532. allocallcpuregisters(list);
  2533. a_call_name(list,'FPC_GETMEM');
  2534. deallocallcpuregisters(list);
  2535. cgpara1.done;
  2536. { return the new address }
  2537. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  2538. { do move call }
  2539. cgpara1.init;
  2540. cgpara2.init;
  2541. cgpara3.init;
  2542. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2543. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2544. paramanager.getintparaloc(pocall_default,3,cgpara3);
  2545. { load size }
  2546. paramanager.allocparaloc(list,cgpara3);
  2547. a_param_reg(list,OS_INT,sizereg,cgpara3);
  2548. { load destination }
  2549. paramanager.allocparaloc(list,cgpara2);
  2550. a_param_reg(list,OS_ADDR,destreg,cgpara2);
  2551. { load source }
  2552. paramanager.allocparaloc(list,cgpara1);
  2553. a_param_reg(list,OS_ADDR,sourcereg,cgpara1);
  2554. paramanager.freeparaloc(list,cgpara3);
  2555. paramanager.freeparaloc(list,cgpara2);
  2556. paramanager.freeparaloc(list,cgpara1);
  2557. allocallcpuregisters(list);
  2558. a_call_name(list,'FPC_MOVE');
  2559. deallocallcpuregisters(list);
  2560. cgpara3.done;
  2561. cgpara2.done;
  2562. cgpara1.done;
  2563. end;
  2564. procedure tcg.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  2565. var
  2566. cgpara1 : TCGPara;
  2567. begin
  2568. { do move call }
  2569. cgpara1.init;
  2570. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2571. { load source }
  2572. paramanager.allocparaloc(list,cgpara1);
  2573. a_param_loc(list,l,cgpara1);
  2574. paramanager.freeparaloc(list,cgpara1);
  2575. allocallcpuregisters(list);
  2576. a_call_name(list,'FPC_FREEMEM');
  2577. deallocallcpuregisters(list);
  2578. cgpara1.done;
  2579. end;
  2580. procedure tcg.g_save_standard_registers(list:TAsmList);
  2581. var
  2582. href : treference;
  2583. size : longint;
  2584. r : integer;
  2585. begin
  2586. { Get temp }
  2587. size:=0;
  2588. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2589. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2590. inc(size,sizeof(aint));
  2591. if size>0 then
  2592. begin
  2593. tg.GetTemp(list,size,tt_noreuse,current_procinfo.save_regs_ref);
  2594. { Copy registers to temp }
  2595. href:=current_procinfo.save_regs_ref;
  2596. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2597. begin
  2598. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2599. begin
  2600. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  2601. inc(href.offset,sizeof(aint));
  2602. end;
  2603. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  2604. end;
  2605. end;
  2606. end;
  2607. procedure tcg.g_restore_standard_registers(list:TAsmList);
  2608. var
  2609. href : treference;
  2610. r : integer;
  2611. hreg : tregister;
  2612. begin
  2613. { Copy registers from temp }
  2614. href:=current_procinfo.save_regs_ref;
  2615. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2616. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2617. begin
  2618. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2619. { Allocate register so the optimizer does not remove the load }
  2620. a_reg_alloc(list,hreg);
  2621. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2622. inc(href.offset,sizeof(aint));
  2623. end;
  2624. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2625. end;
  2626. procedure tcg.g_profilecode(list : TAsmList);
  2627. begin
  2628. end;
  2629. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  2630. begin
  2631. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  2632. end;
  2633. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: aint);
  2634. begin
  2635. a_load_const_ref(list, OS_INT, a, href);
  2636. end;
  2637. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  2638. begin
  2639. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  2640. end;
  2641. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);
  2642. var
  2643. hsym : tsym;
  2644. href : treference;
  2645. paraloc : tcgparalocation;
  2646. begin
  2647. { calculate the parameter info for the procdef }
  2648. if not procdef.has_paraloc_info then
  2649. begin
  2650. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  2651. procdef.has_paraloc_info:=true;
  2652. end;
  2653. hsym:=tsym(procdef.parast.search('self'));
  2654. if not(assigned(hsym) and
  2655. (hsym.typ=paravarsym)) then
  2656. internalerror(200305251);
  2657. paraloc:=tparavarsym(hsym).paraloc[callerside].location^;
  2658. case paraloc.loc of
  2659. LOC_REGISTER:
  2660. cg.a_op_const_reg(list,OP_SUB,paraloc.size,ioffset,paraloc.register);
  2661. LOC_REFERENCE:
  2662. begin
  2663. { offset in the wrapper needs to be adjusted for the stored
  2664. return address }
  2665. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset+sizeof(aint));
  2666. cg.a_op_const_ref(list,OP_SUB,paraloc.size,ioffset,href);
  2667. end
  2668. else
  2669. internalerror(200309189);
  2670. end;
  2671. end;
  2672. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2673. begin
  2674. a_call_name(list,s);
  2675. end;
  2676. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string): tregister;
  2677. var
  2678. l: tasmsymbol;
  2679. ref: treference;
  2680. begin
  2681. result := NR_NO;
  2682. case target_info.system of
  2683. system_powerpc_darwin,
  2684. system_i386_darwin:
  2685. begin
  2686. l:=current_asmdata.getasmsymbol('L'+symname+'$non_lazy_ptr');
  2687. if not(assigned(l)) then
  2688. begin
  2689. l:=current_asmdata.DefineAsmSymbol('L'+symname+'$non_lazy_ptr',AB_COMMON,AT_DATA);
  2690. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2691. current_asmdata.asmlists[al_picdata].concat(tai_const.create_indirect_sym(current_asmdata.RefAsmSymbol(symname)));
  2692. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2693. end;
  2694. result := cg.getaddressregister(list);
  2695. reference_reset_symbol(ref,l,0);
  2696. { ref.base:=current_procinfo.got;
  2697. ref.relsymbol:=current_procinfo.CurrGOTLabel;}
  2698. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2699. end;
  2700. end;
  2701. end;
  2702. {*****************************************************************************
  2703. TCG64
  2704. *****************************************************************************}
  2705. {$ifndef cpu64bit}
  2706. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2707. begin
  2708. a_load64_reg_reg(list,regsrc,regdst);
  2709. a_op64_const_reg(list,op,size,value,regdst);
  2710. end;
  2711. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2712. var
  2713. tmpreg64 : tregister64;
  2714. begin
  2715. { when src1=dst then we need to first create a temp to prevent
  2716. overwriting src1 with src2 }
  2717. if (regsrc1.reghi=regdst.reghi) or
  2718. (regsrc1.reglo=regdst.reghi) or
  2719. (regsrc1.reghi=regdst.reglo) or
  2720. (regsrc1.reglo=regdst.reglo) then
  2721. begin
  2722. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2723. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2724. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2725. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2726. a_load64_reg_reg(list,tmpreg64,regdst);
  2727. end
  2728. else
  2729. begin
  2730. a_load64_reg_reg(list,regsrc2,regdst);
  2731. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2732. end;
  2733. end;
  2734. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2735. var
  2736. tmpreg64 : tregister64;
  2737. begin
  2738. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2739. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2740. a_load64_subsetref_reg(list,sref,tmpreg64);
  2741. a_op64_const_reg(list,op,size,a,tmpreg64);
  2742. a_load64_reg_subsetref(list,tmpreg64,sref);
  2743. end;
  2744. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2745. var
  2746. tmpreg64 : tregister64;
  2747. begin
  2748. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2749. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2750. a_load64_subsetref_reg(list,sref,tmpreg64);
  2751. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2752. a_load64_reg_subsetref(list,tmpreg64,sref);
  2753. end;
  2754. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2755. var
  2756. tmpreg64 : tregister64;
  2757. begin
  2758. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2759. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2760. a_load64_subsetref_reg(list,sref,tmpreg64);
  2761. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2762. a_load64_reg_subsetref(list,tmpreg64,sref);
  2763. end;
  2764. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2765. var
  2766. tmpreg64 : tregister64;
  2767. begin
  2768. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2769. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2770. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2771. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2772. end;
  2773. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2774. begin
  2775. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2776. ovloc.loc:=LOC_VOID;
  2777. end;
  2778. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2779. begin
  2780. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2781. ovloc.loc:=LOC_VOID;
  2782. end;
  2783. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2784. begin
  2785. case l.loc of
  2786. LOC_REFERENCE, LOC_CREFERENCE:
  2787. a_load64_ref_subsetref(list,l.reference,sref);
  2788. LOC_REGISTER,LOC_CREGISTER:
  2789. a_load64_reg_subsetref(list,l.register64,sref);
  2790. LOC_CONSTANT :
  2791. a_load64_const_subsetref(list,l.value64,sref);
  2792. LOC_SUBSETREF,LOC_CSUBSETREF:
  2793. a_load64_subsetref_subsetref(list,l.sref,sref);
  2794. else
  2795. internalerror(2006082210);
  2796. end;
  2797. end;
  2798. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2799. begin
  2800. case l.loc of
  2801. LOC_REFERENCE, LOC_CREFERENCE:
  2802. a_load64_subsetref_ref(list,sref,l.reference);
  2803. LOC_REGISTER,LOC_CREGISTER:
  2804. a_load64_subsetref_reg(list,sref,l.register64);
  2805. LOC_SUBSETREF,LOC_CSUBSETREF:
  2806. a_load64_subsetref_subsetref(list,sref,l.sref);
  2807. else
  2808. internalerror(2006082211);
  2809. end;
  2810. end;
  2811. {$endif cpu64bit}
  2812. initialization
  2813. ;
  2814. finalization
  2815. cg.free;
  2816. {$ifndef cpu64bit}
  2817. cg64.free;
  2818. {$endif cpu64bit}
  2819. end.