cgobj.pas 140 KB

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