2
0

cgobj.pas 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449
  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. 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. {# @abstract(Abstract code generator)
  38. This class implements an abstract instruction generator. Some of
  39. the methods of this class are generic, while others must
  40. be overridden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. should be @link(tcg64f32) and not @var(tcg).
  43. }
  44. { tcg }
  45. tcg = class
  46. { how many times is this current code executed }
  47. executionweight : longint;
  48. alignment : talignment;
  49. rg : array[tregistertype] of trgobj;
  50. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  51. has_next_reg: bitpacked array[TSuperRegister] of boolean;
  52. {$endif cpu8bitalu or cpu16bitalu}
  53. {$ifdef flowgraph}
  54. aktflownode:word;
  55. {$endif}
  56. {************************************************}
  57. { basic routines }
  58. constructor create;
  59. {# Initialize the register allocators needed for the codegenerator.}
  60. procedure init_register_allocators;virtual;
  61. {# Clean up the register allocators needed for the codegenerator.}
  62. procedure done_register_allocators;virtual;
  63. {# 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. }
  64. procedure set_regalloc_live_range_direction(dir: TRADirection);
  65. {$ifdef flowgraph}
  66. procedure init_flowgraph;
  67. procedure done_flowgraph;
  68. {$endif}
  69. {# Gets a register suitable to do integer operations on.}
  70. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  71. {# Gets a register suitable to do integer operations on.}
  72. function getaddressregister(list:TAsmList):Tregister;virtual;
  73. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  74. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  75. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  76. function gettempregister(list:TAsmList):Tregister;virtual;
  77. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  78. the cpu specific child cg object have such a method?}
  79. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  80. {# returns the next virtual register }
  81. function GetNextReg(const r: TRegister): TRegister;virtual;
  82. {$endif cpu8bitalu or cpu16bitalu}
  83. {$ifdef cpu8bitalu}
  84. {# returns the register with the offset of ofs of a continuous set of register starting with r }
  85. function GetOffsetReg(const r : TRegister;ofs : shortint) : TRegister;virtual;abstract;
  86. {# returns the register with the offset of ofs of a continuous set of register starting with r and being continued with rhi }
  87. function GetOffsetReg64(const r,rhi: TRegister;ofs : shortint): TRegister;virtual;abstract;
  88. {$endif cpu8bitalu}
  89. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  90. procedure add_move_instruction(instr:Taicpu);virtual;
  91. function uses_registers(rt:Tregistertype):boolean;virtual;
  92. {# Get a specific register.}
  93. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  94. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  95. {# Get multiple registers specified.}
  96. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  97. {# Free multiple registers specified.}
  98. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  99. procedure allocallcpuregisters(list:TAsmList);virtual;
  100. procedure deallocallcpuregisters(list:TAsmList);virtual;
  101. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  102. procedure translate_register(var reg : tregister);
  103. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister; virtual;
  104. {# Emit a label to the instruction stream. }
  105. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  106. {# Allocates register r by inserting a pai_realloc record }
  107. procedure a_reg_alloc(list : TAsmList;r : tregister);
  108. {# Deallocates register r by inserting a pa_regdealloc record}
  109. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  110. { Synchronize register, make sure it is still valid }
  111. procedure a_reg_sync(list : TAsmList;r : tregister);
  112. {# Pass a parameter, which is located in a register, to a routine.
  113. This routine should push/send the parameter to the routine, as
  114. required by the specific processor ABI and routine modifiers.
  115. It must generate register allocation information for the cgpara in
  116. case it consists of cpuregisters.
  117. @param(size size of the operand in the register)
  118. @param(r register source of the operand)
  119. @param(cgpara where the parameter will be stored)
  120. }
  121. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  122. {# Pass a parameter, which is a constant, to a routine.
  123. A generic version is provided. This routine should
  124. be overridden for optimization purposes if the cpu
  125. permits directly sending this type of parameter.
  126. It must generate register allocation information for the cgpara in
  127. case it consists of cpuregisters.
  128. @param(size size of the operand in constant)
  129. @param(a value of constant to send)
  130. @param(cgpara where the parameter will be stored)
  131. }
  132. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);virtual;
  133. {# Pass the value of a parameter, which is located in memory, to a routine.
  134. A generic version is provided. This routine should
  135. be overridden for optimization purposes if the cpu
  136. permits directly sending this type of parameter.
  137. It must generate register allocation information for the cgpara in
  138. case it consists of cpuregisters.
  139. @param(size size of the operand in constant)
  140. @param(r Memory reference of value to send)
  141. @param(cgpara where the parameter will be stored)
  142. }
  143. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  144. protected
  145. procedure a_load_ref_cgparalocref(list: TAsmList; sourcesize: tcgsize; sizeleft: tcgint; const ref, paralocref: treference; const cgpara: tcgpara; const location: PCGParaLocation); virtual;
  146. public
  147. {# Pass the value of a parameter, which can be located either in a register or memory location,
  148. to a routine.
  149. A generic version is provided.
  150. @param(l location of the operand to send)
  151. @param(nr parameter number (starting from one) of routine (from left to right))
  152. @param(cgpara where the parameter will be stored)
  153. }
  154. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  155. {# Pass the address of a reference to a routine. This routine
  156. will calculate the address of the reference, and pass this
  157. calculated address as a parameter.
  158. It must generate register allocation information for the cgpara in
  159. case it consists of cpuregisters.
  160. A generic version is provided. This routine should
  161. be overridden for optimization purposes if the cpu
  162. permits directly sending this type of parameter.
  163. @param(r reference to get address from)
  164. @param(nr parameter number (starting from one) of routine (from left to right))
  165. }
  166. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  167. {# Load a cgparaloc into a memory reference.
  168. It must generate register allocation information for the cgpara in
  169. case it consists of cpuregisters.
  170. @param(paraloc the source parameter sublocation)
  171. @param(ref the destination reference)
  172. @param(sizeleft indicates the total number of bytes left in all of
  173. the remaining sublocations of this parameter (the current
  174. sublocation and all of the sublocations coming after it).
  175. In case this location is also a reference, it is assumed
  176. to be the final part sublocation of the parameter and that it
  177. contains all of the "sizeleft" bytes).)
  178. @param(align the alignment of the paraloc in case it's a reference)
  179. }
  180. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  181. {# Load a cgparaloc into any kind of register (int, fp, mm).
  182. @param(regsize the size of the destination register)
  183. @param(paraloc the source parameter sublocation)
  184. @param(reg the destination register)
  185. @param(align the alignment of the paraloc in case it's a reference)
  186. }
  187. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  188. { Remarks:
  189. * If a method specifies a size you have only to take care
  190. of that number of bits, i.e. load_const_reg with OP_8 must
  191. only load the lower 8 bit of the specified register
  192. the rest of the register can be undefined
  193. if necessary the compiler will call a method
  194. to zero or sign extend the register
  195. * The a_load_XX_XX with OP_64 needn't to be
  196. implemented for 32 bit
  197. processors, the code generator takes care of that
  198. * the addr size is for work with the natural pointer
  199. size
  200. * the procedures without fpu/mm are only for integer usage
  201. * normally the first location is the source and the
  202. second the destination
  203. }
  204. {# Emits instruction to call the method specified by symbol name.
  205. This routine must be overridden for each new target cpu.
  206. }
  207. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  208. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  209. { same as a_call_name, might be overridden on certain architectures to emit
  210. static calls without usage of a got trampoline }
  211. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  212. { move instructions }
  213. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  214. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  215. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  216. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  217. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  218. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  219. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  220. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  221. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  222. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  223. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  224. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  225. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  226. { bit scan instructions }
  227. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister); virtual;
  228. { Multiplication with doubling result size.
  229. dstlo or dsthi may be NR_NO, in which case corresponding half of result is discarded. }
  230. procedure a_mul_reg_reg_pair(list: TAsmList; size: tcgsize; src1,src2,dstlo,dsthi: TRegister);virtual;
  231. { fpu move instructions }
  232. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  233. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  234. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  235. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  236. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  237. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  238. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  239. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  240. procedure a_loadfpu_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, fpureg: tregister); virtual;
  241. { vector register move instructions }
  242. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  243. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  244. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  245. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  246. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  247. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  248. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  249. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  250. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  251. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  252. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  253. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  254. procedure a_opmm_loc_reg_reg(list: TAsmList;Op : TOpCG;size : tcgsize;const loc : tlocation;src,dst : tregister;shuffle : pmmshuffle); virtual;
  255. procedure a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle); virtual;
  256. procedure a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle); virtual;
  257. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  258. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  259. { basic arithmetic operations }
  260. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  261. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  262. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  263. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  264. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  265. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  266. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  267. procedure a_op_loc_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const loc: tlocation; reg: tregister);
  268. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  269. { trinary operations for processors that support them, 'emulated' }
  270. { on others. None with "ref" arguments since I don't think there }
  271. { are any processors that support it (JM) }
  272. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  273. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  274. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  275. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  276. { unary operations (not, neg) }
  277. procedure a_op_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister); virtual;
  278. procedure a_op_ref(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference); virtual;
  279. procedure a_op_loc(list : TAsmList; Op: TOpCG; const loc: tlocation);
  280. { comparison operations }
  281. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  282. l : tasmlabel); virtual;
  283. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  284. l : tasmlabel); virtual;
  285. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  286. l : tasmlabel);
  287. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  288. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  289. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  290. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  291. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  292. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  293. l : tasmlabel);
  294. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  295. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  296. {$ifdef cpuflags}
  297. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  298. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  299. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  300. }
  301. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  302. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  303. {$endif cpuflags}
  304. {
  305. This routine tries to optimize the op_const_reg/ref opcode, and should be
  306. called at the start of a_op_const_reg/ref. It returns the actual opcode
  307. to emit, and the constant value to emit. This function can opcode OP_NONE to
  308. remove the opcode and OP_MOVE to replace it with a simple load
  309. @param(size Size of the operand in constant)
  310. @param(op The opcode to emit, returns the opcode which must be emitted)
  311. @param(a The constant which should be emitted, returns the constant which must
  312. be emitted)
  313. }
  314. procedure optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);virtual;
  315. {# This should emit the opcode to copy len bytes from the source
  316. to destination.
  317. It must be overridden for each new target processor.
  318. @param(source Source reference of copy)
  319. @param(dest Destination reference of copy)
  320. }
  321. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  322. {# This should emit the opcode to copy len bytes from the an unaligned source
  323. to destination.
  324. It must be overridden for each new target processor.
  325. @param(source Source reference of copy)
  326. @param(dest Destination reference of copy)
  327. }
  328. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  329. {# Generates overflow checking code for a node }
  330. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  331. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  332. {# Emits instructions when compilation is done in profile
  333. mode (this is set as a command line option). The default
  334. behavior does nothing, should be overridden as required.
  335. }
  336. procedure g_profilecode(list : TAsmList);virtual;
  337. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  338. @param(size Number of bytes to allocate)
  339. }
  340. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual;
  341. {# Emits instruction for allocating the locals in entry
  342. code of a routine. This is one of the first
  343. routine called in @var(genentrycode).
  344. @param(localsize Number of bytes to allocate as locals)
  345. }
  346. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  347. {# Emits instructions for returning from a subroutine.
  348. Should also restore the framepointer and stack.
  349. @param(parasize Number of bytes of parameters to deallocate from stack)
  350. }
  351. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  352. {# This routine is called when generating the code for the entry point
  353. of a routine. It should save all registers which are not used in this
  354. routine, and which should be declared as saved in the std_saved_registers
  355. set.
  356. This routine is mainly used when linking to code which is generated
  357. by ABI-compliant compilers (like GCC), to make sure that the reserved
  358. registers of that ABI are not clobbered.
  359. @param(usedinproc Registers which are used in the code of this routine)
  360. }
  361. procedure g_save_registers(list:TAsmList);virtual;
  362. {# This routine is called when generating the code for the exit point
  363. of a routine. It should restore all registers which were previously
  364. saved in @var(g_save_standard_registers).
  365. @param(usedinproc Registers which are used in the code of this routine)
  366. }
  367. procedure g_restore_registers(list:TAsmList);virtual;
  368. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  369. { initialize the pic/got register }
  370. procedure g_maybe_got_init(list: TAsmList); virtual;
  371. { allocallcpuregisters, a_call_name, deallocallcpuregisters sequence }
  372. procedure g_call(list: TAsmList; const s: string);
  373. { Generate code to exit an unwind-protected region. The default implementation
  374. produces a simple jump to destination label. }
  375. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;
  376. { Generate code for integer division by constant,
  377. generic version is suitable for 3-address CPUs }
  378. procedure g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister); virtual;
  379. { some CPUs do not support hardware fpu exceptions, this procedure is called after instructions which
  380. might set FPU exception related flags, so it has to check these flags if needed and throw an exeception }
  381. procedure g_check_for_fpu_exception(list : TAsmList; force,clear : boolean); virtual;
  382. procedure maybe_check_for_fpu_exception(list: TAsmList);
  383. { some CPUs do not support hardware fpu exceptions, this procedure is called after instructions which
  384. might set FPU exception related flags, so it has to check these flags if needed and throw an exeception }
  385. procedure g_check_for_fpu_exception(list: TAsmList); virtual;
  386. protected
  387. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;virtual;
  388. end;
  389. {$ifdef cpu64bitalu}
  390. { This class implements an abstract code generator class
  391. for 128 Bit operations, it applies currently only to 64 Bit CPUs and supports only simple operations
  392. }
  393. tcg128 = class
  394. procedure a_load128_reg_reg(list : TAsmList;regsrc,regdst : tregister128);virtual;
  395. procedure a_load128_reg_ref(list : TAsmList;reg : tregister128;const ref : treference);virtual;
  396. procedure a_load128_ref_reg(list : TAsmList;const ref : treference;reg : tregister128);virtual;
  397. procedure a_load128_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;
  398. procedure a_load128_reg_loc(list : TAsmList;reg : tregister128;const l : tlocation);virtual;
  399. procedure a_load128_const_reg(list : TAsmList;valuelo,valuehi : int64;reg : tregister128);virtual;
  400. procedure a_load128_loc_cgpara(list : TAsmList;const l : tlocation;const paraloc : TCGPara);virtual;
  401. procedure a_load128_ref_cgpara(list: TAsmList; const r: treference;const paraloc: tcgpara);
  402. procedure a_load128_reg_cgpara(list: TAsmList; reg: tregister128;const paraloc: tcgpara);
  403. end;
  404. { Creates a tregister128 record from 2 64 Bit registers. }
  405. function joinreg128(reglo,reghi : tregister) : tregister128;
  406. {$else cpu64bitalu}
  407. {# @abstract(Abstract code generator for 64 Bit operations)
  408. This class implements an abstract code generator class
  409. for 64 Bit operations.
  410. }
  411. tcg64 = class
  412. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  413. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  414. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  415. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  416. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  417. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  418. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  419. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  420. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  421. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  422. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  423. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  424. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  425. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  426. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  427. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  428. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  429. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  430. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  431. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  432. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  433. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  434. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  435. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  436. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  437. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  438. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  439. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  440. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  441. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  442. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  443. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  444. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  445. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  446. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  447. procedure a_op64_reg(list : TAsmList;op:TOpCG;size : tcgsize;regdst : tregister64);virtual;
  448. procedure a_op64_ref(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference);virtual;
  449. procedure a_op64_loc(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation);virtual;
  450. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  451. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  452. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  453. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  454. procedure a_load64_reg_cgpara(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  455. procedure a_load64_const_cgpara(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  456. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  457. procedure a_load64_loc_cgpara(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  458. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister); virtual;abstract;
  459. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64); virtual;abstract;
  460. {
  461. This routine tries to optimize the const_reg opcode, and should be
  462. called at the start of a_op64_const_reg. It returns the actual opcode
  463. to emit, and the constant value to emit. If this routine returns
  464. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  465. @param(op The opcode to emit, returns the opcode which must be emitted)
  466. @param(a The constant which should be emitted, returns the constant which must
  467. be emitted)
  468. @param(reg The register to emit the opcode with, returns the register with
  469. which the opcode will be emitted)
  470. }
  471. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  472. { override to catch 64bit rangechecks }
  473. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  474. end;
  475. { Creates a tregister64 record from 2 32 Bit registers. }
  476. function joinreg64(reglo,reghi : tregister) : tregister64;
  477. {$endif cpu64bitalu}
  478. var
  479. { Main code generator class }
  480. cg : tcg;
  481. {$ifdef cpu64bitalu}
  482. { Code generator class for all operations working with 128-Bit operands }
  483. cg128 : tcg128;
  484. {$else cpu64bitalu}
  485. { Code generator class for all operations working with 64-Bit operands }
  486. cg64 : tcg64;
  487. {$endif cpu64bitalu}
  488. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  489. procedure destroy_codegen;
  490. implementation
  491. uses
  492. globals,systems,fmodule,
  493. verbose,paramgr,symsym,symtable,
  494. tgobj,cutils,procinfo,
  495. cpuinfo;
  496. {*****************************************************************************
  497. basic functionallity
  498. ******************************************************************************}
  499. constructor tcg.create;
  500. begin
  501. end;
  502. {*****************************************************************************
  503. register allocation
  504. ******************************************************************************}
  505. procedure tcg.init_register_allocators;
  506. begin
  507. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  508. fillchar(has_next_reg,sizeof(has_next_reg),0);
  509. {$endif cpu8bitalu or cpu16bitalu}
  510. fillchar(rg,sizeof(rg),0);
  511. add_reg_instruction_hook:=@add_reg_instruction;
  512. executionweight:=100;
  513. end;
  514. procedure tcg.done_register_allocators;
  515. begin
  516. { Safety }
  517. fillchar(rg,sizeof(rg),0);
  518. add_reg_instruction_hook:=nil;
  519. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  520. fillchar(has_next_reg,sizeof(has_next_reg),0);
  521. {$endif cpu8bitalu or cpu16bitalu}
  522. end;
  523. {$ifdef flowgraph}
  524. procedure Tcg.init_flowgraph;
  525. begin
  526. aktflownode:=0;
  527. end;
  528. procedure Tcg.done_flowgraph;
  529. begin
  530. end;
  531. {$endif}
  532. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  533. {$ifdef cpu8bitalu}
  534. var
  535. tmp1,tmp2,tmp3 : TRegister;
  536. {$endif cpu8bitalu}
  537. begin
  538. if not assigned(rg[R_INTREGISTER]) then
  539. internalerror(200312122);
  540. {$if defined(cpu8bitalu)}
  541. case size of
  542. OS_8,OS_S8:
  543. Result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  544. OS_16,OS_S16:
  545. begin
  546. Result:=getintregister(list, OS_8);
  547. has_next_reg[getsupreg(Result)]:=true;
  548. { ensure that the high register can be retrieved by
  549. GetNextReg
  550. }
  551. if getintregister(list, OS_8)<>GetNextReg(Result) then
  552. internalerror(2011021331);
  553. end;
  554. OS_32,OS_S32:
  555. begin
  556. Result:=getintregister(list, OS_8);
  557. has_next_reg[getsupreg(Result)]:=true;
  558. tmp1:=getintregister(list, OS_8);
  559. has_next_reg[getsupreg(tmp1)]:=true;
  560. { ensure that the high register can be retrieved by
  561. GetNextReg
  562. }
  563. if tmp1<>GetNextReg(Result) then
  564. internalerror(2011021332);
  565. tmp2:=getintregister(list, OS_8);
  566. has_next_reg[getsupreg(tmp2)]:=true;
  567. { ensure that the upper register can be retrieved by
  568. GetNextReg
  569. }
  570. if tmp2<>GetNextReg(tmp1) then
  571. internalerror(2011021333);
  572. tmp3:=getintregister(list, OS_8);
  573. { ensure that the upper register can be retrieved by
  574. GetNextReg
  575. }
  576. if tmp3<>GetNextReg(tmp2) then
  577. internalerror(2011021334);
  578. end;
  579. else
  580. internalerror(2011021330);
  581. end;
  582. {$elseif defined(cpu16bitalu)}
  583. case size of
  584. OS_8, OS_S8,
  585. OS_16, OS_S16:
  586. Result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  587. OS_32, OS_S32:
  588. begin
  589. Result:=getintregister(list, OS_16);
  590. has_next_reg[getsupreg(Result)]:=true;
  591. { ensure that the high register can be retrieved by
  592. GetNextReg
  593. }
  594. if getintregister(list, OS_16)<>GetNextReg(Result) then
  595. internalerror(2013030202);
  596. end;
  597. else
  598. internalerror(2013030201);
  599. end;
  600. {$elseif defined(cpu32bitalu) or defined(cpu64bitalu)}
  601. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  602. {$endif}
  603. end;
  604. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  605. begin
  606. if not assigned(rg[R_FPUREGISTER]) then
  607. internalerror(200312123);
  608. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(R_FPUREGISTER,size));
  609. end;
  610. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  611. begin
  612. if not assigned(rg[R_MMREGISTER]) then
  613. internalerror(2003121214);
  614. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(R_MMREGISTER,size));
  615. end;
  616. function tcg.getaddressregister(list:TAsmList):Tregister;
  617. begin
  618. if assigned(rg[R_ADDRESSREGISTER]) then
  619. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  620. else
  621. begin
  622. if not assigned(rg[R_INTREGISTER]) then
  623. internalerror(200312121);
  624. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  625. end;
  626. end;
  627. function tcg.gettempregister(list: TAsmList): Tregister;
  628. begin
  629. result:=rg[R_TEMPREGISTER].getregister(list,R_SUBWHOLE);
  630. end;
  631. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  632. function tcg.GetNextReg(const r: TRegister): TRegister;
  633. begin
  634. {$ifndef AVR}
  635. { the AVR code generator depends on the fact that it can do GetNextReg also on physical registers }
  636. if getsupreg(r)<first_int_imreg then
  637. internalerror(2013051401);
  638. if not has_next_reg[getsupreg(r)] then
  639. internalerror(2017091103);
  640. {$else AVR}
  641. if (getsupreg(r)>=first_int_imreg) and not(has_next_reg[getsupreg(r)]) then
  642. internalerror(2017091103);
  643. {$endif AVR}
  644. if getregtype(r)<>R_INTREGISTER then
  645. internalerror(2017091101);
  646. if getsubreg(r)<>R_SUBWHOLE then
  647. internalerror(2017091102);
  648. result:=TRegister(longint(r)+1);
  649. end;
  650. {$endif cpu8bitalu or cpu16bitalu}
  651. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  652. var
  653. subreg:Tsubregister;
  654. begin
  655. subreg:=cgsize2subreg(getregtype(reg),size);
  656. result:=reg;
  657. setsubreg(result,subreg);
  658. { notify RA }
  659. if result<>reg then
  660. list.concat(tai_regalloc.resize(result));
  661. end;
  662. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  663. begin
  664. if not assigned(rg[getregtype(r)]) then
  665. internalerror(200312125);
  666. rg[getregtype(r)].getcpuregister(list,r);
  667. end;
  668. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  669. begin
  670. if not assigned(rg[getregtype(r)]) then
  671. internalerror(200312126);
  672. rg[getregtype(r)].ungetcpuregister(list,r);
  673. end;
  674. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  675. begin
  676. if assigned(rg[rt]) then
  677. rg[rt].alloccpuregisters(list,r)
  678. else
  679. internalerror(200310092);
  680. end;
  681. procedure tcg.allocallcpuregisters(list:TAsmList);
  682. begin
  683. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  684. if uses_registers(R_ADDRESSREGISTER) then
  685. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  686. {$if not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  687. if uses_registers(R_FPUREGISTER) then
  688. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  689. {$ifdef cpumm}
  690. if uses_registers(R_MMREGISTER) then
  691. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  692. {$endif cpumm}
  693. {$endif not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  694. end;
  695. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  696. begin
  697. if assigned(rg[rt]) then
  698. rg[rt].dealloccpuregisters(list,r)
  699. else
  700. internalerror(200310093);
  701. end;
  702. procedure tcg.deallocallcpuregisters(list:TAsmList);
  703. begin
  704. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  705. if uses_registers(R_ADDRESSREGISTER) then
  706. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  707. {$if not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  708. if uses_registers(R_FPUREGISTER) then
  709. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  710. {$ifdef cpumm}
  711. if uses_registers(R_MMREGISTER) then
  712. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  713. {$endif cpumm}
  714. {$endif not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  715. end;
  716. function tcg.uses_registers(rt:Tregistertype):boolean;
  717. begin
  718. if assigned(rg[rt]) then
  719. result:=rg[rt].uses_registers
  720. else
  721. result:=false;
  722. end;
  723. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  724. var
  725. rt : tregistertype;
  726. begin
  727. rt:=getregtype(r);
  728. { Only add it when a register allocator is configured.
  729. No IE can be generated, because the VMT is written
  730. without a valid rg[] }
  731. if assigned(rg[rt]) then
  732. rg[rt].add_reg_instruction(instr,r,executionweight);
  733. end;
  734. procedure tcg.add_move_instruction(instr:Taicpu);
  735. var
  736. rt : tregistertype;
  737. begin
  738. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  739. if assigned(rg[rt]) then
  740. rg[rt].add_move_instruction(instr)
  741. else
  742. internalerror(200310095);
  743. end;
  744. procedure tcg.set_regalloc_live_range_direction(dir: TRADirection);
  745. var
  746. rt : tregistertype;
  747. begin
  748. for rt:=low(rg) to high(rg) do
  749. begin
  750. if assigned(rg[rt]) then
  751. rg[rt].live_range_direction:=dir;
  752. end;
  753. end;
  754. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  755. var
  756. rt : tregistertype;
  757. begin
  758. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  759. begin
  760. if assigned(rg[rt]) then
  761. rg[rt].do_register_allocation(list,headertai);
  762. end;
  763. { running the other register allocator passes could require addition int/addr. registers
  764. when spilling so run int/addr register allocation at the end }
  765. if assigned(rg[R_INTREGISTER]) then
  766. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  767. if assigned(rg[R_ADDRESSREGISTER]) then
  768. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  769. end;
  770. procedure tcg.translate_register(var reg : tregister);
  771. var
  772. rt: tregistertype;
  773. begin
  774. { Getting here without assigned rg is possible for an "assembler nostackframe"
  775. function returning x87 float, compiler tries to translate NR_ST which is used for
  776. result. }
  777. rt:=getregtype(reg);
  778. if assigned(rg[rt]) then
  779. rg[rt].translate_register(reg);
  780. end;
  781. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  782. begin
  783. list.concat(tai_regalloc.alloc(r,nil));
  784. end;
  785. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  786. begin
  787. if (r<>NR_NO) then
  788. list.concat(tai_regalloc.dealloc(r,nil));
  789. end;
  790. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  791. var
  792. instr : tai;
  793. begin
  794. instr:=tai_regalloc.sync(r);
  795. list.concat(instr);
  796. add_reg_instruction(instr,r);
  797. end;
  798. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  799. begin
  800. list.concat(tai_label.create(l));
  801. end;
  802. {*****************************************************************************
  803. for better code generation these methods should be overridden
  804. ******************************************************************************}
  805. procedure tcg.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  806. var
  807. ref : treference;
  808. tmpreg : tregister;
  809. begin
  810. if assigned(cgpara.location^.next) then
  811. begin
  812. tg.gethltemp(list,cgpara.def,cgpara.def.size,tt_persistent,ref);
  813. a_load_reg_ref(list,size,size,r,ref);
  814. a_load_ref_cgpara(list,size,ref,cgpara);
  815. tg.ungettemp(list,ref);
  816. exit;
  817. end;
  818. paramanager.alloccgpara(list,cgpara);
  819. if cgpara.location^.shiftval<0 then
  820. begin
  821. tmpreg:=getintregister(list,cgpara.location^.size);
  822. a_op_const_reg_reg(list,OP_SHL,cgpara.location^.size,-cgpara.location^.shiftval,r,tmpreg);
  823. r:=tmpreg;
  824. end;
  825. case cgpara.location^.loc of
  826. LOC_REGISTER,LOC_CREGISTER:
  827. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  828. LOC_REFERENCE,LOC_CREFERENCE:
  829. begin
  830. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  831. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  832. end;
  833. LOC_MMREGISTER,LOC_CMMREGISTER:
  834. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  835. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  836. begin
  837. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  838. a_load_reg_ref(list,size,size,r,ref);
  839. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  840. tg.Ungettemp(list,ref);
  841. end
  842. else
  843. internalerror(2002071004);
  844. end;
  845. end;
  846. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  847. var
  848. ref : treference;
  849. begin
  850. cgpara.check_simple_location;
  851. paramanager.alloccgpara(list,cgpara);
  852. if cgpara.location^.shiftval<0 then
  853. a:=a shl -cgpara.location^.shiftval;
  854. case cgpara.location^.loc of
  855. LOC_REGISTER,LOC_CREGISTER:
  856. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  857. LOC_REFERENCE,LOC_CREFERENCE:
  858. begin
  859. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  860. a_load_const_ref(list,cgpara.location^.size,a,ref);
  861. end
  862. else
  863. internalerror(2010053109);
  864. end;
  865. end;
  866. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  867. var
  868. tmpref, ref: treference;
  869. tmpreg: tregister;
  870. location: pcgparalocation;
  871. orgsizeleft,
  872. sizeleft: tcgint;
  873. usesize: tcgsize;
  874. reghasvalue: boolean;
  875. begin
  876. location:=cgpara.location;
  877. tmpref:=r;
  878. sizeleft:=cgpara.intsize;
  879. repeat
  880. paramanager.allocparaloc(list,location);
  881. case location^.loc of
  882. LOC_REGISTER,LOC_CREGISTER:
  883. begin
  884. { Parameter locations are often allocated in multiples of
  885. entire registers. If a parameter only occupies a part of
  886. such a register (e.g. a 16 bit int on a 32 bit
  887. architecture), the size of this parameter can only be
  888. determined by looking at the "size" parameter of this
  889. method -> if the size parameter is <= sizeof(aint), then
  890. we check that there is only one parameter location and
  891. then use this "size" to load the value into the parameter
  892. location }
  893. if (size<>OS_NO) and
  894. (tcgsize2size[size]<=sizeof(aint)) then
  895. begin
  896. cgpara.check_simple_location;
  897. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  898. if location^.shiftval<0 then
  899. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  900. end
  901. { there's a lot more data left, and the current paraloc's
  902. register is entirely filled with part of that data }
  903. else if (sizeleft>sizeof(aint)) then
  904. begin
  905. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  906. end
  907. { we're at the end of the data, and it can be loaded into
  908. the current location's register with a single regular
  909. load }
  910. else if sizeleft in [1,2,4,8] then
  911. begin
  912. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  913. if location^.shiftval<0 then
  914. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  915. end
  916. { we're at the end of the data, and we need multiple loads
  917. to get it in the register because it's an irregular size }
  918. else
  919. begin
  920. { should be the last part }
  921. if assigned(location^.next) then
  922. internalerror(2010052907);
  923. { load the value piecewise to get it into the register }
  924. orgsizeleft:=sizeleft;
  925. reghasvalue:=false;
  926. {$ifdef cpu64bitalu}
  927. if sizeleft>=4 then
  928. begin
  929. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  930. dec(sizeleft,4);
  931. if target_info.endian=endian_big then
  932. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  933. inc(tmpref.offset,4);
  934. reghasvalue:=true;
  935. end;
  936. {$endif cpu64bitalu}
  937. if sizeleft>=2 then
  938. begin
  939. tmpreg:=getintregister(list,location^.size);
  940. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  941. dec(sizeleft,2);
  942. if reghasvalue then
  943. begin
  944. if target_info.endian=endian_big then
  945. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  946. else
  947. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  948. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  949. end
  950. else
  951. begin
  952. if target_info.endian=endian_big then
  953. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  954. else
  955. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  956. end;
  957. inc(tmpref.offset,2);
  958. reghasvalue:=true;
  959. end;
  960. if sizeleft=1 then
  961. begin
  962. tmpreg:=getintregister(list,location^.size);
  963. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  964. dec(sizeleft,1);
  965. if reghasvalue then
  966. begin
  967. if target_info.endian=endian_little then
  968. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  969. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  970. end
  971. else
  972. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  973. inc(tmpref.offset);
  974. end;
  975. if location^.shiftval<0 then
  976. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  977. { the loop will already adjust the offset and sizeleft }
  978. dec(tmpref.offset,orgsizeleft);
  979. sizeleft:=orgsizeleft;
  980. end;
  981. end;
  982. LOC_REFERENCE,LOC_CREFERENCE:
  983. begin
  984. reference_reset_base(ref,location^.reference.index,location^.reference.offset,ctempposinvalid,newalignment(cgpara.alignment,cgpara.intsize-sizeleft),[]);
  985. a_load_ref_cgparalocref(list,size,sizeleft,tmpref,ref,cgpara,location);
  986. end;
  987. LOC_MMREGISTER,LOC_CMMREGISTER:
  988. begin
  989. case location^.size of
  990. OS_F32,
  991. OS_F64,
  992. OS_F128:
  993. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  994. OS_M8..OS_M512:
  995. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  996. else
  997. internalerror(2010053101);
  998. end;
  999. end;
  1000. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1001. begin
  1002. { can be not a float size in case of a record passed in fpu registers }
  1003. { the size comparison is to catch F128 passed in two 64 bit floating point registers }
  1004. if is_float_cgsize(size) and
  1005. (tcgsize2size[location^.size]>=tcgsize2size[size]) then
  1006. usesize:=size
  1007. else
  1008. usesize:=location^.size;
  1009. a_loadfpu_ref_reg(list,usesize,location^.size,tmpref,location^.register);
  1010. end
  1011. else
  1012. internalerror(2010053111);
  1013. end;
  1014. inc(tmpref.offset,tcgsize2size[location^.size]);
  1015. dec(sizeleft,tcgsize2size[location^.size]);
  1016. location:=location^.next;
  1017. until not assigned(location);
  1018. end;
  1019. procedure tcg.a_load_ref_cgparalocref(list: TAsmList; sourcesize: tcgsize; sizeleft: tcgint; const ref, paralocref: treference; const cgpara: tcgpara; const location: PCGParaLocation);
  1020. begin
  1021. if assigned(location^.next) then
  1022. internalerror(2010052906);
  1023. if (sourcesize<>OS_NO) and
  1024. (tcgsize2size[sourcesize]<=sizeof(aint)) then
  1025. a_load_ref_ref(list,sourcesize,location^.size,ref,paralocref)
  1026. else
  1027. { use concatcopy, because the parameter can be larger than }
  1028. { what the OS_* constants can handle }
  1029. g_concatcopy(list,ref,paralocref,sizeleft);
  1030. end;
  1031. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  1032. begin
  1033. case l.loc of
  1034. LOC_REGISTER,
  1035. LOC_CREGISTER :
  1036. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  1037. LOC_CONSTANT :
  1038. a_load_const_cgpara(list,l.size,l.value,cgpara);
  1039. LOC_CREFERENCE,
  1040. LOC_REFERENCE :
  1041. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  1042. else
  1043. internalerror(2002032211);
  1044. end;
  1045. end;
  1046. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  1047. var
  1048. hr : tregister;
  1049. begin
  1050. cgpara.check_simple_location;
  1051. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1052. begin
  1053. paramanager.allocparaloc(list,cgpara.location);
  1054. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  1055. end
  1056. else
  1057. begin
  1058. hr:=getaddressregister(list);
  1059. a_loadaddr_ref_reg(list,r,hr);
  1060. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  1061. end;
  1062. end;
  1063. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  1064. var
  1065. href : treference;
  1066. hreg : tregister;
  1067. cgsize: tcgsize;
  1068. begin
  1069. case paraloc.loc of
  1070. LOC_REGISTER :
  1071. begin
  1072. hreg:=paraloc.register;
  1073. cgsize:=paraloc.size;
  1074. if paraloc.shiftval>0 then
  1075. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  1076. { in case the original size was 3 or 5/6/7 bytes, the value was
  1077. shifted to the top of the to 4 resp. 8 byte register on the
  1078. caller side and needs to be stored with those bytes at the
  1079. start of the reference -> don't shift right }
  1080. else if (paraloc.shiftval<0) and
  1081. ((-paraloc.shiftval) in [8,16,32]) then
  1082. begin
  1083. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1084. { convert to a register of 1/2/4 bytes in size, since the
  1085. original register had to be made larger to be able to hold
  1086. the shifted value }
  1087. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  1088. if cgsize=OS_NO then
  1089. cgsize:=OS_INT;
  1090. hreg:=getintregister(list,cgsize);
  1091. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  1092. end;
  1093. { use the exact size to avoid overwriting of adjacent data }
  1094. if tcgsize2size[cgsize]<=sizeleft then
  1095. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref)
  1096. else
  1097. case sizeleft of
  1098. 1,2,4,8:
  1099. a_load_reg_ref(list,paraloc.size,int_cgsize(sizeleft),hreg,ref);
  1100. 3:
  1101. begin
  1102. if target_info.endian=endian_big then
  1103. begin
  1104. href:=ref;
  1105. inc(href.offset,2);
  1106. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1107. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1108. a_load_reg_ref(list,paraloc.size,OS_16,hreg,ref);
  1109. end
  1110. else
  1111. begin
  1112. a_load_reg_ref(list,paraloc.size,OS_16,hreg,ref);
  1113. href:=ref;
  1114. inc(href.offset,2);
  1115. a_op_const_reg_reg(list,OP_SHR,cgsize,16,hreg,hreg);
  1116. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1117. end
  1118. end;
  1119. 5:
  1120. begin
  1121. if target_info.endian=endian_big then
  1122. begin
  1123. href:=ref;
  1124. inc(href.offset,4);
  1125. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1126. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1127. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1128. end
  1129. else
  1130. begin
  1131. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1132. href:=ref;
  1133. inc(href.offset,4);
  1134. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1135. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1136. end
  1137. end;
  1138. 6:
  1139. begin
  1140. if target_info.endian=endian_big then
  1141. begin
  1142. href:=ref;
  1143. inc(href.offset,4);
  1144. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1145. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,hreg,hreg);
  1146. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1147. end
  1148. else
  1149. begin
  1150. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1151. href:=ref;
  1152. inc(href.offset,4);
  1153. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1154. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1155. end
  1156. end;
  1157. 7:
  1158. begin
  1159. if target_info.endian=endian_big then
  1160. begin
  1161. href:=ref;
  1162. inc(href.offset,6);
  1163. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1164. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1165. href:=ref;
  1166. inc(href.offset,4);
  1167. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1168. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,hreg,hreg);
  1169. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1170. end
  1171. else
  1172. begin
  1173. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1174. href:=ref;
  1175. inc(href.offset,4);
  1176. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1177. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1178. inc(href.offset,2);
  1179. a_op_const_reg_reg(list,OP_SHR,cgsize,16,hreg,hreg);
  1180. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1181. end
  1182. end;
  1183. else
  1184. { other sizes not allowed }
  1185. Internalerror(2017080901);
  1186. end;
  1187. end;
  1188. LOC_MMREGISTER :
  1189. begin
  1190. case paraloc.size of
  1191. OS_F32,
  1192. OS_F64,
  1193. OS_F128:
  1194. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1195. OS_M8..OS_M512:
  1196. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  1197. else
  1198. internalerror(2010053102);
  1199. end;
  1200. end;
  1201. LOC_FPUREGISTER :
  1202. a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1203. LOC_REFERENCE :
  1204. begin
  1205. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,ctempposinvalid,align,[]);
  1206. { use concatcopy, because it can also be a float which fails when
  1207. load_ref_ref is used. Don't copy data when the references are equal }
  1208. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1209. g_concatcopy(list,href,ref,sizeleft);
  1210. end;
  1211. else
  1212. internalerror(2002081302);
  1213. end;
  1214. end;
  1215. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  1216. var
  1217. href : treference;
  1218. begin
  1219. case paraloc.loc of
  1220. LOC_REGISTER :
  1221. begin
  1222. if paraloc.shiftval<0 then
  1223. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1224. case getregtype(reg) of
  1225. R_ADDRESSREGISTER,
  1226. R_INTREGISTER:
  1227. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1228. R_MMREGISTER:
  1229. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1230. R_FPUREGISTER:
  1231. a_loadfpu_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1232. else
  1233. internalerror(2009112422);
  1234. end;
  1235. end;
  1236. LOC_MMREGISTER :
  1237. begin
  1238. case getregtype(reg) of
  1239. R_ADDRESSREGISTER,
  1240. R_INTREGISTER:
  1241. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1242. R_MMREGISTER:
  1243. begin
  1244. case paraloc.size of
  1245. OS_F32,
  1246. OS_F64,
  1247. OS_F128:
  1248. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1249. OS_M8..OS_M512:
  1250. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1251. else
  1252. internalerror(2010053102);
  1253. end;
  1254. end;
  1255. else
  1256. internalerror(2010053104);
  1257. end;
  1258. end;
  1259. LOC_FPUREGISTER :
  1260. begin
  1261. case getregtype(reg) of
  1262. R_FPUREGISTER:
  1263. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg)
  1264. else
  1265. internalerror(2015031401);
  1266. end;
  1267. end;
  1268. LOC_REFERENCE :
  1269. begin
  1270. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,ctempposinvalid,align,[]);
  1271. case getregtype(reg) of
  1272. R_ADDRESSREGISTER,
  1273. R_INTREGISTER :
  1274. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1275. R_FPUREGISTER :
  1276. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1277. R_MMREGISTER :
  1278. { not paraloc.size, because it may be OS_64 instead of
  1279. OS_F64 in case the parameter is passed using integer
  1280. conventions (e.g., on ARM) }
  1281. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1282. else
  1283. internalerror(2004101012);
  1284. end;
  1285. end;
  1286. else
  1287. internalerror(2002081302);
  1288. end;
  1289. end;
  1290. {****************************************************************************
  1291. some generic implementations
  1292. ****************************************************************************}
  1293. { memory/register loading }
  1294. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1295. var
  1296. tmpref : treference;
  1297. tmpreg : tregister;
  1298. i : longint;
  1299. begin
  1300. if ref.alignment<tcgsize2size[fromsize] then
  1301. begin
  1302. tmpref:=ref;
  1303. { we take care of the alignment now }
  1304. tmpref.alignment:=0;
  1305. case FromSize of
  1306. OS_16,OS_S16:
  1307. begin
  1308. tmpreg:=getintregister(list,OS_16);
  1309. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1310. if target_info.endian=endian_big then
  1311. inc(tmpref.offset);
  1312. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1313. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1314. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1315. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1316. if target_info.endian=endian_big then
  1317. dec(tmpref.offset)
  1318. else
  1319. inc(tmpref.offset);
  1320. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1321. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1322. end;
  1323. OS_32,OS_S32:
  1324. begin
  1325. { could add an optimised case for ref.alignment=2 }
  1326. tmpreg:=getintregister(list,OS_32);
  1327. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1328. if target_info.endian=endian_big then
  1329. inc(tmpref.offset,3);
  1330. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1331. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1332. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1333. for i:=1 to 3 do
  1334. begin
  1335. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1336. if target_info.endian=endian_big then
  1337. dec(tmpref.offset)
  1338. else
  1339. inc(tmpref.offset);
  1340. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1341. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1342. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1343. end;
  1344. end
  1345. else
  1346. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1347. end;
  1348. end
  1349. else
  1350. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1351. end;
  1352. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1353. var
  1354. tmpref : treference;
  1355. tmpreg,
  1356. tmpreg2 : tregister;
  1357. i : longint;
  1358. hisize : tcgsize;
  1359. begin
  1360. if ref.alignment in [1,2] then
  1361. begin
  1362. tmpref:=ref;
  1363. { we take care of the alignment now }
  1364. tmpref.alignment:=0;
  1365. case FromSize of
  1366. OS_16,OS_S16:
  1367. if ref.alignment=2 then
  1368. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1369. else
  1370. begin
  1371. if FromSize=OS_16 then
  1372. hisize:=OS_8
  1373. else
  1374. hisize:=OS_S8;
  1375. { first load in tmpreg, because the target register }
  1376. { may be used in ref as well }
  1377. if target_info.endian=endian_little then
  1378. inc(tmpref.offset);
  1379. tmpreg:=getintregister(list,OS_8);
  1380. a_load_ref_reg(list,hisize,hisize,tmpref,tmpreg);
  1381. tmpreg:=makeregsize(list,tmpreg,FromSize);
  1382. a_op_const_reg(list,OP_SHL,FromSize,8,tmpreg);
  1383. if target_info.endian=endian_little then
  1384. dec(tmpref.offset)
  1385. else
  1386. inc(tmpref.offset);
  1387. tmpreg2:=makeregsize(list,register,OS_16);
  1388. a_load_ref_reg(list,OS_8,OS_16,tmpref,tmpreg2);
  1389. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,tmpreg2);
  1390. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1391. end;
  1392. OS_32,OS_S32:
  1393. if ref.alignment=2 then
  1394. begin
  1395. if target_info.endian=endian_little then
  1396. inc(tmpref.offset,2);
  1397. tmpreg:=getintregister(list,OS_32);
  1398. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1399. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1400. if target_info.endian=endian_little then
  1401. dec(tmpref.offset,2)
  1402. else
  1403. inc(tmpref.offset,2);
  1404. tmpreg2:=makeregsize(list,register,OS_32);
  1405. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg2);
  1406. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,tmpreg2);
  1407. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1408. end
  1409. else
  1410. begin
  1411. if target_info.endian=endian_little then
  1412. inc(tmpref.offset,3);
  1413. tmpreg:=getintregister(list,OS_32);
  1414. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1415. tmpreg2:=getintregister(list,OS_32);
  1416. for i:=1 to 3 do
  1417. begin
  1418. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1419. if target_info.endian=endian_little then
  1420. dec(tmpref.offset)
  1421. else
  1422. inc(tmpref.offset);
  1423. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1424. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1425. end;
  1426. a_load_reg_reg(list,fromsize,tosize,tmpreg,register);
  1427. end
  1428. else
  1429. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1430. end;
  1431. end
  1432. else
  1433. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1434. end;
  1435. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1436. var
  1437. tmpreg: tregister;
  1438. begin
  1439. { verify if we have the same reference }
  1440. if references_equal(sref,dref) then
  1441. exit;
  1442. tmpreg:=getintregister(list,tosize);
  1443. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1444. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1445. end;
  1446. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1447. var
  1448. tmpreg: tregister;
  1449. begin
  1450. tmpreg:=getintregister(list,size);
  1451. a_load_const_reg(list,size,a,tmpreg);
  1452. a_load_reg_ref(list,size,size,tmpreg,ref);
  1453. end;
  1454. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1455. begin
  1456. case loc.loc of
  1457. LOC_REFERENCE,LOC_CREFERENCE:
  1458. a_load_const_ref(list,loc.size,a,loc.reference);
  1459. LOC_REGISTER,LOC_CREGISTER:
  1460. a_load_const_reg(list,loc.size,a,loc.register);
  1461. else
  1462. internalerror(200203272);
  1463. end;
  1464. end;
  1465. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1466. begin
  1467. case loc.loc of
  1468. LOC_REFERENCE,LOC_CREFERENCE:
  1469. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1470. LOC_REGISTER,LOC_CREGISTER:
  1471. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1472. LOC_MMREGISTER,LOC_CMMREGISTER:
  1473. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1474. else
  1475. internalerror(200203271);
  1476. end;
  1477. end;
  1478. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1479. begin
  1480. case loc.loc of
  1481. LOC_REFERENCE,LOC_CREFERENCE:
  1482. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1483. LOC_REGISTER,LOC_CREGISTER:
  1484. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1485. LOC_CONSTANT:
  1486. a_load_const_reg(list,tosize,loc.value,reg);
  1487. LOC_MMREGISTER,LOC_CMMREGISTER:
  1488. a_loadmm_reg_intreg(list,loc.size,tosize,loc.register,reg,mms_movescalar);
  1489. else
  1490. internalerror(200109092);
  1491. end;
  1492. end;
  1493. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1494. begin
  1495. case loc.loc of
  1496. LOC_REFERENCE,LOC_CREFERENCE:
  1497. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1498. LOC_REGISTER,LOC_CREGISTER:
  1499. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1500. LOC_CONSTANT:
  1501. a_load_const_ref(list,tosize,loc.value,ref);
  1502. else
  1503. internalerror(200109302);
  1504. end;
  1505. end;
  1506. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1507. var
  1508. powerval : longint;
  1509. signext_a, zeroext_a: tcgint;
  1510. begin
  1511. case size of
  1512. OS_64,OS_S64:
  1513. begin
  1514. signext_a:=int64(a);
  1515. zeroext_a:=int64(a);
  1516. end;
  1517. OS_32,OS_S32:
  1518. begin
  1519. signext_a:=longint(a);
  1520. zeroext_a:=dword(a);
  1521. end;
  1522. OS_16,OS_S16:
  1523. begin
  1524. signext_a:=smallint(a);
  1525. zeroext_a:=word(a);
  1526. end;
  1527. OS_8,OS_S8:
  1528. begin
  1529. signext_a:=shortint(a);
  1530. zeroext_a:=byte(a);
  1531. end
  1532. else
  1533. begin
  1534. { Should we internalerror() here instead? }
  1535. signext_a:=a;
  1536. zeroext_a:=a;
  1537. end;
  1538. end;
  1539. case op of
  1540. OP_OR :
  1541. begin
  1542. { or with zero returns same result }
  1543. if a = 0 then
  1544. op:=OP_NONE
  1545. else
  1546. { or with max returns max }
  1547. if signext_a = -1 then
  1548. op:=OP_MOVE;
  1549. end;
  1550. OP_AND :
  1551. begin
  1552. { and with max returns same result }
  1553. if (signext_a = -1) then
  1554. op:=OP_NONE
  1555. else
  1556. { and with 0 returns 0 }
  1557. if a=0 then
  1558. op:=OP_MOVE;
  1559. end;
  1560. OP_XOR :
  1561. begin
  1562. { xor with zero returns same result }
  1563. if a = 0 then
  1564. op:=OP_NONE;
  1565. end;
  1566. OP_DIV :
  1567. begin
  1568. { division by 1 returns result }
  1569. if a = 1 then
  1570. op:=OP_NONE
  1571. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1572. begin
  1573. a := powerval;
  1574. op:= OP_SHR;
  1575. end;
  1576. end;
  1577. OP_IDIV:
  1578. begin
  1579. if a = 1 then
  1580. op:=OP_NONE;
  1581. end;
  1582. OP_MUL,OP_IMUL:
  1583. begin
  1584. if a = 1 then
  1585. op:=OP_NONE
  1586. else
  1587. if a=0 then
  1588. op:=OP_MOVE
  1589. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1590. begin
  1591. a := powerval;
  1592. op:= OP_SHL;
  1593. end;
  1594. end;
  1595. OP_ADD,OP_SUB:
  1596. begin
  1597. if a = 0 then
  1598. op:=OP_NONE;
  1599. end;
  1600. OP_SAR,OP_SHL,OP_SHR:
  1601. begin
  1602. if a = 0 then
  1603. op:=OP_NONE;
  1604. end;
  1605. OP_ROL,OP_ROR:
  1606. begin
  1607. case size of
  1608. OS_64,OS_S64:
  1609. a:=a and 63;
  1610. OS_32,OS_S32:
  1611. a:=a and 31;
  1612. OS_16,OS_S16:
  1613. a:=a and 15;
  1614. OS_8,OS_S8:
  1615. a:=a and 7;
  1616. end;
  1617. if a = 0 then
  1618. op:=OP_NONE;
  1619. end;
  1620. end;
  1621. end;
  1622. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1623. begin
  1624. case loc.loc of
  1625. LOC_REFERENCE, LOC_CREFERENCE:
  1626. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1627. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1628. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1629. else
  1630. internalerror(200203301);
  1631. end;
  1632. end;
  1633. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1634. begin
  1635. case loc.loc of
  1636. LOC_REFERENCE, LOC_CREFERENCE:
  1637. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1638. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1639. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1640. else
  1641. internalerror(48991);
  1642. end;
  1643. end;
  1644. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1645. var
  1646. reg: tregister;
  1647. regsize: tcgsize;
  1648. begin
  1649. if (fromsize>=tosize) then
  1650. regsize:=fromsize
  1651. else
  1652. regsize:=tosize;
  1653. reg:=getfpuregister(list,regsize);
  1654. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1655. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1656. end;
  1657. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1658. var
  1659. ref : treference;
  1660. begin
  1661. paramanager.alloccgpara(list,cgpara);
  1662. case cgpara.location^.loc of
  1663. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1664. begin
  1665. cgpara.check_simple_location;
  1666. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1667. end;
  1668. LOC_REFERENCE,LOC_CREFERENCE:
  1669. begin
  1670. cgpara.check_simple_location;
  1671. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1672. a_loadfpu_reg_ref(list,size,size,r,ref);
  1673. end;
  1674. LOC_REGISTER,LOC_CREGISTER:
  1675. begin
  1676. { paramfpu_ref does the check_simpe_location check here if necessary }
  1677. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1678. a_loadfpu_reg_ref(list,size,size,r,ref);
  1679. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1680. tg.Ungettemp(list,ref);
  1681. end;
  1682. else
  1683. internalerror(2010053112);
  1684. end;
  1685. end;
  1686. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1687. var
  1688. srcref,
  1689. href : treference;
  1690. srcsize,
  1691. hsize: tcgsize;
  1692. paraloc: PCGParaLocation;
  1693. sizeleft: tcgint;
  1694. begin
  1695. sizeleft:=cgpara.intsize;
  1696. paraloc:=cgpara.location;
  1697. paramanager.alloccgpara(list,cgpara);
  1698. srcref:=ref;
  1699. repeat
  1700. case paraloc^.loc of
  1701. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1702. begin
  1703. { destination: can be something different in case of a record passed in fpu registers }
  1704. if is_float_cgsize(paraloc^.size) then
  1705. hsize:=paraloc^.size
  1706. else
  1707. hsize:=int_float_cgsize(tcgsize2size[paraloc^.size]);
  1708. { source: the size comparison is to catch F128 passed in two 64 bit floating point registers }
  1709. if is_float_cgsize(size) and
  1710. (tcgsize2size[size]<=tcgsize2size[paraloc^.size]) then
  1711. srcsize:=size
  1712. else
  1713. srcsize:=hsize;
  1714. a_loadfpu_ref_reg(list,srcsize,hsize,srcref,paraloc^.register);
  1715. end;
  1716. LOC_REFERENCE,LOC_CREFERENCE:
  1717. begin
  1718. if assigned(paraloc^.next) then
  1719. internalerror(2020050101);
  1720. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,ctempposinvalid,newalignment(cgpara.alignment,cgpara.intsize-sizeleft),[]);
  1721. { concatcopy should choose the best way to copy the data }
  1722. g_concatcopy(list,srcref,href,sizeleft);
  1723. end;
  1724. LOC_REGISTER,LOC_CREGISTER:
  1725. begin
  1726. { force integer size }
  1727. hsize:=int_cgsize(tcgsize2size[paraloc^.size]);
  1728. {$ifndef cpu64bitalu}
  1729. if (hsize in [OS_S64,OS_64]) then
  1730. begin
  1731. { if this is not a simple location, we'll have to add support to cg64 to load parts of a cgpara }
  1732. cgpara.check_simple_location;
  1733. cg64.a_load64_ref_cgpara(list,srcref,cgpara)
  1734. end
  1735. else
  1736. {$endif not cpu64bitalu}
  1737. begin
  1738. a_load_ref_reg(list,hsize,hsize,srcref,paraloc^.register)
  1739. end;
  1740. end
  1741. else
  1742. internalerror(200402201);
  1743. end;
  1744. inc(srcref.offset,tcgsize2size[paraloc^.size]);
  1745. dec(sizeleft,tcgsize2size[paraloc^.size]);
  1746. paraloc:=paraloc^.next;
  1747. until not assigned(paraloc);
  1748. end;
  1749. procedure tcg.a_loadfpu_intreg_reg(list : TAsmList; fromsize,tosize : tcgsize; intreg,fpureg : tregister);
  1750. var
  1751. tmpref: treference;
  1752. begin
  1753. if not(tcgsize2size[fromsize] in [4,8]) or
  1754. not(tcgsize2size[tosize] in [4,8]) or
  1755. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  1756. internalerror(2017070902);
  1757. tg.gettemp(list,tcgsize2size[fromsize],tcgsize2size[fromsize],tt_normal,tmpref);
  1758. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1759. a_loadfpu_ref_reg(list,tosize,tosize,tmpref,fpureg);
  1760. tg.ungettemp(list,tmpref);
  1761. end;
  1762. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1763. var
  1764. tmpreg : tregister;
  1765. tmpref : treference;
  1766. begin
  1767. if assigned(ref.symbol)
  1768. { for avrtiny, the code generator generates a ref which is Z relative and while using it,
  1769. Z is changed, so the following code breaks }
  1770. {$ifdef avr}
  1771. and not((CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) or (tcgsize2size[size]=1))
  1772. {$endif avr} then
  1773. begin
  1774. tmpreg:=getaddressregister(list);
  1775. a_loadaddr_ref_reg(list,ref,tmpreg);
  1776. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1777. end
  1778. else
  1779. tmpref:=ref;
  1780. tmpreg:=getintregister(list,size);
  1781. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  1782. a_op_const_reg(list,op,size,a,tmpreg);
  1783. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1784. end;
  1785. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1786. begin
  1787. case loc.loc of
  1788. LOC_REGISTER, LOC_CREGISTER:
  1789. a_op_const_reg(list,op,loc.size,a,loc.register);
  1790. LOC_REFERENCE, LOC_CREFERENCE:
  1791. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1792. else
  1793. internalerror(200109061);
  1794. end;
  1795. end;
  1796. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1797. var
  1798. tmpreg : tregister;
  1799. tmpref : treference;
  1800. begin
  1801. if assigned(ref.symbol)
  1802. { for avrtiny, the code generator generates a ref which is Z relative and while using it,
  1803. Z is changed, so the following code breaks }
  1804. {$ifdef avr}
  1805. and not((CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) or (tcgsize2size[size]=1))
  1806. {$endif avr} then
  1807. begin
  1808. tmpreg:=getaddressregister(list);
  1809. a_loadaddr_ref_reg(list,ref,tmpreg);
  1810. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1811. end
  1812. else
  1813. tmpref:=ref;
  1814. if op in [OP_NEG,OP_NOT] then
  1815. begin
  1816. tmpreg:=getintregister(list,size);
  1817. a_op_reg_reg(list,op,size,reg,tmpreg);
  1818. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1819. end
  1820. else
  1821. begin
  1822. tmpreg:=getintregister(list,size);
  1823. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  1824. a_op_reg_reg(list,op,size,reg,tmpreg);
  1825. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1826. end;
  1827. end;
  1828. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1829. var
  1830. tmpreg: tregister;
  1831. begin
  1832. case op of
  1833. OP_NOT,OP_NEG:
  1834. { handle it as "load ref,reg; op reg" }
  1835. begin
  1836. a_load_ref_reg(list,size,size,ref,reg);
  1837. a_op_reg_reg(list,op,size,reg,reg);
  1838. end;
  1839. else
  1840. begin
  1841. tmpreg:=getintregister(list,size);
  1842. a_load_ref_reg(list,size,size,ref,tmpreg);
  1843. a_op_reg_reg(list,op,size,tmpreg,reg);
  1844. end;
  1845. end;
  1846. end;
  1847. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1848. begin
  1849. case loc.loc of
  1850. LOC_REGISTER, LOC_CREGISTER:
  1851. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1852. LOC_REFERENCE, LOC_CREFERENCE:
  1853. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1854. else
  1855. internalerror(200109061);
  1856. end;
  1857. end;
  1858. procedure tcg.a_op_loc_reg(list : TAsmList; Op : TOpCG; size: TCGSize; const loc : tlocation; reg : tregister);
  1859. begin
  1860. case loc.loc of
  1861. LOC_REGISTER, LOC_CREGISTER:
  1862. a_op_reg_reg(list,op,size,loc.register,reg);
  1863. LOC_REFERENCE, LOC_CREFERENCE:
  1864. a_op_ref_reg(list,op,size,loc.reference,reg);
  1865. LOC_CONSTANT:
  1866. a_op_const_reg(list,op,size,loc.value,reg);
  1867. else
  1868. internalerror(2018031101);
  1869. end;
  1870. end;
  1871. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1872. var
  1873. tmpreg: tregister;
  1874. begin
  1875. case loc.loc of
  1876. LOC_REGISTER,LOC_CREGISTER:
  1877. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1878. LOC_REFERENCE,LOC_CREFERENCE:
  1879. begin
  1880. tmpreg:=getintregister(list,loc.size);
  1881. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1882. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1883. end;
  1884. else
  1885. internalerror(200109061);
  1886. end;
  1887. end;
  1888. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1889. a:tcgint;src,dst:Tregister);
  1890. begin
  1891. optimize_op_const(size, op, a);
  1892. case op of
  1893. OP_NONE:
  1894. begin
  1895. if src <> dst then
  1896. a_load_reg_reg(list, size, size, src, dst);
  1897. exit;
  1898. end;
  1899. OP_MOVE:
  1900. begin
  1901. a_load_const_reg(list, size, a, dst);
  1902. exit;
  1903. end;
  1904. {$ifdef cpu8bitalu}
  1905. OP_SHL:
  1906. begin
  1907. if a=8 then
  1908. case size of
  1909. OS_S16,OS_16:
  1910. begin
  1911. a_load_reg_reg(list,OS_8,OS_8,src,GetNextReg(dst));
  1912. a_load_const_reg(list,OS_8,0,dst);
  1913. exit;
  1914. end;
  1915. end;
  1916. end;
  1917. OP_SHR:
  1918. begin
  1919. if a=8 then
  1920. case size of
  1921. OS_S16,OS_16:
  1922. begin
  1923. a_load_reg_reg(list,OS_8,OS_8,GetNextReg(src),dst);
  1924. a_load_const_reg(list,OS_8,0,GetNextReg(dst));
  1925. exit;
  1926. end;
  1927. end;
  1928. end;
  1929. {$endif cpu8bitalu}
  1930. {$ifdef cpu16bitalu}
  1931. OP_SHL:
  1932. begin
  1933. if a=16 then
  1934. case size of
  1935. OS_S32,OS_32:
  1936. begin
  1937. a_load_reg_reg(list,OS_16,OS_16,src,GetNextReg(dst));
  1938. a_load_const_reg(list,OS_16,0,dst);
  1939. exit;
  1940. end;
  1941. end;
  1942. end;
  1943. OP_SHR:
  1944. begin
  1945. if a=16 then
  1946. case size of
  1947. OS_S32,OS_32:
  1948. begin
  1949. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(src),dst);
  1950. a_load_const_reg(list,OS_16,0,GetNextReg(dst));
  1951. exit;
  1952. end;
  1953. end;
  1954. end;
  1955. {$endif cpu16bitalu}
  1956. end;
  1957. a_load_reg_reg(list,size,size,src,dst);
  1958. a_op_const_reg(list,op,size,a,dst);
  1959. end;
  1960. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1961. size: tcgsize; src1, src2, dst: tregister);
  1962. var
  1963. tmpreg: tregister;
  1964. begin
  1965. if (dst<>src1) then
  1966. begin
  1967. a_load_reg_reg(list,size,size,src2,dst);
  1968. a_op_reg_reg(list,op,size,src1,dst);
  1969. end
  1970. else
  1971. begin
  1972. { can we do a direct operation on the target register ? }
  1973. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1974. a_op_reg_reg(list,op,size,src2,dst)
  1975. else
  1976. begin
  1977. tmpreg:=getintregister(list,size);
  1978. a_load_reg_reg(list,size,size,src2,tmpreg);
  1979. a_op_reg_reg(list,op,size,src1,tmpreg);
  1980. a_load_reg_reg(list,size,size,tmpreg,dst);
  1981. end;
  1982. end;
  1983. end;
  1984. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1985. begin
  1986. a_op_const_reg_reg(list,op,size,a,src,dst);
  1987. ovloc.loc:=LOC_VOID;
  1988. end;
  1989. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1990. begin
  1991. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1992. ovloc.loc:=LOC_VOID;
  1993. end;
  1994. procedure tcg.a_op_reg(list: TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister);
  1995. begin
  1996. if not (Op in [OP_NOT,OP_NEG]) then
  1997. internalerror(2020050701);
  1998. a_op_reg_reg(list,op,size,reg,reg);
  1999. end;
  2000. procedure tcg.a_op_ref(list: TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference);
  2001. var
  2002. tmpreg: TRegister;
  2003. tmpref: treference;
  2004. begin
  2005. if not (Op in [OP_NOT,OP_NEG]) then
  2006. internalerror(2020050701);
  2007. if assigned(ref.symbol) then
  2008. begin
  2009. tmpreg:=getaddressregister(list);
  2010. a_loadaddr_ref_reg(list,ref,tmpreg);
  2011. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  2012. end
  2013. else
  2014. tmpref:=ref;
  2015. tmpreg:=getintregister(list,size);
  2016. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  2017. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  2018. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  2019. end;
  2020. procedure tcg.a_op_loc(list: TAsmList; Op: TOpCG; const loc: tlocation);
  2021. begin
  2022. case loc.loc of
  2023. LOC_REGISTER, LOC_CREGISTER:
  2024. a_op_reg(list,op,loc.size,loc.register);
  2025. LOC_REFERENCE, LOC_CREFERENCE:
  2026. a_op_ref(list,op,loc.size,loc.reference);
  2027. else
  2028. internalerror(2020050702);
  2029. end;
  2030. end;
  2031. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  2032. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  2033. var
  2034. tmpreg: tregister;
  2035. begin
  2036. tmpreg:=getintregister(list,size);
  2037. a_load_const_reg(list,size,a,tmpreg);
  2038. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2039. end;
  2040. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2041. l : tasmlabel);
  2042. var
  2043. tmpreg: tregister;
  2044. begin
  2045. tmpreg:=getintregister(list,size);
  2046. a_load_ref_reg(list,size,size,ref,tmpreg);
  2047. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2048. end;
  2049. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  2050. l : tasmlabel);
  2051. begin
  2052. case loc.loc of
  2053. LOC_REGISTER,LOC_CREGISTER:
  2054. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2055. LOC_REFERENCE,LOC_CREFERENCE:
  2056. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2057. else
  2058. internalerror(200109061);
  2059. end;
  2060. end;
  2061. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  2062. var
  2063. tmpreg: tregister;
  2064. begin
  2065. tmpreg:=getintregister(list,size);
  2066. a_load_ref_reg(list,size,size,ref,tmpreg);
  2067. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2068. end;
  2069. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  2070. var
  2071. tmpreg: tregister;
  2072. begin
  2073. tmpreg:=getintregister(list,size);
  2074. a_load_ref_reg(list,size,size,ref,tmpreg);
  2075. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2076. end;
  2077. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  2078. begin
  2079. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2080. end;
  2081. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  2082. begin
  2083. case loc.loc of
  2084. LOC_REGISTER,
  2085. LOC_CREGISTER:
  2086. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2087. LOC_REFERENCE,
  2088. LOC_CREFERENCE :
  2089. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2090. LOC_CONSTANT:
  2091. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2092. else
  2093. internalerror(200203231);
  2094. end;
  2095. end;
  2096. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  2097. l : tasmlabel);
  2098. var
  2099. tmpreg: tregister;
  2100. begin
  2101. case loc.loc of
  2102. LOC_REGISTER,LOC_CREGISTER:
  2103. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2104. LOC_REFERENCE,LOC_CREFERENCE:
  2105. begin
  2106. tmpreg:=getintregister(list,size);
  2107. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2108. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2109. end;
  2110. else
  2111. internalerror(200109061);
  2112. end;
  2113. end;
  2114. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  2115. begin
  2116. case loc.loc of
  2117. LOC_MMREGISTER,LOC_CMMREGISTER:
  2118. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2119. LOC_REFERENCE,LOC_CREFERENCE:
  2120. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  2121. LOC_REGISTER,LOC_CREGISTER:
  2122. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2123. else
  2124. internalerror(200310121);
  2125. end;
  2126. end;
  2127. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  2128. begin
  2129. case loc.loc of
  2130. LOC_MMREGISTER,LOC_CMMREGISTER:
  2131. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  2132. LOC_REFERENCE,LOC_CREFERENCE:
  2133. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  2134. else
  2135. internalerror(200310122);
  2136. end;
  2137. end;
  2138. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  2139. var
  2140. href : treference;
  2141. {$ifndef cpu64bitalu}
  2142. tmpreg : tregister;
  2143. reg64 : tregister64;
  2144. {$endif not cpu64bitalu}
  2145. begin
  2146. {$ifndef cpu64bitalu}
  2147. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  2148. (size<>OS_F64) then
  2149. {$endif not cpu64bitalu}
  2150. cgpara.check_simple_location;
  2151. paramanager.alloccgpara(list,cgpara);
  2152. case cgpara.location^.loc of
  2153. LOC_MMREGISTER,LOC_CMMREGISTER:
  2154. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  2155. LOC_REFERENCE,LOC_CREFERENCE:
  2156. begin
  2157. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  2158. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  2159. end;
  2160. LOC_REGISTER,LOC_CREGISTER:
  2161. begin
  2162. if assigned(shuffle) and
  2163. not shufflescalar(shuffle) then
  2164. internalerror(2009112510);
  2165. {$ifndef cpu64bitalu}
  2166. if (size=OS_F64) then
  2167. begin
  2168. if not assigned(cgpara.location^.next) or
  2169. assigned(cgpara.location^.next^.next) then
  2170. internalerror(2009112512);
  2171. case cgpara.location^.next^.loc of
  2172. LOC_REGISTER,LOC_CREGISTER:
  2173. tmpreg:=cgpara.location^.next^.register;
  2174. LOC_REFERENCE,LOC_CREFERENCE:
  2175. tmpreg:=getintregister(list,OS_32);
  2176. else
  2177. internalerror(2009112910);
  2178. end;
  2179. if (target_info.endian=ENDIAN_BIG) then
  2180. begin
  2181. { paraloc^ -> high
  2182. paraloc^.next -> low }
  2183. reg64.reghi:=cgpara.location^.register;
  2184. reg64.reglo:=tmpreg;
  2185. end
  2186. else
  2187. begin
  2188. { paraloc^ -> low
  2189. paraloc^.next -> high }
  2190. reg64.reglo:=cgpara.location^.register;
  2191. reg64.reghi:=tmpreg;
  2192. end;
  2193. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  2194. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2195. begin
  2196. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  2197. internalerror(2009112911);
  2198. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  2199. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  2200. end;
  2201. end
  2202. else
  2203. {$endif not cpu64bitalu}
  2204. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  2205. end
  2206. else
  2207. internalerror(200310123);
  2208. end;
  2209. end;
  2210. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  2211. var
  2212. hr : tregister;
  2213. hs : tmmshuffle;
  2214. begin
  2215. cgpara.check_simple_location;
  2216. hr:=getmmregister(list,cgpara.location^.size);
  2217. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  2218. if realshuffle(shuffle) then
  2219. begin
  2220. hs:=shuffle^;
  2221. removeshuffles(hs);
  2222. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  2223. end
  2224. else
  2225. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  2226. end;
  2227. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  2228. begin
  2229. case loc.loc of
  2230. LOC_MMREGISTER,LOC_CMMREGISTER:
  2231. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  2232. LOC_REFERENCE,LOC_CREFERENCE:
  2233. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  2234. else
  2235. internalerror(200310123);
  2236. end;
  2237. end;
  2238. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2239. var
  2240. hr : tregister;
  2241. hs : tmmshuffle;
  2242. begin
  2243. hr:=getmmregister(list,size);
  2244. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2245. if realshuffle(shuffle) then
  2246. begin
  2247. hs:=shuffle^;
  2248. removeshuffles(hs);
  2249. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2250. end
  2251. else
  2252. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2253. end;
  2254. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2255. var
  2256. hr : tregister;
  2257. hs : tmmshuffle;
  2258. begin
  2259. hr:=getmmregister(list,size);
  2260. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2261. if realshuffle(shuffle) then
  2262. begin
  2263. hs:=shuffle^;
  2264. removeshuffles(hs);
  2265. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2266. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2267. end
  2268. else
  2269. begin
  2270. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2271. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2272. end;
  2273. end;
  2274. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  2275. var
  2276. tmpref: treference;
  2277. begin
  2278. if (tcgsize2size[fromsize]<>4) or
  2279. (tcgsize2size[tosize]<>4) then
  2280. internalerror(2009112503);
  2281. tg.gettemp(list,4,4,tt_normal,tmpref);
  2282. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  2283. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  2284. tg.ungettemp(list,tmpref);
  2285. end;
  2286. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  2287. var
  2288. tmpref: treference;
  2289. begin
  2290. if (tcgsize2size[fromsize]<>4) or
  2291. (tcgsize2size[tosize]<>4) then
  2292. internalerror(2009112504);
  2293. tg.gettemp(list,8,8,tt_normal,tmpref);
  2294. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  2295. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  2296. tg.ungettemp(list,tmpref);
  2297. end;
  2298. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2299. begin
  2300. case loc.loc of
  2301. LOC_CMMREGISTER,LOC_MMREGISTER:
  2302. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2303. LOC_CREFERENCE,LOC_REFERENCE:
  2304. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2305. else
  2306. internalerror(200312232);
  2307. end;
  2308. end;
  2309. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  2310. begin
  2311. case loc.loc of
  2312. LOC_CMMREGISTER,LOC_MMREGISTER:
  2313. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  2314. LOC_CREFERENCE,LOC_REFERENCE:
  2315. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  2316. else
  2317. internalerror(200312232);
  2318. end;
  2319. end;
  2320. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2321. src1,src2,dst : tregister;shuffle : pmmshuffle);
  2322. begin
  2323. internalerror(2013061102);
  2324. end;
  2325. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2326. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  2327. begin
  2328. internalerror(2013061101);
  2329. end;
  2330. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2331. begin
  2332. g_concatcopy(list,source,dest,len);
  2333. end;
  2334. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2335. begin
  2336. g_overflowCheck(list,loc,def);
  2337. end;
  2338. {$ifdef cpuflags}
  2339. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  2340. var
  2341. tmpreg : tregister;
  2342. begin
  2343. tmpreg:=getintregister(list,size);
  2344. g_flags2reg(list,size,f,tmpreg);
  2345. a_load_reg_ref(list,size,size,tmpreg,ref);
  2346. end;
  2347. {$endif cpuflags}
  2348. procedure tcg.g_check_for_fpu_exception(list: TAsmList);
  2349. begin
  2350. { empty by default }
  2351. end;
  2352. {*****************************************************************************
  2353. Entry/Exit Code Functions
  2354. *****************************************************************************}
  2355. procedure tcg.g_save_registers(list:TAsmList);
  2356. var
  2357. href : treference;
  2358. size : longint;
  2359. r : integer;
  2360. regs_to_save_int,
  2361. regs_to_save_address,
  2362. regs_to_save_mm : tcpuregisterarray;
  2363. begin
  2364. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2365. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  2366. regs_to_save_mm:=paramanager.get_saved_registers_mm(current_procinfo.procdef.proccalloption);
  2367. { calculate temp. size }
  2368. size:=0;
  2369. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2370. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2371. inc(size,sizeof(aint));
  2372. if uses_registers(R_ADDRESSREGISTER) then
  2373. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2374. if regs_to_save_int[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2375. inc(size,sizeof(aint));
  2376. { mm registers }
  2377. if uses_registers(R_MMREGISTER) then
  2378. begin
  2379. { Make sure we reserve enough space to do the alignment based on the offset
  2380. later on. We can't use the size for this, because the alignment of the start
  2381. of the temp is smaller than needed for an OS_VECTOR }
  2382. inc(size,tcgsize2size[OS_VECTOR]);
  2383. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2384. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2385. inc(size,tcgsize2size[OS_VECTOR]);
  2386. end;
  2387. if size>0 then
  2388. begin
  2389. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  2390. include(current_procinfo.flags,pi_has_saved_regs);
  2391. { Copy registers to temp }
  2392. href:=current_procinfo.save_regs_ref;
  2393. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2394. begin
  2395. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2396. begin
  2397. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE),href);
  2398. inc(href.offset,sizeof(aint));
  2399. end;
  2400. include(rg[R_INTREGISTER].preserved_by_proc,regs_to_save_int[r]);
  2401. end;
  2402. if uses_registers(R_ADDRESSREGISTER) then
  2403. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  2404. begin
  2405. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2406. begin
  2407. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE),href);
  2408. inc(href.offset,sizeof(aint));
  2409. end;
  2410. include(rg[R_ADDRESSREGISTER].preserved_by_proc,regs_to_save_address[r]);
  2411. end;
  2412. if uses_registers(R_MMREGISTER) then
  2413. begin
  2414. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2415. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2416. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2417. begin
  2418. { the array has to be declared even if no MM registers are saved
  2419. (such as with SSE on i386), and since 0-element arrays don't
  2420. exist, they contain a single RS_INVALID element in that case
  2421. }
  2422. if regs_to_save_mm[r]<>RS_INVALID then
  2423. begin
  2424. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2425. begin
  2426. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,regs_to_save_mm[r],R_SUBMMWHOLE),href,nil);
  2427. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2428. end;
  2429. include(rg[R_MMREGISTER].preserved_by_proc,regs_to_save_mm[r]);
  2430. end;
  2431. end;
  2432. end;
  2433. end;
  2434. end;
  2435. procedure tcg.g_restore_registers(list:TAsmList);
  2436. var
  2437. href : treference;
  2438. r : integer;
  2439. hreg : tregister;
  2440. regs_to_save_int,
  2441. regs_to_save_address,
  2442. regs_to_save_mm : tcpuregisterarray;
  2443. begin
  2444. if not(pi_has_saved_regs in current_procinfo.flags) then
  2445. exit;
  2446. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2447. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  2448. regs_to_save_mm:=paramanager.get_saved_registers_mm(current_procinfo.procdef.proccalloption);
  2449. { Copy registers from temp }
  2450. href:=current_procinfo.save_regs_ref;
  2451. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2452. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2453. begin
  2454. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  2455. { Allocate register so the optimizer does not remove the load }
  2456. a_reg_alloc(list,hreg);
  2457. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2458. inc(href.offset,sizeof(aint));
  2459. end;
  2460. if uses_registers(R_ADDRESSREGISTER) then
  2461. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  2462. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2463. begin
  2464. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  2465. { Allocate register so the optimizer does not remove the load }
  2466. a_reg_alloc(list,hreg);
  2467. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2468. inc(href.offset,sizeof(aint));
  2469. end;
  2470. if uses_registers(R_MMREGISTER) then
  2471. begin
  2472. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2473. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2474. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2475. begin
  2476. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2477. begin
  2478. hreg:=newreg(R_MMREGISTER,regs_to_save_mm[r],R_SUBMMWHOLE);
  2479. { Allocate register so the optimizer does not remove the load }
  2480. a_reg_alloc(list,hreg);
  2481. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2482. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2483. end;
  2484. end;
  2485. end;
  2486. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2487. end;
  2488. procedure tcg.g_profilecode(list : TAsmList);
  2489. begin
  2490. end;
  2491. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2492. var
  2493. hsym : tsym;
  2494. href : treference;
  2495. paraloc : Pcgparalocation;
  2496. begin
  2497. { calculate the parameter info for the procdef }
  2498. procdef.init_paraloc_info(callerside);
  2499. hsym:=tsym(procdef.parast.Find('self'));
  2500. if not(assigned(hsym) and
  2501. (hsym.typ=paravarsym)) then
  2502. internalerror(200305251);
  2503. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2504. while paraloc<>nil do
  2505. with paraloc^ do
  2506. begin
  2507. case loc of
  2508. LOC_REGISTER:
  2509. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2510. LOC_REFERENCE:
  2511. begin
  2512. { offset in the wrapper needs to be adjusted for the stored
  2513. return address }
  2514. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  2515. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2516. end
  2517. else
  2518. internalerror(200309189);
  2519. end;
  2520. paraloc:=next;
  2521. end;
  2522. end;
  2523. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2524. begin
  2525. a_call_name(list,s,false);
  2526. end;
  2527. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2528. var
  2529. l: tasmsymbol;
  2530. ref: treference;
  2531. nlsymname: string;
  2532. symtyp: TAsmsymtype;
  2533. begin
  2534. result := NR_NO;
  2535. case target_info.system of
  2536. system_powerpc_darwin,
  2537. system_i386_darwin,
  2538. system_i386_iphonesim,
  2539. system_powerpc64_darwin,
  2540. system_arm_ios:
  2541. begin
  2542. nlsymname:='L'+symname+'$non_lazy_ptr';
  2543. l:=current_asmdata.getasmsymbol(nlsymname);
  2544. if not(assigned(l)) then
  2545. begin
  2546. if is_data in flags then
  2547. symtyp:=AT_DATA
  2548. else
  2549. symtyp:=AT_FUNCTION;
  2550. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2551. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA,voidpointertype);
  2552. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2553. if not(is_weak in flags) then
  2554. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname,symtyp).Name))
  2555. else
  2556. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname,symtyp).Name));
  2557. {$ifdef cpu64bitaddr}
  2558. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2559. {$else cpu64bitaddr}
  2560. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2561. {$endif cpu64bitaddr}
  2562. end;
  2563. result := getaddressregister(list);
  2564. reference_reset_symbol(ref,l,0,sizeof(pint),[]);
  2565. { a_load_ref_reg will turn this into a pic-load if needed }
  2566. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2567. end;
  2568. end;
  2569. end;
  2570. procedure tcg.g_maybe_got_init(list: TAsmList);
  2571. begin
  2572. end;
  2573. procedure tcg.g_call(list: TAsmList;const s: string);
  2574. begin
  2575. allocallcpuregisters(list);
  2576. a_call_name(list,s,false);
  2577. deallocallcpuregisters(list);
  2578. end;
  2579. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2580. begin
  2581. a_jmp_always(list,l);
  2582. end;
  2583. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2584. begin
  2585. internalerror(200807231);
  2586. end;
  2587. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2588. begin
  2589. internalerror(200807232);
  2590. end;
  2591. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2592. begin
  2593. internalerror(200807233);
  2594. end;
  2595. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2596. begin
  2597. internalerror(200807234);
  2598. end;
  2599. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2600. begin
  2601. Result:=TRegister(0);
  2602. internalerror(200807238);
  2603. end;
  2604. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  2605. begin
  2606. internalerror(2014070601);
  2607. end;
  2608. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2609. begin
  2610. internalerror(2014070602);
  2611. end;
  2612. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2613. begin
  2614. internalerror(2014060801);
  2615. end;
  2616. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2617. var
  2618. divreg: tregister;
  2619. magic: aInt;
  2620. u_magic: aWord;
  2621. u_shift: byte;
  2622. u_add: boolean;
  2623. begin
  2624. divreg:=getintregister(list,OS_INT);
  2625. if (size in [OS_S32,OS_S64]) then
  2626. begin
  2627. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2628. { load magic value }
  2629. a_load_const_reg(list,OS_INT,magic,divreg);
  2630. { multiply, discarding low bits }
  2631. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2632. { add/subtract numerator }
  2633. if (a>0) and (magic<0) then
  2634. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2635. else if (a<0) and (magic>0) then
  2636. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2637. { shift shift places to the right (arithmetic) }
  2638. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2639. { extract and add sign bit }
  2640. if (a>=0) then
  2641. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2642. else
  2643. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2644. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2645. end
  2646. else if (size in [OS_32,OS_64]) then
  2647. begin
  2648. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2649. { load magic in divreg }
  2650. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2651. { multiply, discarding low bits }
  2652. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2653. if (u_add) then
  2654. begin
  2655. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2656. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2657. { divreg=(numerator-result) }
  2658. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2659. { divreg=(numerator-result)/2 }
  2660. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2661. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2662. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2663. end
  2664. else
  2665. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2666. end
  2667. else
  2668. InternalError(2014060601);
  2669. end;
  2670. procedure tcg.g_check_for_fpu_exception(list: TAsmList;force,clear : boolean);
  2671. begin
  2672. { empty by default }
  2673. end;
  2674. procedure tcg.maybe_check_for_fpu_exception(list: TAsmList);
  2675. begin
  2676. current_procinfo.FPUExceptionCheckNeeded:=true;
  2677. g_check_for_fpu_exception(list,false,true);
  2678. end;
  2679. {*****************************************************************************
  2680. TCG64
  2681. *****************************************************************************}
  2682. {$ifndef cpu64bitalu}
  2683. function joinreg64(reglo,reghi : tregister) : tregister64;
  2684. begin
  2685. result.reglo:=reglo;
  2686. result.reghi:=reghi;
  2687. end;
  2688. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2689. begin
  2690. a_load64_reg_reg(list,regsrc,regdst);
  2691. a_op64_const_reg(list,op,size,value,regdst);
  2692. end;
  2693. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2694. var
  2695. tmpreg64 : tregister64;
  2696. begin
  2697. { when src1=dst then we need to first create a temp to prevent
  2698. overwriting src1 with src2 }
  2699. if (regsrc1.reghi=regdst.reghi) or
  2700. (regsrc1.reglo=regdst.reghi) or
  2701. (regsrc1.reghi=regdst.reglo) or
  2702. (regsrc1.reglo=regdst.reglo) then
  2703. begin
  2704. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2705. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2706. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2707. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2708. a_load64_reg_reg(list,tmpreg64,regdst);
  2709. end
  2710. else
  2711. begin
  2712. a_load64_reg_reg(list,regsrc2,regdst);
  2713. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2714. end;
  2715. end;
  2716. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2717. var
  2718. tmpreg64 : tregister64;
  2719. begin
  2720. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2721. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2722. a_load64_subsetref_reg(list,sref,tmpreg64);
  2723. a_op64_const_reg(list,op,size,a,tmpreg64);
  2724. a_load64_reg_subsetref(list,tmpreg64,sref);
  2725. end;
  2726. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2727. var
  2728. tmpreg64 : tregister64;
  2729. begin
  2730. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2731. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2732. a_load64_subsetref_reg(list,sref,tmpreg64);
  2733. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2734. a_load64_reg_subsetref(list,tmpreg64,sref);
  2735. end;
  2736. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2737. var
  2738. tmpreg64 : tregister64;
  2739. begin
  2740. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2741. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2742. a_load64_subsetref_reg(list,sref,tmpreg64);
  2743. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2744. a_load64_reg_subsetref(list,tmpreg64,sref);
  2745. end;
  2746. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2747. var
  2748. tmpreg64 : tregister64;
  2749. begin
  2750. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2751. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2752. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2753. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2754. end;
  2755. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2756. begin
  2757. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2758. ovloc.loc:=LOC_VOID;
  2759. end;
  2760. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2761. begin
  2762. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2763. ovloc.loc:=LOC_VOID;
  2764. end;
  2765. procedure tcg64.a_op64_reg(list: TAsmList; op: TOpCG; size: tcgsize; regdst: tregister64);
  2766. begin
  2767. if not (op in [OP_NOT,OP_NEG]) then
  2768. internalerror(2020050706);
  2769. a_op64_reg_reg(list,op,size,regdst,regdst);
  2770. end;
  2771. procedure tcg64.a_op64_ref(list: TAsmList; op: TOpCG; size: tcgsize; const ref: treference);
  2772. var
  2773. tempreg: tregister64;
  2774. begin
  2775. if not (op in [OP_NOT,OP_NEG]) then
  2776. internalerror(2020050706);
  2777. tempreg.reghi:=cg.getintregister(list,OS_32);
  2778. tempreg.reglo:=cg.getintregister(list,OS_32);
  2779. a_load64_ref_reg(list,ref,tempreg);
  2780. a_op64_reg_reg(list,op,size,tempreg,tempreg);
  2781. a_load64_reg_ref(list,tempreg,ref);
  2782. end;
  2783. procedure tcg64.a_op64_loc(list: TAsmList; op: TOpCG; size: tcgsize; const l: tlocation);
  2784. begin
  2785. case l.loc of
  2786. LOC_REFERENCE, LOC_CREFERENCE:
  2787. a_op64_ref(list,op,size,l.reference);
  2788. LOC_REGISTER,LOC_CREGISTER:
  2789. a_op64_reg(list,op,size,l.register64);
  2790. else
  2791. internalerror(2020050707);
  2792. end;
  2793. end;
  2794. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2795. begin
  2796. case l.loc of
  2797. LOC_REFERENCE, LOC_CREFERENCE:
  2798. a_load64_ref_subsetref(list,l.reference,sref);
  2799. LOC_REGISTER,LOC_CREGISTER:
  2800. a_load64_reg_subsetref(list,l.register64,sref);
  2801. LOC_CONSTANT :
  2802. a_load64_const_subsetref(list,l.value64,sref);
  2803. LOC_SUBSETREF,LOC_CSUBSETREF:
  2804. a_load64_subsetref_subsetref(list,l.sref,sref);
  2805. else
  2806. internalerror(2006082210);
  2807. end;
  2808. end;
  2809. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2810. begin
  2811. case l.loc of
  2812. LOC_REFERENCE, LOC_CREFERENCE:
  2813. a_load64_subsetref_ref(list,sref,l.reference);
  2814. LOC_REGISTER,LOC_CREGISTER:
  2815. a_load64_subsetref_reg(list,sref,l.register64);
  2816. LOC_SUBSETREF,LOC_CSUBSETREF:
  2817. a_load64_subsetref_subsetref(list,sref,l.sref);
  2818. else
  2819. internalerror(2006082211);
  2820. end;
  2821. end;
  2822. {$else cpu64bitalu}
  2823. function joinreg128(reglo, reghi: tregister): tregister128;
  2824. begin
  2825. result.reglo:=reglo;
  2826. result.reghi:=reghi;
  2827. end;
  2828. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2829. var
  2830. paraloclo,
  2831. paralochi : pcgparalocation;
  2832. begin
  2833. if not(cgpara.size in [OS_128,OS_S128]) then
  2834. internalerror(2012090604);
  2835. if not assigned(cgpara.location) then
  2836. internalerror(2012090605);
  2837. { init lo/hi para }
  2838. cgparahi.reset;
  2839. if cgpara.size=OS_S128 then
  2840. cgparahi.size:=OS_S64
  2841. else
  2842. cgparahi.size:=OS_64;
  2843. cgparahi.intsize:=8;
  2844. cgparahi.alignment:=cgpara.alignment;
  2845. paralochi:=cgparahi.add_location;
  2846. cgparalo.reset;
  2847. cgparalo.size:=OS_64;
  2848. cgparalo.intsize:=8;
  2849. cgparalo.alignment:=cgpara.alignment;
  2850. paraloclo:=cgparalo.add_location;
  2851. { 2 parameter fields? }
  2852. if assigned(cgpara.location^.next) then
  2853. begin
  2854. { Order for multiple locations is always
  2855. paraloc^ -> high
  2856. paraloc^.next -> low }
  2857. if (target_info.endian=ENDIAN_BIG) then
  2858. begin
  2859. { paraloc^ -> high
  2860. paraloc^.next -> low }
  2861. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2862. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2863. end
  2864. else
  2865. begin
  2866. { paraloc^ -> low
  2867. paraloc^.next -> high }
  2868. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2869. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2870. end;
  2871. end
  2872. else
  2873. begin
  2874. { single parameter, this can only be in memory }
  2875. if cgpara.location^.loc<>LOC_REFERENCE then
  2876. internalerror(2012090606);
  2877. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2878. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2879. { for big endian low is at +8, for little endian high }
  2880. if target_info.endian = endian_big then
  2881. begin
  2882. inc(cgparalo.location^.reference.offset,8);
  2883. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2884. end
  2885. else
  2886. begin
  2887. inc(cgparahi.location^.reference.offset,8);
  2888. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2889. end;
  2890. end;
  2891. { fix size }
  2892. paraloclo^.size:=cgparalo.size;
  2893. paraloclo^.next:=nil;
  2894. paralochi^.size:=cgparahi.size;
  2895. paralochi^.next:=nil;
  2896. end;
  2897. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2898. regdst: tregister128);
  2899. begin
  2900. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2901. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2902. end;
  2903. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2904. const ref: treference);
  2905. var
  2906. tmpreg: tregister;
  2907. tmpref: treference;
  2908. begin
  2909. if target_info.endian = endian_big then
  2910. begin
  2911. tmpreg:=reg.reglo;
  2912. reg.reglo:=reg.reghi;
  2913. reg.reghi:=tmpreg;
  2914. end;
  2915. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2916. tmpref := ref;
  2917. inc(tmpref.offset,8);
  2918. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2919. end;
  2920. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2921. reg: tregister128);
  2922. var
  2923. tmpreg: tregister;
  2924. tmpref: treference;
  2925. begin
  2926. if target_info.endian = endian_big then
  2927. begin
  2928. tmpreg := reg.reglo;
  2929. reg.reglo := reg.reghi;
  2930. reg.reghi := tmpreg;
  2931. end;
  2932. tmpref := ref;
  2933. if (tmpref.base=reg.reglo) then
  2934. begin
  2935. tmpreg:=cg.getaddressregister(list);
  2936. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2937. tmpref.base:=tmpreg;
  2938. end
  2939. else
  2940. { this works only for the i386, thus the i386 needs to override }
  2941. { this method and this method must be replaced by a more generic }
  2942. { implementation FK }
  2943. if (tmpref.index=reg.reglo) then
  2944. begin
  2945. tmpreg:=cg.getaddressregister(list);
  2946. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2947. tmpref.index:=tmpreg;
  2948. end;
  2949. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2950. inc(tmpref.offset,8);
  2951. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2952. end;
  2953. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2954. const ref: treference);
  2955. begin
  2956. case l.loc of
  2957. LOC_REGISTER,LOC_CREGISTER:
  2958. a_load128_reg_ref(list,l.register128,ref);
  2959. { not yet implemented:
  2960. LOC_CONSTANT :
  2961. a_load128_const_ref(list,l.value128,ref);
  2962. LOC_SUBSETREF, LOC_CSUBSETREF:
  2963. a_load64_subsetref_ref(list,l.sref,ref); }
  2964. else
  2965. internalerror(201209061);
  2966. end;
  2967. end;
  2968. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2969. const l: tlocation);
  2970. begin
  2971. case l.loc of
  2972. LOC_REFERENCE, LOC_CREFERENCE:
  2973. a_load128_reg_ref(list,reg,l.reference);
  2974. LOC_REGISTER,LOC_CREGISTER:
  2975. a_load128_reg_reg(list,reg,l.register128);
  2976. { not yet implemented:
  2977. LOC_SUBSETREF, LOC_CSUBSETREF:
  2978. a_load64_reg_subsetref(list,reg,l.sref);
  2979. LOC_MMREGISTER, LOC_CMMREGISTER:
  2980. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2981. else
  2982. internalerror(201209062);
  2983. end;
  2984. end;
  2985. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2986. valuehi: int64; reg: tregister128);
  2987. begin
  2988. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2989. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2990. end;
  2991. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2992. const paraloc: TCGPara);
  2993. begin
  2994. case l.loc of
  2995. LOC_REGISTER,
  2996. LOC_CREGISTER :
  2997. a_load128_reg_cgpara(list,l.register128,paraloc);
  2998. {not yet implemented:
  2999. LOC_CONSTANT :
  3000. a_load128_const_cgpara(list,l.value64,paraloc);
  3001. }
  3002. LOC_CREFERENCE,
  3003. LOC_REFERENCE :
  3004. a_load128_ref_cgpara(list,l.reference,paraloc);
  3005. else
  3006. internalerror(2012090603);
  3007. end;
  3008. end;
  3009. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  3010. var
  3011. tmplochi,tmploclo: tcgpara;
  3012. begin
  3013. tmploclo.init;
  3014. tmplochi.init;
  3015. splitparaloc128(paraloc,tmploclo,tmplochi);
  3016. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  3017. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  3018. tmploclo.done;
  3019. tmplochi.done;
  3020. end;
  3021. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  3022. var
  3023. tmprefhi,tmpreflo : treference;
  3024. tmploclo,tmplochi : tcgpara;
  3025. begin
  3026. tmploclo.init;
  3027. tmplochi.init;
  3028. splitparaloc128(paraloc,tmploclo,tmplochi);
  3029. tmprefhi:=r;
  3030. tmpreflo:=r;
  3031. if target_info.endian=endian_big then
  3032. inc(tmpreflo.offset,8)
  3033. else
  3034. inc(tmprefhi.offset,8);
  3035. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  3036. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  3037. tmploclo.done;
  3038. tmplochi.done;
  3039. end;
  3040. {$endif cpu64bitalu}
  3041. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  3042. begin
  3043. result:=[];
  3044. if sym.typ<>AT_FUNCTION then
  3045. include(result,is_data);
  3046. if sym.bind=AB_WEAK_EXTERNAL then
  3047. include(result,is_weak);
  3048. end;
  3049. procedure destroy_codegen;
  3050. begin
  3051. cg.free;
  3052. cg:=nil;
  3053. {$ifdef cpu64bitalu}
  3054. cg128.free;
  3055. cg128:=nil;
  3056. {$else cpu64bitalu}
  3057. cg64.free;
  3058. cg64:=nil;
  3059. {$endif cpu64bitalu}
  3060. end;
  3061. end.