cgobj.pas 170 KB

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