cgobj.pas 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422
  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. reghasvalue: boolean;
  874. begin
  875. location:=cgpara.location;
  876. tmpref:=r;
  877. sizeleft:=cgpara.intsize;
  878. while assigned(location) do
  879. begin
  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_M128,
  995. OS_MS8..OS_MS128:
  996. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  997. else
  998. internalerror(2010053101);
  999. end;
  1000. end;
  1001. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1002. begin
  1003. a_loadfpu_ref_reg(list,size,location^.size,tmpref,location^.register);
  1004. end
  1005. else
  1006. internalerror(2010053111);
  1007. end;
  1008. inc(tmpref.offset,tcgsize2size[location^.size]);
  1009. dec(sizeleft,tcgsize2size[location^.size]);
  1010. location:=location^.next;
  1011. end;
  1012. end;
  1013. procedure tcg.a_load_ref_cgparalocref(list: TAsmList; sourcesize: tcgsize; sizeleft: tcgint; const ref, paralocref: treference; const cgpara: tcgpara; const location: PCGParaLocation);
  1014. begin
  1015. if assigned(location^.next) then
  1016. internalerror(2010052906);
  1017. if (sourcesize<>OS_NO) and
  1018. (tcgsize2size[sourcesize]<=sizeof(aint)) then
  1019. a_load_ref_ref(list,sourcesize,location^.size,ref,paralocref)
  1020. else
  1021. { use concatcopy, because the parameter can be larger than }
  1022. { what the OS_* constants can handle }
  1023. g_concatcopy(list,ref,paralocref,sizeleft);
  1024. end;
  1025. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  1026. begin
  1027. case l.loc of
  1028. LOC_REGISTER,
  1029. LOC_CREGISTER :
  1030. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  1031. LOC_CONSTANT :
  1032. a_load_const_cgpara(list,l.size,l.value,cgpara);
  1033. LOC_CREFERENCE,
  1034. LOC_REFERENCE :
  1035. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  1036. else
  1037. internalerror(2002032211);
  1038. end;
  1039. end;
  1040. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  1041. var
  1042. hr : tregister;
  1043. begin
  1044. cgpara.check_simple_location;
  1045. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1046. begin
  1047. paramanager.allocparaloc(list,cgpara.location);
  1048. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  1049. end
  1050. else
  1051. begin
  1052. hr:=getaddressregister(list);
  1053. a_loadaddr_ref_reg(list,r,hr);
  1054. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  1055. end;
  1056. end;
  1057. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  1058. var
  1059. href : treference;
  1060. hreg : tregister;
  1061. cgsize: tcgsize;
  1062. begin
  1063. case paraloc.loc of
  1064. LOC_REGISTER :
  1065. begin
  1066. hreg:=paraloc.register;
  1067. cgsize:=paraloc.size;
  1068. if paraloc.shiftval>0 then
  1069. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  1070. { in case the original size was 3 or 5/6/7 bytes, the value was
  1071. shifted to the top of the to 4 resp. 8 byte register on the
  1072. caller side and needs to be stored with those bytes at the
  1073. start of the reference -> don't shift right }
  1074. else if (paraloc.shiftval<0) and
  1075. ((-paraloc.shiftval) in [8,16,32]) then
  1076. begin
  1077. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1078. { convert to a register of 1/2/4 bytes in size, since the
  1079. original register had to be made larger to be able to hold
  1080. the shifted value }
  1081. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  1082. if cgsize=OS_NO then
  1083. cgsize:=OS_INT;
  1084. hreg:=getintregister(list,cgsize);
  1085. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  1086. end;
  1087. { use the exact size to avoid overwriting of adjacent data }
  1088. if tcgsize2size[cgsize]<=sizeleft then
  1089. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref)
  1090. else
  1091. case sizeleft of
  1092. 1,2,4,8:
  1093. a_load_reg_ref(list,paraloc.size,int_cgsize(sizeleft),hreg,ref);
  1094. 3:
  1095. begin
  1096. if target_info.endian=endian_big then
  1097. begin
  1098. href:=ref;
  1099. inc(href.offset,2);
  1100. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1101. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1102. a_load_reg_ref(list,paraloc.size,OS_16,hreg,ref);
  1103. end
  1104. else
  1105. begin
  1106. a_load_reg_ref(list,paraloc.size,OS_16,hreg,ref);
  1107. href:=ref;
  1108. inc(href.offset,2);
  1109. a_op_const_reg_reg(list,OP_SHR,cgsize,16,hreg,hreg);
  1110. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1111. end
  1112. end;
  1113. 5:
  1114. begin
  1115. if target_info.endian=endian_big then
  1116. begin
  1117. href:=ref;
  1118. inc(href.offset,4);
  1119. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1120. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1121. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1122. end
  1123. else
  1124. begin
  1125. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1126. href:=ref;
  1127. inc(href.offset,4);
  1128. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1129. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1130. end
  1131. end;
  1132. 6:
  1133. begin
  1134. if target_info.endian=endian_big then
  1135. begin
  1136. href:=ref;
  1137. inc(href.offset,4);
  1138. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1139. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,hreg,hreg);
  1140. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1141. end
  1142. else
  1143. begin
  1144. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1145. href:=ref;
  1146. inc(href.offset,4);
  1147. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1148. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1149. end
  1150. end;
  1151. 7:
  1152. begin
  1153. if target_info.endian=endian_big then
  1154. begin
  1155. href:=ref;
  1156. inc(href.offset,6);
  1157. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1158. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1159. href:=ref;
  1160. inc(href.offset,4);
  1161. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1162. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,hreg,hreg);
  1163. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1164. end
  1165. else
  1166. begin
  1167. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1168. href:=ref;
  1169. inc(href.offset,4);
  1170. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1171. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1172. inc(href.offset,2);
  1173. a_op_const_reg_reg(list,OP_SHR,cgsize,16,hreg,hreg);
  1174. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1175. end
  1176. end;
  1177. else
  1178. { other sizes not allowed }
  1179. Internalerror(2017080901);
  1180. end;
  1181. end;
  1182. LOC_MMREGISTER :
  1183. begin
  1184. case paraloc.size of
  1185. OS_F32,
  1186. OS_F64,
  1187. OS_F128:
  1188. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1189. OS_M8..OS_M128,
  1190. OS_MS8..OS_MS128:
  1191. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  1192. else
  1193. internalerror(2010053102);
  1194. end;
  1195. end;
  1196. LOC_FPUREGISTER :
  1197. a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1198. LOC_REFERENCE :
  1199. begin
  1200. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,ctempposinvalid,align,[]);
  1201. { use concatcopy, because it can also be a float which fails when
  1202. load_ref_ref is used. Don't copy data when the references are equal }
  1203. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1204. g_concatcopy(list,href,ref,sizeleft);
  1205. end;
  1206. else
  1207. internalerror(2002081302);
  1208. end;
  1209. end;
  1210. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  1211. var
  1212. href : treference;
  1213. begin
  1214. case paraloc.loc of
  1215. LOC_REGISTER :
  1216. begin
  1217. if paraloc.shiftval<0 then
  1218. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1219. case getregtype(reg) of
  1220. R_ADDRESSREGISTER,
  1221. R_INTREGISTER:
  1222. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1223. R_MMREGISTER:
  1224. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1225. R_FPUREGISTER:
  1226. a_loadfpu_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1227. else
  1228. internalerror(2009112422);
  1229. end;
  1230. end;
  1231. LOC_MMREGISTER :
  1232. begin
  1233. case getregtype(reg) of
  1234. R_ADDRESSREGISTER,
  1235. R_INTREGISTER:
  1236. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1237. R_MMREGISTER:
  1238. begin
  1239. case paraloc.size of
  1240. OS_F32,
  1241. OS_F64,
  1242. OS_F128:
  1243. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1244. OS_M8..OS_M128,
  1245. OS_MS8..OS_MS128:
  1246. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1247. else
  1248. internalerror(2010053102);
  1249. end;
  1250. end;
  1251. else
  1252. internalerror(2010053104);
  1253. end;
  1254. end;
  1255. LOC_FPUREGISTER :
  1256. begin
  1257. case getregtype(reg) of
  1258. R_FPUREGISTER:
  1259. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg)
  1260. else
  1261. internalerror(2015031401);
  1262. end;
  1263. end;
  1264. LOC_REFERENCE :
  1265. begin
  1266. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,ctempposinvalid,align,[]);
  1267. case getregtype(reg) of
  1268. R_ADDRESSREGISTER,
  1269. R_INTREGISTER :
  1270. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1271. R_FPUREGISTER :
  1272. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1273. R_MMREGISTER :
  1274. { not paraloc.size, because it may be OS_64 instead of
  1275. OS_F64 in case the parameter is passed using integer
  1276. conventions (e.g., on ARM) }
  1277. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1278. else
  1279. internalerror(2004101012);
  1280. end;
  1281. end;
  1282. else
  1283. internalerror(2002081302);
  1284. end;
  1285. end;
  1286. {****************************************************************************
  1287. some generic implementations
  1288. ****************************************************************************}
  1289. { memory/register loading }
  1290. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1291. var
  1292. tmpref : treference;
  1293. tmpreg : tregister;
  1294. i : longint;
  1295. begin
  1296. if ref.alignment<tcgsize2size[fromsize] then
  1297. begin
  1298. tmpref:=ref;
  1299. { we take care of the alignment now }
  1300. tmpref.alignment:=0;
  1301. case FromSize of
  1302. OS_16,OS_S16:
  1303. begin
  1304. tmpreg:=getintregister(list,OS_16);
  1305. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1306. if target_info.endian=endian_big then
  1307. inc(tmpref.offset);
  1308. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1309. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1310. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1311. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1312. if target_info.endian=endian_big then
  1313. dec(tmpref.offset)
  1314. else
  1315. inc(tmpref.offset);
  1316. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1317. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1318. end;
  1319. OS_32,OS_S32:
  1320. begin
  1321. { could add an optimised case for ref.alignment=2 }
  1322. tmpreg:=getintregister(list,OS_32);
  1323. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1324. if target_info.endian=endian_big then
  1325. inc(tmpref.offset,3);
  1326. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1327. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1328. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1329. for i:=1 to 3 do
  1330. begin
  1331. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1332. if target_info.endian=endian_big then
  1333. dec(tmpref.offset)
  1334. else
  1335. inc(tmpref.offset);
  1336. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1337. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1338. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1339. end;
  1340. end
  1341. else
  1342. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1343. end;
  1344. end
  1345. else
  1346. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1347. end;
  1348. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1349. var
  1350. tmpref : treference;
  1351. tmpreg,
  1352. tmpreg2 : tregister;
  1353. i : longint;
  1354. hisize : tcgsize;
  1355. begin
  1356. if ref.alignment in [1,2] then
  1357. begin
  1358. tmpref:=ref;
  1359. { we take care of the alignment now }
  1360. tmpref.alignment:=0;
  1361. case FromSize of
  1362. OS_16,OS_S16:
  1363. if ref.alignment=2 then
  1364. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1365. else
  1366. begin
  1367. if FromSize=OS_16 then
  1368. hisize:=OS_8
  1369. else
  1370. hisize:=OS_S8;
  1371. { first load in tmpreg, because the target register }
  1372. { may be used in ref as well }
  1373. if target_info.endian=endian_little then
  1374. inc(tmpref.offset);
  1375. tmpreg:=getintregister(list,OS_8);
  1376. a_load_ref_reg(list,hisize,hisize,tmpref,tmpreg);
  1377. tmpreg:=makeregsize(list,tmpreg,FromSize);
  1378. a_op_const_reg(list,OP_SHL,FromSize,8,tmpreg);
  1379. if target_info.endian=endian_little then
  1380. dec(tmpref.offset)
  1381. else
  1382. inc(tmpref.offset);
  1383. tmpreg2:=makeregsize(list,register,OS_16);
  1384. a_load_ref_reg(list,OS_8,OS_16,tmpref,tmpreg2);
  1385. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,tmpreg2);
  1386. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1387. end;
  1388. OS_32,OS_S32:
  1389. if ref.alignment=2 then
  1390. begin
  1391. if target_info.endian=endian_little then
  1392. inc(tmpref.offset,2);
  1393. tmpreg:=getintregister(list,OS_32);
  1394. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1395. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1396. if target_info.endian=endian_little then
  1397. dec(tmpref.offset,2)
  1398. else
  1399. inc(tmpref.offset,2);
  1400. tmpreg2:=makeregsize(list,register,OS_32);
  1401. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg2);
  1402. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,tmpreg2);
  1403. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1404. end
  1405. else
  1406. begin
  1407. if target_info.endian=endian_little then
  1408. inc(tmpref.offset,3);
  1409. tmpreg:=getintregister(list,OS_32);
  1410. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1411. tmpreg2:=getintregister(list,OS_32);
  1412. for i:=1 to 3 do
  1413. begin
  1414. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1415. if target_info.endian=endian_little then
  1416. dec(tmpref.offset)
  1417. else
  1418. inc(tmpref.offset);
  1419. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1420. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1421. end;
  1422. a_load_reg_reg(list,fromsize,tosize,tmpreg,register);
  1423. end
  1424. else
  1425. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1426. end;
  1427. end
  1428. else
  1429. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1430. end;
  1431. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1432. var
  1433. tmpreg: tregister;
  1434. begin
  1435. { verify if we have the same reference }
  1436. if references_equal(sref,dref) then
  1437. exit;
  1438. tmpreg:=getintregister(list,tosize);
  1439. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1440. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1441. end;
  1442. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1443. var
  1444. tmpreg: tregister;
  1445. begin
  1446. tmpreg:=getintregister(list,size);
  1447. a_load_const_reg(list,size,a,tmpreg);
  1448. a_load_reg_ref(list,size,size,tmpreg,ref);
  1449. end;
  1450. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1451. begin
  1452. case loc.loc of
  1453. LOC_REFERENCE,LOC_CREFERENCE:
  1454. a_load_const_ref(list,loc.size,a,loc.reference);
  1455. LOC_REGISTER,LOC_CREGISTER:
  1456. a_load_const_reg(list,loc.size,a,loc.register);
  1457. else
  1458. internalerror(200203272);
  1459. end;
  1460. end;
  1461. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1462. begin
  1463. case loc.loc of
  1464. LOC_REFERENCE,LOC_CREFERENCE:
  1465. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1466. LOC_REGISTER,LOC_CREGISTER:
  1467. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1468. LOC_MMREGISTER,LOC_CMMREGISTER:
  1469. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1470. else
  1471. internalerror(200203271);
  1472. end;
  1473. end;
  1474. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1475. begin
  1476. case loc.loc of
  1477. LOC_REFERENCE,LOC_CREFERENCE:
  1478. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1479. LOC_REGISTER,LOC_CREGISTER:
  1480. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1481. LOC_CONSTANT:
  1482. a_load_const_reg(list,tosize,loc.value,reg);
  1483. LOC_MMREGISTER,LOC_CMMREGISTER:
  1484. a_loadmm_reg_intreg(list,loc.size,tosize,loc.register,reg,mms_movescalar);
  1485. else
  1486. internalerror(200109092);
  1487. end;
  1488. end;
  1489. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1490. begin
  1491. case loc.loc of
  1492. LOC_REFERENCE,LOC_CREFERENCE:
  1493. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1494. LOC_REGISTER,LOC_CREGISTER:
  1495. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1496. LOC_CONSTANT:
  1497. a_load_const_ref(list,tosize,loc.value,ref);
  1498. else
  1499. internalerror(200109302);
  1500. end;
  1501. end;
  1502. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1503. var
  1504. powerval : longint;
  1505. signext_a, zeroext_a: tcgint;
  1506. begin
  1507. case size of
  1508. OS_64,OS_S64:
  1509. begin
  1510. signext_a:=int64(a);
  1511. zeroext_a:=int64(a);
  1512. end;
  1513. OS_32,OS_S32:
  1514. begin
  1515. signext_a:=longint(a);
  1516. zeroext_a:=dword(a);
  1517. end;
  1518. OS_16,OS_S16:
  1519. begin
  1520. signext_a:=smallint(a);
  1521. zeroext_a:=word(a);
  1522. end;
  1523. OS_8,OS_S8:
  1524. begin
  1525. signext_a:=shortint(a);
  1526. zeroext_a:=byte(a);
  1527. end
  1528. else
  1529. begin
  1530. { Should we internalerror() here instead? }
  1531. signext_a:=a;
  1532. zeroext_a:=a;
  1533. end;
  1534. end;
  1535. case op of
  1536. OP_OR :
  1537. begin
  1538. { or with zero returns same result }
  1539. if a = 0 then
  1540. op:=OP_NONE
  1541. else
  1542. { or with max returns max }
  1543. if signext_a = -1 then
  1544. op:=OP_MOVE;
  1545. end;
  1546. OP_AND :
  1547. begin
  1548. { and with max returns same result }
  1549. if (signext_a = -1) then
  1550. op:=OP_NONE
  1551. else
  1552. { and with 0 returns 0 }
  1553. if a=0 then
  1554. op:=OP_MOVE;
  1555. end;
  1556. OP_XOR :
  1557. begin
  1558. { xor with zero returns same result }
  1559. if a = 0 then
  1560. op:=OP_NONE;
  1561. end;
  1562. OP_DIV :
  1563. begin
  1564. { division by 1 returns result }
  1565. if a = 1 then
  1566. op:=OP_NONE
  1567. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1568. begin
  1569. a := powerval;
  1570. op:= OP_SHR;
  1571. end;
  1572. end;
  1573. OP_IDIV:
  1574. begin
  1575. if a = 1 then
  1576. op:=OP_NONE;
  1577. end;
  1578. OP_MUL,OP_IMUL:
  1579. begin
  1580. if a = 1 then
  1581. op:=OP_NONE
  1582. else
  1583. if a=0 then
  1584. op:=OP_MOVE
  1585. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1586. begin
  1587. a := powerval;
  1588. op:= OP_SHL;
  1589. end;
  1590. end;
  1591. OP_ADD,OP_SUB:
  1592. begin
  1593. if a = 0 then
  1594. op:=OP_NONE;
  1595. end;
  1596. OP_SAR,OP_SHL,OP_SHR:
  1597. begin
  1598. if a = 0 then
  1599. op:=OP_NONE;
  1600. end;
  1601. OP_ROL,OP_ROR:
  1602. begin
  1603. case size of
  1604. OS_64,OS_S64:
  1605. a:=a and 63;
  1606. OS_32,OS_S32:
  1607. a:=a and 31;
  1608. OS_16,OS_S16:
  1609. a:=a and 15;
  1610. OS_8,OS_S8:
  1611. a:=a and 7;
  1612. end;
  1613. if a = 0 then
  1614. op:=OP_NONE;
  1615. end;
  1616. end;
  1617. end;
  1618. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1619. begin
  1620. case loc.loc of
  1621. LOC_REFERENCE, LOC_CREFERENCE:
  1622. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1623. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1624. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1625. else
  1626. internalerror(200203301);
  1627. end;
  1628. end;
  1629. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1630. begin
  1631. case loc.loc of
  1632. LOC_REFERENCE, LOC_CREFERENCE:
  1633. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1634. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1635. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1636. else
  1637. internalerror(48991);
  1638. end;
  1639. end;
  1640. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1641. var
  1642. reg: tregister;
  1643. regsize: tcgsize;
  1644. begin
  1645. if (fromsize>=tosize) then
  1646. regsize:=fromsize
  1647. else
  1648. regsize:=tosize;
  1649. reg:=getfpuregister(list,regsize);
  1650. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1651. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1652. end;
  1653. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1654. var
  1655. ref : treference;
  1656. begin
  1657. paramanager.alloccgpara(list,cgpara);
  1658. case cgpara.location^.loc of
  1659. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1660. begin
  1661. cgpara.check_simple_location;
  1662. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1663. end;
  1664. LOC_REFERENCE,LOC_CREFERENCE:
  1665. begin
  1666. cgpara.check_simple_location;
  1667. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1668. a_loadfpu_reg_ref(list,size,size,r,ref);
  1669. end;
  1670. LOC_REGISTER,LOC_CREGISTER:
  1671. begin
  1672. { paramfpu_ref does the check_simpe_location check here if necessary }
  1673. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1674. a_loadfpu_reg_ref(list,size,size,r,ref);
  1675. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1676. tg.Ungettemp(list,ref);
  1677. end;
  1678. else
  1679. internalerror(2010053112);
  1680. end;
  1681. end;
  1682. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1683. var
  1684. href : treference;
  1685. hsize: tcgsize;
  1686. paraloc: PCGParaLocation;
  1687. begin
  1688. case cgpara.location^.loc of
  1689. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1690. begin
  1691. paramanager.alloccgpara(list,cgpara);
  1692. paraloc:=cgpara.location;
  1693. href:=ref;
  1694. while assigned(paraloc) do
  1695. begin
  1696. if not(paraloc^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  1697. internalerror(2015031501);
  1698. a_loadfpu_ref_reg(list,paraloc^.size,paraloc^.size,href,paraloc^.register);
  1699. inc(href.offset,tcgsize2size[paraloc^.size]);
  1700. paraloc:=paraloc^.next;
  1701. end;
  1702. end;
  1703. LOC_REFERENCE,LOC_CREFERENCE:
  1704. begin
  1705. cgpara.check_simple_location;
  1706. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1707. { concatcopy should choose the best way to copy the data }
  1708. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1709. end;
  1710. LOC_REGISTER,LOC_CREGISTER:
  1711. begin
  1712. { force integer size }
  1713. hsize:=int_cgsize(tcgsize2size[size]);
  1714. {$ifndef cpu64bitalu}
  1715. if (hsize in [OS_S64,OS_64]) then
  1716. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1717. else
  1718. {$endif not cpu64bitalu}
  1719. begin
  1720. cgpara.check_simple_location;
  1721. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1722. end;
  1723. end
  1724. else
  1725. internalerror(200402201);
  1726. end;
  1727. end;
  1728. procedure tcg.a_loadfpu_intreg_reg(list : TAsmList; fromsize,tosize : tcgsize; intreg,fpureg : tregister);
  1729. var
  1730. tmpref: treference;
  1731. begin
  1732. if not(tcgsize2size[fromsize] in [4,8]) or
  1733. not(tcgsize2size[tosize] in [4,8]) or
  1734. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  1735. internalerror(2017070902);
  1736. tg.gettemp(list,tcgsize2size[fromsize],tcgsize2size[fromsize],tt_normal,tmpref);
  1737. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1738. a_loadfpu_ref_reg(list,tosize,tosize,tmpref,fpureg);
  1739. tg.ungettemp(list,tmpref);
  1740. end;
  1741. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1742. var
  1743. tmpreg : tregister;
  1744. begin
  1745. if assigned(ref.symbol)
  1746. { for avrtiny, the code generator generates a ref which is Z relative and while using it,
  1747. Z is changed, so the following code breaks }
  1748. {$ifdef avr}and not(CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]){$endif avr} then
  1749. begin
  1750. tmpreg:=getaddressregister(list);
  1751. a_loadaddr_ref_reg(list,ref,tmpreg);
  1752. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1753. end
  1754. else
  1755. tmpref:=ref;
  1756. tmpreg:=getintregister(list,size);
  1757. a_load_ref_reg(list,size,size,ref,tmpreg);
  1758. a_op_const_reg(list,op,size,a,tmpreg);
  1759. a_load_reg_ref(list,size,size,tmpreg,ref);
  1760. end;
  1761. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1762. begin
  1763. case loc.loc of
  1764. LOC_REGISTER, LOC_CREGISTER:
  1765. a_op_const_reg(list,op,loc.size,a,loc.register);
  1766. LOC_REFERENCE, LOC_CREFERENCE:
  1767. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1768. else
  1769. internalerror(200109061);
  1770. end;
  1771. end;
  1772. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1773. var
  1774. tmpreg : tregister;
  1775. begin
  1776. if assigned(ref.symbol)
  1777. { for avrtiny, the code generator generates a ref which is Z relative and while using it,
  1778. Z is changed, so the following code breaks }
  1779. {$ifdef avr}and not(CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]){$endif avr} then
  1780. begin
  1781. tmpreg:=getaddressregister(list);
  1782. a_loadaddr_ref_reg(list,ref,tmpreg);
  1783. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1784. end
  1785. else
  1786. tmpref:=ref;
  1787. if op in [OP_NEG,OP_NOT] then
  1788. begin
  1789. tmpreg:=getintregister(list,size);
  1790. a_op_reg_reg(list,op,size,reg,tmpreg);
  1791. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1792. end
  1793. else
  1794. begin
  1795. tmpreg:=getintregister(list,size);
  1796. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  1797. a_op_reg_reg(list,op,size,reg,tmpreg);
  1798. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1799. end;
  1800. end;
  1801. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1802. var
  1803. tmpreg: tregister;
  1804. begin
  1805. case op of
  1806. OP_NOT,OP_NEG:
  1807. { handle it as "load ref,reg; op reg" }
  1808. begin
  1809. a_load_ref_reg(list,size,size,ref,reg);
  1810. a_op_reg_reg(list,op,size,reg,reg);
  1811. end;
  1812. else
  1813. begin
  1814. tmpreg:=getintregister(list,size);
  1815. a_load_ref_reg(list,size,size,ref,tmpreg);
  1816. a_op_reg_reg(list,op,size,tmpreg,reg);
  1817. end;
  1818. end;
  1819. end;
  1820. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1821. begin
  1822. case loc.loc of
  1823. LOC_REGISTER, LOC_CREGISTER:
  1824. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1825. LOC_REFERENCE, LOC_CREFERENCE:
  1826. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1827. else
  1828. internalerror(200109061);
  1829. end;
  1830. end;
  1831. procedure tcg.a_op_loc_reg(list : TAsmList; Op : TOpCG; size: TCGSize; const loc : tlocation; reg : tregister);
  1832. begin
  1833. case loc.loc of
  1834. LOC_REGISTER, LOC_CREGISTER:
  1835. a_op_reg_reg(list,op,size,loc.register,reg);
  1836. LOC_REFERENCE, LOC_CREFERENCE:
  1837. a_op_ref_reg(list,op,size,loc.reference,reg);
  1838. LOC_CONSTANT:
  1839. a_op_const_reg(list,op,size,loc.value,reg);
  1840. else
  1841. internalerror(2018031101);
  1842. end;
  1843. end;
  1844. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1845. var
  1846. tmpreg: tregister;
  1847. begin
  1848. case loc.loc of
  1849. LOC_REGISTER,LOC_CREGISTER:
  1850. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1851. LOC_REFERENCE,LOC_CREFERENCE:
  1852. begin
  1853. tmpreg:=getintregister(list,loc.size);
  1854. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1855. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1856. end;
  1857. else
  1858. internalerror(200109061);
  1859. end;
  1860. end;
  1861. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1862. a:tcgint;src,dst:Tregister);
  1863. begin
  1864. optimize_op_const(size, op, a);
  1865. case op of
  1866. OP_NONE:
  1867. begin
  1868. if src <> dst then
  1869. a_load_reg_reg(list, size, size, src, dst);
  1870. exit;
  1871. end;
  1872. OP_MOVE:
  1873. begin
  1874. a_load_const_reg(list, size, a, dst);
  1875. exit;
  1876. end;
  1877. {$ifdef cpu8bitalu}
  1878. OP_SHL:
  1879. begin
  1880. if a=8 then
  1881. case size of
  1882. OS_S16,OS_16:
  1883. begin
  1884. a_load_reg_reg(list,OS_8,OS_8,src,GetNextReg(dst));
  1885. a_load_const_reg(list,OS_8,0,dst);
  1886. exit;
  1887. end;
  1888. end;
  1889. end;
  1890. OP_SHR:
  1891. begin
  1892. if a=8 then
  1893. case size of
  1894. OS_S16,OS_16:
  1895. begin
  1896. a_load_reg_reg(list,OS_8,OS_8,GetNextReg(src),dst);
  1897. a_load_const_reg(list,OS_8,0,GetNextReg(dst));
  1898. exit;
  1899. end;
  1900. end;
  1901. end;
  1902. {$endif cpu8bitalu}
  1903. {$ifdef cpu16bitalu}
  1904. OP_SHL:
  1905. begin
  1906. if a=16 then
  1907. case size of
  1908. OS_S32,OS_32:
  1909. begin
  1910. a_load_reg_reg(list,OS_16,OS_16,src,GetNextReg(dst));
  1911. a_load_const_reg(list,OS_16,0,dst);
  1912. exit;
  1913. end;
  1914. end;
  1915. end;
  1916. OP_SHR:
  1917. begin
  1918. if a=16 then
  1919. case size of
  1920. OS_S32,OS_32:
  1921. begin
  1922. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(src),dst);
  1923. a_load_const_reg(list,OS_16,0,GetNextReg(dst));
  1924. exit;
  1925. end;
  1926. end;
  1927. end;
  1928. {$endif cpu16bitalu}
  1929. end;
  1930. a_load_reg_reg(list,size,size,src,dst);
  1931. a_op_const_reg(list,op,size,a,dst);
  1932. end;
  1933. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1934. size: tcgsize; src1, src2, dst: tregister);
  1935. var
  1936. tmpreg: tregister;
  1937. begin
  1938. if (dst<>src1) then
  1939. begin
  1940. a_load_reg_reg(list,size,size,src2,dst);
  1941. a_op_reg_reg(list,op,size,src1,dst);
  1942. end
  1943. else
  1944. begin
  1945. { can we do a direct operation on the target register ? }
  1946. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1947. a_op_reg_reg(list,op,size,src2,dst)
  1948. else
  1949. begin
  1950. tmpreg:=getintregister(list,size);
  1951. a_load_reg_reg(list,size,size,src2,tmpreg);
  1952. a_op_reg_reg(list,op,size,src1,tmpreg);
  1953. a_load_reg_reg(list,size,size,tmpreg,dst);
  1954. end;
  1955. end;
  1956. end;
  1957. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1958. begin
  1959. a_op_const_reg_reg(list,op,size,a,src,dst);
  1960. ovloc.loc:=LOC_VOID;
  1961. end;
  1962. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1963. begin
  1964. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1965. ovloc.loc:=LOC_VOID;
  1966. end;
  1967. procedure tcg.a_op_reg(list: TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister);
  1968. begin
  1969. if not (Op in [OP_NOT,OP_NEG]) then
  1970. internalerror(2020050701);
  1971. a_op_reg_reg(list,op,size,reg,reg);
  1972. end;
  1973. procedure tcg.a_op_ref(list: TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference);
  1974. var
  1975. tmpreg: TRegister;
  1976. tmpref: treference;
  1977. begin
  1978. if not (Op in [OP_NOT,OP_NEG]) then
  1979. internalerror(2020050701);
  1980. if assigned(ref.symbol) then
  1981. begin
  1982. tmpreg:=getaddressregister(list);
  1983. a_loadaddr_ref_reg(list,ref,tmpreg);
  1984. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1985. end
  1986. else
  1987. tmpref:=ref;
  1988. tmpreg:=getintregister(list,size);
  1989. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  1990. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  1991. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1992. end;
  1993. procedure tcg.a_op_loc(list: TAsmList; Op: TOpCG; const loc: tlocation);
  1994. begin
  1995. case loc.loc of
  1996. LOC_REGISTER, LOC_CREGISTER:
  1997. a_op_reg(list,op,loc.size,loc.register);
  1998. LOC_REFERENCE, LOC_CREFERENCE:
  1999. a_op_ref(list,op,loc.size,loc.reference);
  2000. else
  2001. internalerror(2020050702);
  2002. end;
  2003. end;
  2004. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  2005. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  2006. var
  2007. tmpreg: tregister;
  2008. begin
  2009. tmpreg:=getintregister(list,size);
  2010. a_load_const_reg(list,size,a,tmpreg);
  2011. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2012. end;
  2013. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2014. l : tasmlabel);
  2015. var
  2016. tmpreg: tregister;
  2017. begin
  2018. tmpreg:=getintregister(list,size);
  2019. a_load_ref_reg(list,size,size,ref,tmpreg);
  2020. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2021. end;
  2022. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  2023. l : tasmlabel);
  2024. begin
  2025. case loc.loc of
  2026. LOC_REGISTER,LOC_CREGISTER:
  2027. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2028. LOC_REFERENCE,LOC_CREFERENCE:
  2029. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2030. else
  2031. internalerror(200109061);
  2032. end;
  2033. end;
  2034. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  2035. var
  2036. tmpreg: tregister;
  2037. begin
  2038. tmpreg:=getintregister(list,size);
  2039. a_load_ref_reg(list,size,size,ref,tmpreg);
  2040. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2041. end;
  2042. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  2043. var
  2044. tmpreg: tregister;
  2045. begin
  2046. tmpreg:=getintregister(list,size);
  2047. a_load_ref_reg(list,size,size,ref,tmpreg);
  2048. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2049. end;
  2050. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  2051. begin
  2052. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2053. end;
  2054. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  2055. begin
  2056. case loc.loc of
  2057. LOC_REGISTER,
  2058. LOC_CREGISTER:
  2059. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2060. LOC_REFERENCE,
  2061. LOC_CREFERENCE :
  2062. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2063. LOC_CONSTANT:
  2064. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2065. else
  2066. internalerror(200203231);
  2067. end;
  2068. end;
  2069. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  2070. l : tasmlabel);
  2071. var
  2072. tmpreg: tregister;
  2073. begin
  2074. case loc.loc of
  2075. LOC_REGISTER,LOC_CREGISTER:
  2076. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2077. LOC_REFERENCE,LOC_CREFERENCE:
  2078. begin
  2079. tmpreg:=getintregister(list,size);
  2080. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2081. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2082. end;
  2083. else
  2084. internalerror(200109061);
  2085. end;
  2086. end;
  2087. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  2088. begin
  2089. case loc.loc of
  2090. LOC_MMREGISTER,LOC_CMMREGISTER:
  2091. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2092. LOC_REFERENCE,LOC_CREFERENCE:
  2093. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  2094. LOC_REGISTER,LOC_CREGISTER:
  2095. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2096. else
  2097. internalerror(200310121);
  2098. end;
  2099. end;
  2100. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  2101. begin
  2102. case loc.loc of
  2103. LOC_MMREGISTER,LOC_CMMREGISTER:
  2104. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  2105. LOC_REFERENCE,LOC_CREFERENCE:
  2106. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  2107. else
  2108. internalerror(200310122);
  2109. end;
  2110. end;
  2111. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  2112. var
  2113. href : treference;
  2114. {$ifndef cpu64bitalu}
  2115. tmpreg : tregister;
  2116. reg64 : tregister64;
  2117. {$endif not cpu64bitalu}
  2118. begin
  2119. {$ifndef cpu64bitalu}
  2120. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  2121. (size<>OS_F64) then
  2122. {$endif not cpu64bitalu}
  2123. cgpara.check_simple_location;
  2124. paramanager.alloccgpara(list,cgpara);
  2125. case cgpara.location^.loc of
  2126. LOC_MMREGISTER,LOC_CMMREGISTER:
  2127. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  2128. LOC_REFERENCE,LOC_CREFERENCE:
  2129. begin
  2130. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  2131. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  2132. end;
  2133. LOC_REGISTER,LOC_CREGISTER:
  2134. begin
  2135. if assigned(shuffle) and
  2136. not shufflescalar(shuffle) then
  2137. internalerror(2009112510);
  2138. {$ifndef cpu64bitalu}
  2139. if (size=OS_F64) then
  2140. begin
  2141. if not assigned(cgpara.location^.next) or
  2142. assigned(cgpara.location^.next^.next) then
  2143. internalerror(2009112512);
  2144. case cgpara.location^.next^.loc of
  2145. LOC_REGISTER,LOC_CREGISTER:
  2146. tmpreg:=cgpara.location^.next^.register;
  2147. LOC_REFERENCE,LOC_CREFERENCE:
  2148. tmpreg:=getintregister(list,OS_32);
  2149. else
  2150. internalerror(2009112910);
  2151. end;
  2152. if (target_info.endian=ENDIAN_BIG) then
  2153. begin
  2154. { paraloc^ -> high
  2155. paraloc^.next -> low }
  2156. reg64.reghi:=cgpara.location^.register;
  2157. reg64.reglo:=tmpreg;
  2158. end
  2159. else
  2160. begin
  2161. { paraloc^ -> low
  2162. paraloc^.next -> high }
  2163. reg64.reglo:=cgpara.location^.register;
  2164. reg64.reghi:=tmpreg;
  2165. end;
  2166. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  2167. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2168. begin
  2169. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  2170. internalerror(2009112911);
  2171. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  2172. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  2173. end;
  2174. end
  2175. else
  2176. {$endif not cpu64bitalu}
  2177. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  2178. end
  2179. else
  2180. internalerror(200310123);
  2181. end;
  2182. end;
  2183. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  2184. var
  2185. hr : tregister;
  2186. hs : tmmshuffle;
  2187. begin
  2188. cgpara.check_simple_location;
  2189. hr:=getmmregister(list,cgpara.location^.size);
  2190. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  2191. if realshuffle(shuffle) then
  2192. begin
  2193. hs:=shuffle^;
  2194. removeshuffles(hs);
  2195. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  2196. end
  2197. else
  2198. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  2199. end;
  2200. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  2201. begin
  2202. case loc.loc of
  2203. LOC_MMREGISTER,LOC_CMMREGISTER:
  2204. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  2205. LOC_REFERENCE,LOC_CREFERENCE:
  2206. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  2207. else
  2208. internalerror(200310123);
  2209. end;
  2210. end;
  2211. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2212. var
  2213. hr : tregister;
  2214. hs : tmmshuffle;
  2215. begin
  2216. hr:=getmmregister(list,size);
  2217. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2218. if realshuffle(shuffle) then
  2219. begin
  2220. hs:=shuffle^;
  2221. removeshuffles(hs);
  2222. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2223. end
  2224. else
  2225. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2226. end;
  2227. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2228. var
  2229. hr : tregister;
  2230. hs : tmmshuffle;
  2231. begin
  2232. hr:=getmmregister(list,size);
  2233. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2234. if realshuffle(shuffle) then
  2235. begin
  2236. hs:=shuffle^;
  2237. removeshuffles(hs);
  2238. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2239. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2240. end
  2241. else
  2242. begin
  2243. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2244. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2245. end;
  2246. end;
  2247. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  2248. var
  2249. tmpref: treference;
  2250. begin
  2251. if (tcgsize2size[fromsize]<>4) or
  2252. (tcgsize2size[tosize]<>4) then
  2253. internalerror(2009112503);
  2254. tg.gettemp(list,4,4,tt_normal,tmpref);
  2255. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  2256. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  2257. tg.ungettemp(list,tmpref);
  2258. end;
  2259. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  2260. var
  2261. tmpref: treference;
  2262. begin
  2263. if (tcgsize2size[fromsize]<>4) or
  2264. (tcgsize2size[tosize]<>4) then
  2265. internalerror(2009112504);
  2266. tg.gettemp(list,8,8,tt_normal,tmpref);
  2267. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  2268. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  2269. tg.ungettemp(list,tmpref);
  2270. end;
  2271. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2272. begin
  2273. case loc.loc of
  2274. LOC_CMMREGISTER,LOC_MMREGISTER:
  2275. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2276. LOC_CREFERENCE,LOC_REFERENCE:
  2277. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2278. else
  2279. internalerror(200312232);
  2280. end;
  2281. end;
  2282. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  2283. begin
  2284. case loc.loc of
  2285. LOC_CMMREGISTER,LOC_MMREGISTER:
  2286. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  2287. LOC_CREFERENCE,LOC_REFERENCE:
  2288. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  2289. else
  2290. internalerror(200312232);
  2291. end;
  2292. end;
  2293. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2294. src1,src2,dst : tregister;shuffle : pmmshuffle);
  2295. begin
  2296. internalerror(2013061102);
  2297. end;
  2298. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2299. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  2300. begin
  2301. internalerror(2013061101);
  2302. end;
  2303. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2304. begin
  2305. g_concatcopy(list,source,dest,len);
  2306. end;
  2307. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2308. begin
  2309. g_overflowCheck(list,loc,def);
  2310. end;
  2311. {$ifdef cpuflags}
  2312. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  2313. var
  2314. tmpreg : tregister;
  2315. begin
  2316. tmpreg:=getintregister(list,size);
  2317. g_flags2reg(list,size,f,tmpreg);
  2318. a_load_reg_ref(list,size,size,tmpreg,ref);
  2319. end;
  2320. {$endif cpuflags}
  2321. procedure tcg.g_check_for_fpu_exception(list: TAsmList);
  2322. begin
  2323. { empty by default }
  2324. end;
  2325. {*****************************************************************************
  2326. Entry/Exit Code Functions
  2327. *****************************************************************************}
  2328. procedure tcg.g_save_registers(list:TAsmList);
  2329. var
  2330. href : treference;
  2331. size : longint;
  2332. r : integer;
  2333. regs_to_save_int,
  2334. regs_to_save_address,
  2335. regs_to_save_mm : tcpuregisterarray;
  2336. begin
  2337. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2338. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  2339. regs_to_save_mm:=paramanager.get_saved_registers_mm(current_procinfo.procdef.proccalloption);
  2340. { calculate temp. size }
  2341. size:=0;
  2342. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2343. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2344. inc(size,sizeof(aint));
  2345. if uses_registers(R_ADDRESSREGISTER) then
  2346. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2347. if regs_to_save_int[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2348. inc(size,sizeof(aint));
  2349. { mm registers }
  2350. if uses_registers(R_MMREGISTER) then
  2351. begin
  2352. { Make sure we reserve enough space to do the alignment based on the offset
  2353. later on. We can't use the size for this, because the alignment of the start
  2354. of the temp is smaller than needed for an OS_VECTOR }
  2355. inc(size,tcgsize2size[OS_VECTOR]);
  2356. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2357. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2358. inc(size,tcgsize2size[OS_VECTOR]);
  2359. end;
  2360. if size>0 then
  2361. begin
  2362. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  2363. include(current_procinfo.flags,pi_has_saved_regs);
  2364. { Copy registers to temp }
  2365. href:=current_procinfo.save_regs_ref;
  2366. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2367. begin
  2368. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2369. begin
  2370. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE),href);
  2371. inc(href.offset,sizeof(aint));
  2372. end;
  2373. include(rg[R_INTREGISTER].preserved_by_proc,regs_to_save_int[r]);
  2374. end;
  2375. if uses_registers(R_ADDRESSREGISTER) then
  2376. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  2377. begin
  2378. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2379. begin
  2380. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE),href);
  2381. inc(href.offset,sizeof(aint));
  2382. end;
  2383. include(rg[R_ADDRESSREGISTER].preserved_by_proc,regs_to_save_address[r]);
  2384. end;
  2385. if uses_registers(R_MMREGISTER) then
  2386. begin
  2387. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2388. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2389. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2390. begin
  2391. { the array has to be declared even if no MM registers are saved
  2392. (such as with SSE on i386), and since 0-element arrays don't
  2393. exist, they contain a single RS_INVALID element in that case
  2394. }
  2395. if regs_to_save_mm[r]<>RS_INVALID then
  2396. begin
  2397. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2398. begin
  2399. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,regs_to_save_mm[r],R_SUBMMWHOLE),href,nil);
  2400. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2401. end;
  2402. include(rg[R_MMREGISTER].preserved_by_proc,regs_to_save_mm[r]);
  2403. end;
  2404. end;
  2405. end;
  2406. end;
  2407. end;
  2408. procedure tcg.g_restore_registers(list:TAsmList);
  2409. var
  2410. href : treference;
  2411. r : integer;
  2412. hreg : tregister;
  2413. regs_to_save_int,
  2414. regs_to_save_address,
  2415. regs_to_save_mm : tcpuregisterarray;
  2416. begin
  2417. if not(pi_has_saved_regs in current_procinfo.flags) then
  2418. exit;
  2419. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2420. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  2421. regs_to_save_mm:=paramanager.get_saved_registers_mm(current_procinfo.procdef.proccalloption);
  2422. { Copy registers from temp }
  2423. href:=current_procinfo.save_regs_ref;
  2424. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2425. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2426. begin
  2427. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  2428. { Allocate register so the optimizer does not remove the load }
  2429. a_reg_alloc(list,hreg);
  2430. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2431. inc(href.offset,sizeof(aint));
  2432. end;
  2433. if uses_registers(R_ADDRESSREGISTER) then
  2434. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  2435. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2436. begin
  2437. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  2438. { Allocate register so the optimizer does not remove the load }
  2439. a_reg_alloc(list,hreg);
  2440. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2441. inc(href.offset,sizeof(aint));
  2442. end;
  2443. if uses_registers(R_MMREGISTER) then
  2444. begin
  2445. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2446. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2447. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2448. begin
  2449. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2450. begin
  2451. hreg:=newreg(R_MMREGISTER,regs_to_save_mm[r],R_SUBMMWHOLE);
  2452. { Allocate register so the optimizer does not remove the load }
  2453. a_reg_alloc(list,hreg);
  2454. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2455. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2456. end;
  2457. end;
  2458. end;
  2459. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2460. end;
  2461. procedure tcg.g_profilecode(list : TAsmList);
  2462. begin
  2463. end;
  2464. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2465. var
  2466. hsym : tsym;
  2467. href : treference;
  2468. paraloc : Pcgparalocation;
  2469. begin
  2470. { calculate the parameter info for the procdef }
  2471. procdef.init_paraloc_info(callerside);
  2472. hsym:=tsym(procdef.parast.Find('self'));
  2473. if not(assigned(hsym) and
  2474. (hsym.typ=paravarsym)) then
  2475. internalerror(200305251);
  2476. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2477. while paraloc<>nil do
  2478. with paraloc^ do
  2479. begin
  2480. case loc of
  2481. LOC_REGISTER:
  2482. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2483. LOC_REFERENCE:
  2484. begin
  2485. { offset in the wrapper needs to be adjusted for the stored
  2486. return address }
  2487. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  2488. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2489. end
  2490. else
  2491. internalerror(200309189);
  2492. end;
  2493. paraloc:=next;
  2494. end;
  2495. end;
  2496. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2497. begin
  2498. a_call_name(list,s,false);
  2499. end;
  2500. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2501. var
  2502. l: tasmsymbol;
  2503. ref: treference;
  2504. nlsymname: string;
  2505. symtyp: TAsmsymtype;
  2506. begin
  2507. result := NR_NO;
  2508. case target_info.system of
  2509. system_powerpc_darwin,
  2510. system_i386_darwin,
  2511. system_i386_iphonesim,
  2512. system_powerpc64_darwin,
  2513. system_arm_ios:
  2514. begin
  2515. nlsymname:='L'+symname+'$non_lazy_ptr';
  2516. l:=current_asmdata.getasmsymbol(nlsymname);
  2517. if not(assigned(l)) then
  2518. begin
  2519. if is_data in flags then
  2520. symtyp:=AT_DATA
  2521. else
  2522. symtyp:=AT_FUNCTION;
  2523. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2524. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA,voidpointertype);
  2525. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2526. if not(is_weak in flags) then
  2527. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname,symtyp).Name))
  2528. else
  2529. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname,symtyp).Name));
  2530. {$ifdef cpu64bitaddr}
  2531. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2532. {$else cpu64bitaddr}
  2533. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2534. {$endif cpu64bitaddr}
  2535. end;
  2536. result := getaddressregister(list);
  2537. reference_reset_symbol(ref,l,0,sizeof(pint),[]);
  2538. { a_load_ref_reg will turn this into a pic-load if needed }
  2539. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2540. end;
  2541. end;
  2542. end;
  2543. procedure tcg.g_maybe_got_init(list: TAsmList);
  2544. begin
  2545. end;
  2546. procedure tcg.g_call(list: TAsmList;const s: string);
  2547. begin
  2548. allocallcpuregisters(list);
  2549. a_call_name(list,s,false);
  2550. deallocallcpuregisters(list);
  2551. end;
  2552. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2553. begin
  2554. a_jmp_always(list,l);
  2555. end;
  2556. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2557. begin
  2558. internalerror(200807231);
  2559. end;
  2560. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2561. begin
  2562. internalerror(200807232);
  2563. end;
  2564. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2565. begin
  2566. internalerror(200807233);
  2567. end;
  2568. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2569. begin
  2570. internalerror(200807234);
  2571. end;
  2572. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2573. begin
  2574. Result:=TRegister(0);
  2575. internalerror(200807238);
  2576. end;
  2577. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  2578. begin
  2579. internalerror(2014070601);
  2580. end;
  2581. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2582. begin
  2583. internalerror(2014070602);
  2584. end;
  2585. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2586. begin
  2587. internalerror(2014060801);
  2588. end;
  2589. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2590. var
  2591. divreg: tregister;
  2592. magic: aInt;
  2593. u_magic: aWord;
  2594. u_shift: byte;
  2595. u_add: boolean;
  2596. begin
  2597. divreg:=getintregister(list,OS_INT);
  2598. if (size in [OS_S32,OS_S64]) then
  2599. begin
  2600. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2601. { load magic value }
  2602. a_load_const_reg(list,OS_INT,magic,divreg);
  2603. { multiply, discarding low bits }
  2604. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2605. { add/subtract numerator }
  2606. if (a>0) and (magic<0) then
  2607. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2608. else if (a<0) and (magic>0) then
  2609. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2610. { shift shift places to the right (arithmetic) }
  2611. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2612. { extract and add sign bit }
  2613. if (a>=0) then
  2614. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2615. else
  2616. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2617. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2618. end
  2619. else if (size in [OS_32,OS_64]) then
  2620. begin
  2621. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2622. { load magic in divreg }
  2623. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2624. { multiply, discarding low bits }
  2625. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2626. if (u_add) then
  2627. begin
  2628. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2629. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2630. { divreg=(numerator-result) }
  2631. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2632. { divreg=(numerator-result)/2 }
  2633. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2634. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2635. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2636. end
  2637. else
  2638. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2639. end
  2640. else
  2641. InternalError(2014060601);
  2642. end;
  2643. procedure tcg.g_check_for_fpu_exception(list: TAsmList;force,clear : boolean);
  2644. begin
  2645. { empty by default }
  2646. end;
  2647. procedure tcg.maybe_check_for_fpu_exception(list: TAsmList);
  2648. begin
  2649. current_procinfo.FPUExceptionCheckNeeded:=true;
  2650. g_check_for_fpu_exception(list,false,true);
  2651. end;
  2652. {*****************************************************************************
  2653. TCG64
  2654. *****************************************************************************}
  2655. {$ifndef cpu64bitalu}
  2656. function joinreg64(reglo,reghi : tregister) : tregister64;
  2657. begin
  2658. result.reglo:=reglo;
  2659. result.reghi:=reghi;
  2660. end;
  2661. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2662. begin
  2663. a_load64_reg_reg(list,regsrc,regdst);
  2664. a_op64_const_reg(list,op,size,value,regdst);
  2665. end;
  2666. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2667. var
  2668. tmpreg64 : tregister64;
  2669. begin
  2670. { when src1=dst then we need to first create a temp to prevent
  2671. overwriting src1 with src2 }
  2672. if (regsrc1.reghi=regdst.reghi) or
  2673. (regsrc1.reglo=regdst.reghi) or
  2674. (regsrc1.reghi=regdst.reglo) or
  2675. (regsrc1.reglo=regdst.reglo) then
  2676. begin
  2677. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2678. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2679. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2680. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2681. a_load64_reg_reg(list,tmpreg64,regdst);
  2682. end
  2683. else
  2684. begin
  2685. a_load64_reg_reg(list,regsrc2,regdst);
  2686. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2687. end;
  2688. end;
  2689. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2690. var
  2691. tmpreg64 : tregister64;
  2692. begin
  2693. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2694. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2695. a_load64_subsetref_reg(list,sref,tmpreg64);
  2696. a_op64_const_reg(list,op,size,a,tmpreg64);
  2697. a_load64_reg_subsetref(list,tmpreg64,sref);
  2698. end;
  2699. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2700. var
  2701. tmpreg64 : tregister64;
  2702. begin
  2703. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2704. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2705. a_load64_subsetref_reg(list,sref,tmpreg64);
  2706. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2707. a_load64_reg_subsetref(list,tmpreg64,sref);
  2708. end;
  2709. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2710. var
  2711. tmpreg64 : tregister64;
  2712. begin
  2713. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2714. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2715. a_load64_subsetref_reg(list,sref,tmpreg64);
  2716. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2717. a_load64_reg_subsetref(list,tmpreg64,sref);
  2718. end;
  2719. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2720. var
  2721. tmpreg64 : tregister64;
  2722. begin
  2723. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2724. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2725. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2726. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2727. end;
  2728. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2729. begin
  2730. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2731. ovloc.loc:=LOC_VOID;
  2732. end;
  2733. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2734. begin
  2735. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2736. ovloc.loc:=LOC_VOID;
  2737. end;
  2738. procedure tcg64.a_op64_reg(list: TAsmList; op: TOpCG; size: tcgsize; regdst: tregister64);
  2739. begin
  2740. if not (op in [OP_NOT,OP_NEG]) then
  2741. internalerror(2020050706);
  2742. a_op64_reg_reg(list,op,size,regdst,regdst);
  2743. end;
  2744. procedure tcg64.a_op64_ref(list: TAsmList; op: TOpCG; size: tcgsize; const ref: treference);
  2745. var
  2746. tempreg: tregister64;
  2747. begin
  2748. if not (op in [OP_NOT,OP_NEG]) then
  2749. internalerror(2020050706);
  2750. tempreg.reghi:=cg.getintregister(list,OS_32);
  2751. tempreg.reglo:=cg.getintregister(list,OS_32);
  2752. a_load64_ref_reg(list,ref,tempreg);
  2753. a_op64_reg_reg(list,op,size,tempreg,tempreg);
  2754. a_load64_reg_ref(list,tempreg,ref);
  2755. end;
  2756. procedure tcg64.a_op64_loc(list: TAsmList; op: TOpCG; size: tcgsize; const l: tlocation);
  2757. begin
  2758. case l.loc of
  2759. LOC_REFERENCE, LOC_CREFERENCE:
  2760. a_op64_ref(list,op,size,l.reference);
  2761. LOC_REGISTER,LOC_CREGISTER:
  2762. a_op64_reg(list,op,size,l.register64);
  2763. else
  2764. internalerror(2020050707);
  2765. end;
  2766. end;
  2767. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2768. begin
  2769. case l.loc of
  2770. LOC_REFERENCE, LOC_CREFERENCE:
  2771. a_load64_ref_subsetref(list,l.reference,sref);
  2772. LOC_REGISTER,LOC_CREGISTER:
  2773. a_load64_reg_subsetref(list,l.register64,sref);
  2774. LOC_CONSTANT :
  2775. a_load64_const_subsetref(list,l.value64,sref);
  2776. LOC_SUBSETREF,LOC_CSUBSETREF:
  2777. a_load64_subsetref_subsetref(list,l.sref,sref);
  2778. else
  2779. internalerror(2006082210);
  2780. end;
  2781. end;
  2782. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2783. begin
  2784. case l.loc of
  2785. LOC_REFERENCE, LOC_CREFERENCE:
  2786. a_load64_subsetref_ref(list,sref,l.reference);
  2787. LOC_REGISTER,LOC_CREGISTER:
  2788. a_load64_subsetref_reg(list,sref,l.register64);
  2789. LOC_SUBSETREF,LOC_CSUBSETREF:
  2790. a_load64_subsetref_subsetref(list,sref,l.sref);
  2791. else
  2792. internalerror(2006082211);
  2793. end;
  2794. end;
  2795. {$else cpu64bitalu}
  2796. function joinreg128(reglo, reghi: tregister): tregister128;
  2797. begin
  2798. result.reglo:=reglo;
  2799. result.reghi:=reghi;
  2800. end;
  2801. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2802. var
  2803. paraloclo,
  2804. paralochi : pcgparalocation;
  2805. begin
  2806. if not(cgpara.size in [OS_128,OS_S128]) then
  2807. internalerror(2012090604);
  2808. if not assigned(cgpara.location) then
  2809. internalerror(2012090605);
  2810. { init lo/hi para }
  2811. cgparahi.reset;
  2812. if cgpara.size=OS_S128 then
  2813. cgparahi.size:=OS_S64
  2814. else
  2815. cgparahi.size:=OS_64;
  2816. cgparahi.intsize:=8;
  2817. cgparahi.alignment:=cgpara.alignment;
  2818. paralochi:=cgparahi.add_location;
  2819. cgparalo.reset;
  2820. cgparalo.size:=OS_64;
  2821. cgparalo.intsize:=8;
  2822. cgparalo.alignment:=cgpara.alignment;
  2823. paraloclo:=cgparalo.add_location;
  2824. { 2 parameter fields? }
  2825. if assigned(cgpara.location^.next) then
  2826. begin
  2827. { Order for multiple locations is always
  2828. paraloc^ -> high
  2829. paraloc^.next -> low }
  2830. if (target_info.endian=ENDIAN_BIG) then
  2831. begin
  2832. { paraloc^ -> high
  2833. paraloc^.next -> low }
  2834. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2835. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2836. end
  2837. else
  2838. begin
  2839. { paraloc^ -> low
  2840. paraloc^.next -> high }
  2841. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2842. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2843. end;
  2844. end
  2845. else
  2846. begin
  2847. { single parameter, this can only be in memory }
  2848. if cgpara.location^.loc<>LOC_REFERENCE then
  2849. internalerror(2012090606);
  2850. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2851. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2852. { for big endian low is at +8, for little endian high }
  2853. if target_info.endian = endian_big then
  2854. begin
  2855. inc(cgparalo.location^.reference.offset,8);
  2856. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2857. end
  2858. else
  2859. begin
  2860. inc(cgparahi.location^.reference.offset,8);
  2861. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2862. end;
  2863. end;
  2864. { fix size }
  2865. paraloclo^.size:=cgparalo.size;
  2866. paraloclo^.next:=nil;
  2867. paralochi^.size:=cgparahi.size;
  2868. paralochi^.next:=nil;
  2869. end;
  2870. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2871. regdst: tregister128);
  2872. begin
  2873. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2874. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2875. end;
  2876. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2877. const ref: treference);
  2878. var
  2879. tmpreg: tregister;
  2880. tmpref: treference;
  2881. begin
  2882. if target_info.endian = endian_big then
  2883. begin
  2884. tmpreg:=reg.reglo;
  2885. reg.reglo:=reg.reghi;
  2886. reg.reghi:=tmpreg;
  2887. end;
  2888. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2889. tmpref := ref;
  2890. inc(tmpref.offset,8);
  2891. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2892. end;
  2893. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2894. reg: tregister128);
  2895. var
  2896. tmpreg: tregister;
  2897. tmpref: treference;
  2898. begin
  2899. if target_info.endian = endian_big then
  2900. begin
  2901. tmpreg := reg.reglo;
  2902. reg.reglo := reg.reghi;
  2903. reg.reghi := tmpreg;
  2904. end;
  2905. tmpref := ref;
  2906. if (tmpref.base=reg.reglo) then
  2907. begin
  2908. tmpreg:=cg.getaddressregister(list);
  2909. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2910. tmpref.base:=tmpreg;
  2911. end
  2912. else
  2913. { this works only for the i386, thus the i386 needs to override }
  2914. { this method and this method must be replaced by a more generic }
  2915. { implementation FK }
  2916. if (tmpref.index=reg.reglo) then
  2917. begin
  2918. tmpreg:=cg.getaddressregister(list);
  2919. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2920. tmpref.index:=tmpreg;
  2921. end;
  2922. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2923. inc(tmpref.offset,8);
  2924. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2925. end;
  2926. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2927. const ref: treference);
  2928. begin
  2929. case l.loc of
  2930. LOC_REGISTER,LOC_CREGISTER:
  2931. a_load128_reg_ref(list,l.register128,ref);
  2932. { not yet implemented:
  2933. LOC_CONSTANT :
  2934. a_load128_const_ref(list,l.value128,ref);
  2935. LOC_SUBSETREF, LOC_CSUBSETREF:
  2936. a_load64_subsetref_ref(list,l.sref,ref); }
  2937. else
  2938. internalerror(201209061);
  2939. end;
  2940. end;
  2941. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2942. const l: tlocation);
  2943. begin
  2944. case l.loc of
  2945. LOC_REFERENCE, LOC_CREFERENCE:
  2946. a_load128_reg_ref(list,reg,l.reference);
  2947. LOC_REGISTER,LOC_CREGISTER:
  2948. a_load128_reg_reg(list,reg,l.register128);
  2949. { not yet implemented:
  2950. LOC_SUBSETREF, LOC_CSUBSETREF:
  2951. a_load64_reg_subsetref(list,reg,l.sref);
  2952. LOC_MMREGISTER, LOC_CMMREGISTER:
  2953. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2954. else
  2955. internalerror(201209062);
  2956. end;
  2957. end;
  2958. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2959. valuehi: int64; reg: tregister128);
  2960. begin
  2961. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2962. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2963. end;
  2964. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2965. const paraloc: TCGPara);
  2966. begin
  2967. case l.loc of
  2968. LOC_REGISTER,
  2969. LOC_CREGISTER :
  2970. a_load128_reg_cgpara(list,l.register128,paraloc);
  2971. {not yet implemented:
  2972. LOC_CONSTANT :
  2973. a_load128_const_cgpara(list,l.value64,paraloc);
  2974. }
  2975. LOC_CREFERENCE,
  2976. LOC_REFERENCE :
  2977. a_load128_ref_cgpara(list,l.reference,paraloc);
  2978. else
  2979. internalerror(2012090603);
  2980. end;
  2981. end;
  2982. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2983. var
  2984. tmplochi,tmploclo: tcgpara;
  2985. begin
  2986. tmploclo.init;
  2987. tmplochi.init;
  2988. splitparaloc128(paraloc,tmploclo,tmplochi);
  2989. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2990. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2991. tmploclo.done;
  2992. tmplochi.done;
  2993. end;
  2994. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2995. var
  2996. tmprefhi,tmpreflo : treference;
  2997. tmploclo,tmplochi : tcgpara;
  2998. begin
  2999. tmploclo.init;
  3000. tmplochi.init;
  3001. splitparaloc128(paraloc,tmploclo,tmplochi);
  3002. tmprefhi:=r;
  3003. tmpreflo:=r;
  3004. if target_info.endian=endian_big then
  3005. inc(tmpreflo.offset,8)
  3006. else
  3007. inc(tmprefhi.offset,8);
  3008. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  3009. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  3010. tmploclo.done;
  3011. tmplochi.done;
  3012. end;
  3013. {$endif cpu64bitalu}
  3014. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  3015. begin
  3016. result:=[];
  3017. if sym.typ<>AT_FUNCTION then
  3018. include(result,is_data);
  3019. if sym.bind=AB_WEAK_EXTERNAL then
  3020. include(result,is_weak);
  3021. end;
  3022. procedure destroy_codegen;
  3023. begin
  3024. cg.free;
  3025. cg:=nil;
  3026. {$ifdef cpu64bitalu}
  3027. cg128.free;
  3028. cg128:=nil;
  3029. {$else cpu64bitalu}
  3030. cg64.free;
  3031. cg64:=nil;
  3032. {$endif cpu64bitalu}
  3033. end;
  3034. end.