ncgutil.pas 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bitalu}
  27. ,cg64f32
  28. {$endif not cpu64bitalu}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  50. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mem(list:TAsmList;var l:tlocation);
  52. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  53. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  54. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  55. { Retrieve the location of the data pointed to in location l, when the location is
  56. a register it is expected to contain the address of the data }
  57. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);
  58. function has_alias_name(pd:tprocdef;const s:string):boolean;
  59. procedure alloc_proc_symbol(pd: tprocdef);
  60. procedure gen_proc_symbol(list:TAsmList);
  61. procedure gen_proc_symbol_end(list:TAsmList);
  62. procedure gen_proc_entry_code(list:TAsmList);
  63. procedure gen_proc_exit_code(list:TAsmList);
  64. procedure gen_stack_check_size_para(list:TAsmList);
  65. procedure gen_stack_check_call(list:TAsmList);
  66. procedure gen_save_used_regs(list:TAsmList);
  67. procedure gen_restore_used_regs(list:TAsmList);
  68. procedure gen_initialize_code(list:TAsmList);
  69. procedure gen_finalize_code(list:TAsmList);
  70. procedure gen_entry_code(list:TAsmList);
  71. procedure gen_exit_code(list:TAsmList);
  72. procedure gen_load_para_value(list:TAsmList);
  73. procedure gen_load_return_value(list:TAsmList);
  74. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  75. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  76. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  77. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  78. { adds the regvars used in n and its children to rv.allregvars,
  79. those which were already in rv.allregvars to rv.commonregvars and
  80. uses rv.myregvars as scratch (so that two uses of the same regvar
  81. in a single tree to make it appear in commonregvars). Useful to
  82. find out which regvars are used in two different node trees
  83. (e.g. in the "else" and "then" path, or in various case blocks }
  84. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  85. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  86. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  87. { loadn and change its location to a new register (= SSA). In case reload }
  88. { is true, transfer the old to the new register }
  89. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  90. {#
  91. Allocate the buffers for exception management and setjmp environment.
  92. Return a pointer to these buffers, send them to the utility routine
  93. so they are registered, and then call setjmp.
  94. Then compare the result of setjmp with 0, and if not equal
  95. to zero, then jump to exceptlabel.
  96. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  97. It is to note that this routine may be called *after* the stackframe of a
  98. routine has been called, therefore on machines where the stack cannot
  99. be modified, all temps should be allocated on the heap instead of the
  100. stack.
  101. }
  102. const
  103. EXCEPT_BUF_SIZE = 3*sizeof(pint);
  104. type
  105. texceptiontemps=record
  106. jmpbuf,
  107. envbuf,
  108. reasonbuf : treference;
  109. end;
  110. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  111. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  112. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  113. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  114. procedure insertbssdata(sym : tstaticvarsym);
  115. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  116. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  117. procedure location_free(list: TAsmList; const location : TLocation);
  118. function getprocalign : shortint;
  119. procedure gen_pic_helpers(list : TAsmList);
  120. implementation
  121. uses
  122. version,
  123. cutils,cclasses,
  124. globals,systems,verbose,export,
  125. ppu,defutil,
  126. procinfo,paramgr,fmodule,
  127. regvars,dbgbase,
  128. pass_1,pass_2,
  129. nbas,ncon,nld,nmem,nutils,
  130. tgobj,cgobj,cgcpu
  131. {$ifdef powerpc}
  132. , cpupi
  133. {$endif}
  134. {$ifdef powerpc64}
  135. , cpupi
  136. {$endif}
  137. {$ifdef SUPPORT_MMX}
  138. , cgx86
  139. {$endif SUPPORT_MMX}
  140. ;
  141. {*****************************************************************************
  142. Misc Helpers
  143. *****************************************************************************}
  144. procedure location_free(list: TAsmList; const location : TLocation);
  145. begin
  146. case location.loc of
  147. LOC_VOID:
  148. ;
  149. LOC_REGISTER,
  150. LOC_CREGISTER:
  151. begin
  152. {$ifdef cpu64bitaddr}
  153. { x86-64 system v abi:
  154. structs with up to 16 bytes are returned in registers }
  155. if location.size in [OS_128,OS_S128] then
  156. begin
  157. if getsupreg(location.register)<first_int_imreg then
  158. cg.ungetcpuregister(list,location.register);
  159. if getsupreg(location.registerhi)<first_int_imreg then
  160. cg.ungetcpuregister(list,location.registerhi);
  161. end
  162. {$else cpu64bitaddr}
  163. if location.size in [OS_64,OS_S64] then
  164. begin
  165. if getsupreg(location.register64.reglo)<first_int_imreg then
  166. cg.ungetcpuregister(list,location.register64.reglo);
  167. if getsupreg(location.register64.reghi)<first_int_imreg then
  168. cg.ungetcpuregister(list,location.register64.reghi);
  169. end
  170. {$endif}
  171. else
  172. if getsupreg(location.register)<first_int_imreg then
  173. cg.ungetcpuregister(list,location.register);
  174. end;
  175. LOC_FPUREGISTER,
  176. LOC_CFPUREGISTER:
  177. begin
  178. if getsupreg(location.register)<first_fpu_imreg then
  179. cg.ungetcpuregister(list,location.register);
  180. end;
  181. LOC_MMREGISTER,
  182. LOC_CMMREGISTER :
  183. begin
  184. if getsupreg(location.register)<first_mm_imreg then
  185. cg.ungetcpuregister(list,location.register);
  186. end;
  187. LOC_REFERENCE,
  188. LOC_CREFERENCE :
  189. begin
  190. if use_fixed_stack then
  191. location_freetemp(list,location);
  192. end;
  193. else
  194. internalerror(2004110211);
  195. end;
  196. end;
  197. procedure firstcomplex(p : tbinarynode);
  198. var
  199. fcl, fcr: longint;
  200. ncl, ncr: longint;
  201. begin
  202. { always calculate boolean AND and OR from left to right }
  203. if (p.nodetype in [orn,andn]) and
  204. is_boolean(p.left.resultdef) then
  205. begin
  206. if nf_swapped in p.flags then
  207. internalerror(200709253);
  208. end
  209. else
  210. begin
  211. fcl:=node_resources_fpu(p.left);
  212. fcr:=node_resources_fpu(p.right);
  213. ncl:=node_complexity(p.left);
  214. ncr:=node_complexity(p.right);
  215. { We swap left and right if
  216. a) right needs more floating point registers than left, and
  217. left needs more than 0 floating point registers (if it
  218. doesn't need any, swapping won't change the floating
  219. point register pressure)
  220. b) both left and right need an equal amount of floating
  221. point registers or right needs no floating point registers,
  222. and in addition right has a higher complexity than left
  223. (+- needs more integer registers, but not necessarily)
  224. }
  225. if ((fcr>fcl) and
  226. (fcl>0)) or
  227. (((fcr=fcl) or
  228. (fcr=0)) and
  229. (ncr>ncl)) then
  230. p.swapleftright
  231. end;
  232. end;
  233. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  234. {
  235. produces jumps to true respectively false labels using boolean expressions
  236. depending on whether the loading of regvars is currently being
  237. synchronized manually (such as in an if-node) or automatically (most of
  238. the other cases where this procedure is called), loadregvars can be
  239. "lr_load_regvars" or "lr_dont_load_regvars"
  240. }
  241. var
  242. opsize : tcgsize;
  243. storepos : tfileposinfo;
  244. tmpreg : tregister;
  245. begin
  246. if nf_error in p.flags then
  247. exit;
  248. storepos:=current_filepos;
  249. current_filepos:=p.fileinfo;
  250. if is_boolean(p.resultdef) then
  251. begin
  252. {$ifdef OLDREGVARS}
  253. if loadregvars = lr_load_regvars then
  254. load_all_regvars(list);
  255. {$endif OLDREGVARS}
  256. if is_constboolnode(p) then
  257. begin
  258. if Tordconstnode(p).value.uvalue<>0 then
  259. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  260. else
  261. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  262. end
  263. else
  264. begin
  265. opsize:=def_cgsize(p.resultdef);
  266. case p.location.loc of
  267. LOC_SUBSETREG,LOC_CSUBSETREG,
  268. LOC_SUBSETREF,LOC_CSUBSETREF:
  269. begin
  270. tmpreg := cg.getintregister(list,OS_INT);
  271. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  272. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  273. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  274. end;
  275. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  276. begin
  277. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  278. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  279. end;
  280. LOC_JUMP:
  281. ;
  282. {$ifdef cpuflags}
  283. LOC_FLAGS :
  284. begin
  285. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  286. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  287. end;
  288. {$endif cpuflags}
  289. else
  290. begin
  291. printnode(output,p);
  292. internalerror(200308241);
  293. end;
  294. end;
  295. end;
  296. end
  297. else
  298. internalerror(200112305);
  299. current_filepos:=storepos;
  300. end;
  301. (*
  302. This code needs fixing. It is not safe to use rgint; on the m68000 it
  303. would be rgaddr.
  304. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  305. begin
  306. case t.loc of
  307. LOC_REGISTER:
  308. begin
  309. { can't be a regvar, since it would be LOC_CREGISTER then }
  310. exclude(regs,getsupreg(t.register));
  311. if t.register64.reghi<>NR_NO then
  312. exclude(regs,getsupreg(t.register64.reghi));
  313. end;
  314. LOC_CREFERENCE,LOC_REFERENCE:
  315. begin
  316. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  317. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  318. exclude(regs,getsupreg(t.reference.base));
  319. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  320. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  321. exclude(regs,getsupreg(t.reference.index));
  322. end;
  323. end;
  324. end;
  325. *)
  326. {*****************************************************************************
  327. EXCEPTION MANAGEMENT
  328. *****************************************************************************}
  329. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  330. var
  331. srsym : ttypesym;
  332. begin
  333. if jmp_buf_size=-1 then
  334. begin
  335. srsym:=search_system_type('JMP_BUF');
  336. jmp_buf_size:=srsym.typedef.size;
  337. jmp_buf_align:=srsym.typedef.alignment;
  338. end;
  339. tg.GetTemp(list,EXCEPT_BUF_SIZE,sizeof(pint),tt_persistent,t.envbuf);
  340. tg.GetTemp(list,jmp_buf_size,jmp_buf_align,tt_persistent,t.jmpbuf);
  341. tg.GetTemp(list,sizeof(pint),sizeof(pint),tt_persistent,t.reasonbuf);
  342. end;
  343. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  344. begin
  345. tg.Ungettemp(list,t.jmpbuf);
  346. tg.ungettemp(list,t.envbuf);
  347. tg.ungettemp(list,t.reasonbuf);
  348. end;
  349. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  350. var
  351. paraloc1,paraloc2,paraloc3 : tcgpara;
  352. begin
  353. paraloc1.init;
  354. paraloc2.init;
  355. paraloc3.init;
  356. paramanager.getintparaloc(pocall_default,1,paraloc1);
  357. paramanager.getintparaloc(pocall_default,2,paraloc2);
  358. paramanager.getintparaloc(pocall_default,3,paraloc3);
  359. paramanager.allocparaloc(list,paraloc3);
  360. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  361. paramanager.allocparaloc(list,paraloc2);
  362. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  363. { push type of exceptionframe }
  364. paramanager.allocparaloc(list,paraloc1);
  365. cg.a_param_const(list,OS_S32,1,paraloc1);
  366. paramanager.freeparaloc(list,paraloc3);
  367. paramanager.freeparaloc(list,paraloc2);
  368. paramanager.freeparaloc(list,paraloc1);
  369. cg.allocallcpuregisters(list);
  370. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR',false);
  371. cg.deallocallcpuregisters(list);
  372. paramanager.getintparaloc(pocall_default,1,paraloc1);
  373. paramanager.allocparaloc(list,paraloc1);
  374. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  375. paramanager.freeparaloc(list,paraloc1);
  376. cg.allocallcpuregisters(list);
  377. cg.a_call_name(list,'FPC_SETJMP',false);
  378. cg.deallocallcpuregisters(list);
  379. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  380. cg.g_exception_reason_save(list, t.reasonbuf);
  381. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  382. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  383. paraloc1.done;
  384. paraloc2.done;
  385. paraloc3.done;
  386. end;
  387. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  388. begin
  389. cg.allocallcpuregisters(list);
  390. cg.a_call_name(list,'FPC_POPADDRSTACK',false);
  391. cg.deallocallcpuregisters(list);
  392. if not onlyfree then
  393. begin
  394. { g_exception_reason_load already allocates NR_FUNCTION_RESULT_REG }
  395. cg.g_exception_reason_load(list, t.reasonbuf);
  396. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  397. cg.a_reg_dealloc(list,NR_FUNCTION_RESULT_REG);
  398. end;
  399. end;
  400. {*****************************************************************************
  401. TLocation
  402. *****************************************************************************}
  403. {$ifndef cpu64bitalu}
  404. { 32-bit version }
  405. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  406. var
  407. hregister,
  408. hregisterhi : tregister;
  409. hreg64 : tregister64;
  410. hl : tasmlabel;
  411. oldloc : tlocation;
  412. const_location: boolean;
  413. begin
  414. oldloc:=l;
  415. if dst_size=OS_NO then
  416. internalerror(200309144);
  417. { handle transformations to 64bit separate }
  418. if dst_size in [OS_64,OS_S64] then
  419. begin
  420. if not (l.size in [OS_64,OS_S64]) then
  421. begin
  422. { load a smaller size to OS_64 }
  423. if l.loc=LOC_REGISTER then
  424. begin
  425. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  426. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  427. end
  428. else
  429. hregister:=cg.getintregister(list,OS_INT);
  430. { load value in low register }
  431. case l.loc of
  432. {$ifdef cpuflags}
  433. LOC_FLAGS :
  434. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  435. {$endif cpuflags}
  436. LOC_JUMP :
  437. begin
  438. cg.a_label(list,current_procinfo.CurrTrueLabel);
  439. cg.a_load_const_reg(list,OS_INT,1,hregister);
  440. current_asmdata.getjumplabel(hl);
  441. cg.a_jmp_always(list,hl);
  442. cg.a_label(list,current_procinfo.CurrFalseLabel);
  443. cg.a_load_const_reg(list,OS_INT,0,hregister);
  444. cg.a_label(list,hl);
  445. end;
  446. else
  447. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  448. end;
  449. { reset hi part, take care of the signed bit of the current value }
  450. hregisterhi:=cg.getintregister(list,OS_INT);
  451. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  452. begin
  453. if l.loc=LOC_CONSTANT then
  454. begin
  455. if (longint(l.value)<0) then
  456. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  457. else
  458. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  459. end
  460. else
  461. begin
  462. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  463. hregisterhi);
  464. end;
  465. end
  466. else
  467. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  468. location_reset(l,LOC_REGISTER,dst_size);
  469. l.register64.reglo:=hregister;
  470. l.register64.reghi:=hregisterhi;
  471. end
  472. else
  473. begin
  474. { 64bit to 64bit }
  475. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  476. begin
  477. hregister:=l.register64.reglo;
  478. hregisterhi:=l.register64.reghi;
  479. const_location := true;
  480. end
  481. else
  482. begin
  483. hregister:=cg.getintregister(list,OS_INT);
  484. hregisterhi:=cg.getintregister(list,OS_INT);
  485. const_location := false;
  486. end;
  487. hreg64.reglo:=hregister;
  488. hreg64.reghi:=hregisterhi;
  489. { load value in new register }
  490. cg64.a_load64_loc_reg(list,l,hreg64);
  491. if not const_location then
  492. location_reset(l,LOC_REGISTER,dst_size)
  493. else
  494. location_reset(l,LOC_CREGISTER,dst_size);
  495. l.register64.reglo:=hregister;
  496. l.register64.reghi:=hregisterhi;
  497. end;
  498. end
  499. else
  500. begin
  501. {Do not bother to recycle the existing register. The register
  502. allocator eliminates unnecessary moves, so it's not needed
  503. and trying to recycle registers can cause problems because
  504. the registers changes size and may need aditional constraints.
  505. Not if it's about LOC_CREGISTER's (JM)
  506. }
  507. const_location :=
  508. (maybeconst) and
  509. (l.loc = LOC_CREGISTER) and
  510. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  511. ((l.size = dst_size) or
  512. (TCGSize2Size[l.size] = sizeof(aint)));
  513. if not const_location then
  514. hregister:=cg.getintregister(list,dst_size)
  515. else
  516. hregister := l.register;
  517. { load value in new register }
  518. case l.loc of
  519. {$ifdef cpuflags}
  520. LOC_FLAGS :
  521. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  522. {$endif cpuflags}
  523. LOC_JUMP :
  524. begin
  525. cg.a_label(list,current_procinfo.CurrTrueLabel);
  526. cg.a_load_const_reg(list,dst_size,1,hregister);
  527. current_asmdata.getjumplabel(hl);
  528. cg.a_jmp_always(list,hl);
  529. cg.a_label(list,current_procinfo.CurrFalseLabel);
  530. cg.a_load_const_reg(list,dst_size,0,hregister);
  531. cg.a_label(list,hl);
  532. end;
  533. else
  534. begin
  535. { load_loc_reg can only handle size >= l.size, when the
  536. new size is smaller then we need to adjust the size
  537. of the orignal and maybe recalculate l.register for i386 }
  538. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  539. begin
  540. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  541. l.register:=cg.makeregsize(list,l.register,dst_size);
  542. { for big endian systems, the reference's offset must }
  543. { be increased in this case, since they have the }
  544. { MSB first in memory and e.g. byte(word_var) should }
  545. { return the second byte in this case (JM) }
  546. if (target_info.endian = ENDIAN_BIG) and
  547. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  548. begin
  549. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  550. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  551. end;
  552. {$ifdef x86}
  553. if not (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  554. l.size:=dst_size;
  555. {$endif x86}
  556. end;
  557. cg.a_load_loc_reg(list,dst_size,l,hregister);
  558. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size])
  559. {$ifdef x86}
  560. and (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG])
  561. {$endif x86}
  562. then
  563. l.size:=dst_size;
  564. end;
  565. end;
  566. if not const_location then
  567. location_reset(l,LOC_REGISTER,dst_size)
  568. else
  569. location_reset(l,LOC_CREGISTER,dst_size);
  570. l.register:=hregister;
  571. end;
  572. { Release temp when it was a reference }
  573. if oldloc.loc=LOC_REFERENCE then
  574. location_freetemp(list,oldloc);
  575. end;
  576. {$else not cpu64bitalu}
  577. { 64-bit version }
  578. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  579. var
  580. hregister : tregister;
  581. hl : tasmlabel;
  582. oldloc : tlocation;
  583. begin
  584. oldloc:=l;
  585. hregister:=cg.getintregister(list,dst_size);
  586. { load value in new register }
  587. case l.loc of
  588. LOC_FLAGS :
  589. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  590. LOC_JUMP :
  591. begin
  592. cg.a_label(list,current_procinfo.CurrTrueLabel);
  593. cg.a_load_const_reg(list,dst_size,1,hregister);
  594. current_asmdata.getjumplabel(hl);
  595. cg.a_jmp_always(list,hl);
  596. cg.a_label(list,current_procinfo.CurrFalseLabel);
  597. cg.a_load_const_reg(list,dst_size,0,hregister);
  598. cg.a_label(list,hl);
  599. end;
  600. else
  601. begin
  602. { load_loc_reg can only handle size >= l.size, when the
  603. new size is smaller then we need to adjust the size
  604. of the orignal and maybe recalculate l.register for i386 }
  605. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  606. begin
  607. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  608. l.register:=cg.makeregsize(list,l.register,dst_size);
  609. { for big endian systems, the reference's offset must }
  610. { be increased in this case, since they have the }
  611. { MSB first in memory and e.g. byte(word_var) should }
  612. { return the second byte in this case (JM) }
  613. if (target_info.endian = ENDIAN_BIG) and
  614. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  615. begin
  616. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  617. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  618. end;
  619. {$ifdef x86}
  620. l.size:=dst_size;
  621. {$endif x86}
  622. end;
  623. cg.a_load_loc_reg(list,dst_size,l,hregister);
  624. {$ifndef x86}
  625. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  626. l.size:=dst_size;
  627. {$endif not x86}
  628. end;
  629. end;
  630. if (l.loc <> LOC_CREGISTER) or
  631. not maybeconst then
  632. location_reset(l,LOC_REGISTER,dst_size)
  633. else
  634. location_reset(l,LOC_CREGISTER,dst_size);
  635. l.register:=hregister;
  636. { Release temp when it was a reference }
  637. if oldloc.loc=LOC_REFERENCE then
  638. location_freetemp(list,oldloc);
  639. end;
  640. {$endif not cpu64bitalu}
  641. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  642. var
  643. reg : tregister;
  644. href : treference;
  645. begin
  646. if (l.loc<>LOC_FPUREGISTER) and
  647. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  648. begin
  649. { if it's in an mm register, store to memory first }
  650. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  651. begin
  652. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  653. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  654. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  655. l.reference:=href;
  656. end;
  657. reg:=cg.getfpuregister(list,l.size);
  658. cg.a_loadfpu_loc_reg(list,l.size,l,reg);
  659. location_freetemp(list,l);
  660. location_reset(l,LOC_FPUREGISTER,l.size);
  661. l.register:=reg;
  662. end;
  663. end;
  664. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  665. var
  666. reg : tregister;
  667. href : treference;
  668. newsize : tcgsize;
  669. begin
  670. if (l.loc<>LOC_MMREGISTER) and
  671. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  672. begin
  673. { if it's in an fpu register, store to memory first }
  674. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  675. begin
  676. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  677. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,href);
  678. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  679. l.reference:=href;
  680. end;
  681. {$ifndef cpu64bitalu}
  682. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  683. (l.size in [OS_64,OS_S64]) then
  684. begin
  685. reg:=cg.getmmregister(list,OS_F64);
  686. cg64.a_loadmm_intreg64_reg(list,OS_F64,l.register64,reg);
  687. l.size:=OS_F64
  688. end
  689. else
  690. {$endif not cpu64bitalu}
  691. begin
  692. { on ARM, CFP values may be located in integer registers,
  693. and its second_int_to_real() also uses this routine to
  694. force integer (memory) values in an mmregister }
  695. if (l.size in [OS_32,OS_S32]) then
  696. newsize:=OS_F32
  697. else if (l.size in [OS_64,OS_S64]) then
  698. newsize:=OS_F64
  699. else
  700. newsize:=l.size;
  701. reg:=cg.getmmregister(list,newsize);
  702. cg.a_loadmm_loc_reg(list,newsize,l,reg,mms_movescalar);
  703. l.size:=newsize;
  704. end;
  705. location_freetemp(list,l);
  706. location_reset(l,LOC_MMREGISTER,l.size);
  707. l.register:=reg;
  708. end;
  709. end;
  710. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  711. var
  712. tmpreg: tregister;
  713. begin
  714. if (setbase<>0) then
  715. begin
  716. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  717. internalerror(2007091502);
  718. { subtract the setbase }
  719. case l.loc of
  720. LOC_CREGISTER:
  721. begin
  722. tmpreg := cg.getintregister(list,l.size);
  723. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  724. l.loc:=LOC_REGISTER;
  725. l.register:=tmpreg;
  726. end;
  727. LOC_REGISTER:
  728. begin
  729. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  730. end;
  731. end;
  732. end;
  733. end;
  734. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  735. var
  736. reg : tregister;
  737. begin
  738. if (l.loc<>LOC_MMREGISTER) and
  739. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  740. begin
  741. reg:=cg.getmmregister(list,OS_VECTOR);
  742. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  743. location_freetemp(list,l);
  744. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  745. l.register:=reg;
  746. end;
  747. end;
  748. procedure location_force_mem(list:TAsmList;var l:tlocation);
  749. var
  750. r : treference;
  751. begin
  752. case l.loc of
  753. LOC_FPUREGISTER,
  754. LOC_CFPUREGISTER :
  755. begin
  756. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  757. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,r);
  758. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  759. l.reference:=r;
  760. end;
  761. LOC_MMREGISTER,
  762. LOC_CMMREGISTER:
  763. begin
  764. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  765. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  766. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  767. l.reference:=r;
  768. end;
  769. LOC_CONSTANT,
  770. LOC_REGISTER,
  771. LOC_CREGISTER :
  772. begin
  773. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  774. {$ifndef cpu64bitalu}
  775. if l.size in [OS_64,OS_S64] then
  776. cg64.a_load64_loc_ref(list,l,r)
  777. else
  778. {$endif not cpu64bitalu}
  779. cg.a_load_loc_ref(list,l.size,l,r);
  780. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  781. l.reference:=r;
  782. end;
  783. LOC_SUBSETREG,
  784. LOC_CSUBSETREG,
  785. LOC_SUBSETREF,
  786. LOC_CSUBSETREF:
  787. begin
  788. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  789. cg.a_load_loc_ref(list,l.size,l,r);
  790. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  791. l.reference:=r;
  792. end;
  793. LOC_CREFERENCE,
  794. LOC_REFERENCE : ;
  795. else
  796. internalerror(200203219);
  797. end;
  798. end;
  799. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);
  800. begin
  801. case l.loc of
  802. LOC_REGISTER,
  803. LOC_CREGISTER :
  804. begin
  805. if not loadref then
  806. internalerror(200410231);
  807. reference_reset_base(ref,l.register,0,alignment);
  808. end;
  809. LOC_REFERENCE,
  810. LOC_CREFERENCE :
  811. begin
  812. if loadref then
  813. begin
  814. reference_reset_base(ref,cg.getaddressregister(list),0,alignment);
  815. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  816. end
  817. else
  818. ref:=l.reference;
  819. end;
  820. else
  821. internalerror(200309181);
  822. end;
  823. end;
  824. {****************************************************************************
  825. Init/Finalize Code
  826. ****************************************************************************}
  827. procedure copyvalueparas(p:TObject;arg:pointer);
  828. var
  829. href : treference;
  830. hreg : tregister;
  831. list : TAsmList;
  832. hsym : tparavarsym;
  833. l : longint;
  834. localcopyloc : tlocation;
  835. begin
  836. list:=TAsmList(arg);
  837. if (tsym(p).typ=paravarsym) and
  838. (tparavarsym(p).varspez=vs_value) and
  839. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  840. begin
  841. { we have no idea about the alignment at the caller side }
  842. location_get_data_ref(list,tparavarsym(p).initialloc,href,true,1);
  843. if is_open_array(tparavarsym(p).vardef) or
  844. is_array_of_const(tparavarsym(p).vardef) then
  845. begin
  846. { cdecl functions don't have a high pointer so it is not possible to generate
  847. a local copy }
  848. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  849. begin
  850. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  851. if not assigned(hsym) then
  852. internalerror(200306061);
  853. hreg:=cg.getaddressregister(list);
  854. if not is_packed_array(tparavarsym(p).vardef) then
  855. cg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef).elesize,hreg)
  856. else
  857. internalerror(2006080401);
  858. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  859. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).initialloc);
  860. end;
  861. end
  862. else
  863. begin
  864. { Allocate space for the local copy }
  865. l:=tparavarsym(p).getsize;
  866. localcopyloc.loc:=LOC_REFERENCE;
  867. localcopyloc.size:=int_cgsize(l);
  868. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  869. { Copy data }
  870. if is_shortstring(tparavarsym(p).vardef) then
  871. begin
  872. { this code is only executed before the code for the body and the entry/exit code is generated
  873. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  874. }
  875. include(current_procinfo.flags,pi_do_call);
  876. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef).len)
  877. end
  878. else if tparavarsym(p).vardef.typ = variantdef then
  879. begin
  880. { this code is only executed before the code for the body and the entry/exit code is generated
  881. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  882. }
  883. include(current_procinfo.flags,pi_do_call);
  884. cg.g_copyvariant(list,href,localcopyloc.reference)
  885. end
  886. else
  887. begin
  888. { pass proper alignment info }
  889. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  890. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  891. end;
  892. { update localloc of varsym }
  893. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  894. tparavarsym(p).localloc:=localcopyloc;
  895. tparavarsym(p).initialloc:=localcopyloc;
  896. end;
  897. end;
  898. end;
  899. const
  900. {$ifdef cpu64bitalu}
  901. trashintvalues: array[0..nroftrashvalues-1] of aint = ($5555555555555555,aint($AAAAAAAAAAAAAAAA),aint($EFEFEFEFEFEFEFEF),0);
  902. {$else cpu64bitalu}
  903. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55555555,aint($AAAAAAAA),aint($EFEFEFEF),0);
  904. {$endif cpu64bitalu}
  905. procedure trash_reference(list: TAsmList; const ref: treference; size: aint);
  906. var
  907. countreg, valuereg: tregister;
  908. hl: tasmlabel;
  909. trashintval: aint;
  910. tmpref: treference;
  911. begin
  912. trashintval := trashintvalues[localvartrashing];
  913. case size of
  914. 0: ; { empty record }
  915. 1: cg.a_load_const_ref(list,OS_8,byte(trashintval),ref);
  916. 2: cg.a_load_const_ref(list,OS_16,word(trashintval),ref);
  917. 4: cg.a_load_const_ref(list,OS_32,longint(trashintval),ref);
  918. {$ifdef cpu64bitalu}
  919. 8: cg.a_load_const_ref(list,OS_64,int64(trashintval),ref);
  920. {$endif cpu64bitalu}
  921. else
  922. begin
  923. countreg := cg.getintregister(list,OS_ADDR);
  924. valuereg := cg.getintregister(list,OS_8);
  925. cg.a_load_const_reg(list,OS_INT,size,countreg);
  926. cg.a_load_const_reg(list,OS_8,byte(trashintval),valuereg);
  927. current_asmdata.getjumplabel(hl);
  928. tmpref := ref;
  929. if (tmpref.index <> NR_NO) then
  930. internalerror(200607201);
  931. tmpref.index := countreg;
  932. dec(tmpref.offset);
  933. cg.a_label(list,hl);
  934. cg.a_load_reg_ref(list,OS_8,OS_8,valuereg,tmpref);
  935. cg.a_op_const_reg(list,OP_SUB,OS_INT,1,countreg);
  936. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,countreg,hl);
  937. cg.a_reg_sync(list,tmpref.base);
  938. cg.a_reg_sync(list,valuereg);
  939. end;
  940. end;
  941. end;
  942. { trash contents of local variables or parameters (function result) }
  943. procedure trash_variable(p:TObject;arg:pointer);
  944. var
  945. trashintval: aint;
  946. list: TAsmList absolute arg;
  947. begin
  948. if ((tsym(p).typ=localvarsym) or
  949. ((tsym(p).typ=paravarsym) and
  950. (vo_is_funcret in tparavarsym(p).varoptions))) and
  951. not(tabstractnormalvarsym(p).vardef.needs_inittable) and
  952. not(assigned(tabstractnormalvarsym(p).defaultconstsym)) then
  953. begin
  954. trashintval := trashintvalues[localvartrashing];
  955. case tabstractnormalvarsym(p).initialloc.loc of
  956. LOC_CREGISTER :
  957. {$ifopt q+}
  958. {$define overflowon}
  959. {$q-}
  960. {$endif}
  961. cg.a_load_const_reg(list,reg_cgsize(tabstractnormalvarsym(p).initialloc.register),
  962. trashintval and (aword(1) shl (tcgsize2size[reg_cgsize(tabstractnormalvarsym(p).initialloc.register)] * 8) - 1),
  963. tabstractnormalvarsym(p).initialloc.register);
  964. {$ifdef overflowon}
  965. {$undef overflowon}
  966. {$q+}
  967. {$endif}
  968. LOC_REFERENCE :
  969. begin
  970. if ((tsym(p).typ=localvarsym) and
  971. not(vo_is_funcret in tabstractvarsym(p).varoptions)) or
  972. not is_shortstring(tabstractnormalvarsym(p).vardef) then
  973. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  974. tlocalvarsym(p).getsize)
  975. else
  976. { may be an open string, even if is_open_string() returns }
  977. { false (for some helpers in the system unit) }
  978. { an open string has at least size 2 }
  979. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  980. 2);
  981. end;
  982. LOC_CMMREGISTER :
  983. ;
  984. LOC_CFPUREGISTER :
  985. ;
  986. else
  987. internalerror(200410124);
  988. end;
  989. end;
  990. end;
  991. { initializes the regvars from staticsymtable with 0 }
  992. procedure initialize_regvars(p:TObject;arg:pointer);
  993. var
  994. href : treference;
  995. begin
  996. if (tsym(p).typ=staticvarsym) then
  997. begin
  998. { Static variables can have the initialloc only set to LOC_CxREGISTER
  999. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  1000. case tstaticvarsym(p).initialloc.loc of
  1001. LOC_CREGISTER :
  1002. begin
  1003. {$ifndef cpu64bitalu}
  1004. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  1005. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  1006. else
  1007. {$endif not cpu64bitalu}
  1008. cg.a_load_const_reg(TAsmList(arg),reg_cgsize(tstaticvarsym(p).initialloc.register),0,
  1009. tstaticvarsym(p).initialloc.register);
  1010. end;
  1011. LOC_CMMREGISTER :
  1012. { clear the whole register }
  1013. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  1014. tstaticvarsym(p).initialloc.register,
  1015. tstaticvarsym(p).initialloc.register,
  1016. nil);
  1017. LOC_CFPUREGISTER :
  1018. begin
  1019. { initialize fpu regvar by loading from memory }
  1020. reference_reset_symbol(href,
  1021. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  1022. var_align(tstaticvarsym(p).vardef.alignment));
  1023. cg.a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).initialloc.size,
  1024. tstaticvarsym(p).initialloc.size, href, tstaticvarsym(p).initialloc.register);
  1025. end;
  1026. LOC_INVALID :
  1027. ;
  1028. else
  1029. internalerror(200410124);
  1030. end;
  1031. end;
  1032. end;
  1033. { generates the code for initialisation of local data }
  1034. procedure initialize_data(p:TObject;arg:pointer);
  1035. var
  1036. OldAsmList : TAsmList;
  1037. hp : tnode;
  1038. begin
  1039. if (tsym(p).typ = localvarsym) and
  1040. { local (procedure or unit) variables only need initialization if
  1041. they are used }
  1042. ((tabstractvarsym(p).refs>0) or
  1043. { managed return symbols must be inited }
  1044. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  1045. ) and
  1046. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  1047. not(vo_is_external in tabstractvarsym(p).varoptions) and
  1048. not(is_class(tabstractvarsym(p).vardef)) and
  1049. tabstractvarsym(p).vardef.needs_inittable then
  1050. begin
  1051. OldAsmList:=current_asmdata.CurrAsmList;
  1052. current_asmdata.CurrAsmList:=TAsmList(arg);
  1053. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  1054. firstpass(hp);
  1055. secondpass(hp);
  1056. hp.free;
  1057. current_asmdata.CurrAsmList:=OldAsmList;
  1058. end;
  1059. end;
  1060. procedure finalize_sym(asmlist:TAsmList;sym:tsym);
  1061. var
  1062. hp : tnode;
  1063. OldAsmList : TAsmList;
  1064. begin
  1065. include(current_procinfo.flags,pi_needs_implicit_finally);
  1066. OldAsmList:=current_asmdata.CurrAsmList;
  1067. current_asmdata.CurrAsmList:=asmlist;
  1068. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  1069. firstpass(hp);
  1070. secondpass(hp);
  1071. hp.free;
  1072. current_asmdata.CurrAsmList:=OldAsmList;
  1073. end;
  1074. { generates the code for finalisation of local variables }
  1075. procedure finalize_local_vars(p:TObject;arg:pointer);
  1076. begin
  1077. if (tsym(p).typ=localvarsym) and
  1078. (tlocalvarsym(p).refs>0) and
  1079. not(vo_is_external in tlocalvarsym(p).varoptions) and
  1080. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  1081. not(is_class(tlocalvarsym(p).vardef)) and
  1082. tlocalvarsym(p).vardef.needs_inittable then
  1083. finalize_sym(TAsmList(arg),tsym(p));
  1084. end;
  1085. { generates the code for finalization of static symtable and
  1086. all local (static) typed consts }
  1087. procedure finalize_static_data(p:TObject;arg:pointer);
  1088. var
  1089. i : longint;
  1090. pd : tprocdef;
  1091. begin
  1092. case tsym(p).typ of
  1093. staticvarsym :
  1094. begin
  1095. { local (procedure or unit) variables only need finalization
  1096. if they are used
  1097. }
  1098. if ((tstaticvarsym(p).refs>0) or
  1099. { global (unit) variables always need finalization, since
  1100. they may also be used in another unit
  1101. }
  1102. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  1103. (tstaticvarsym(p).varspez<>vs_const) and
  1104. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  1105. not(vo_is_external in tstaticvarsym(p).varoptions) and
  1106. not(is_class(tstaticvarsym(p).vardef)) and
  1107. tstaticvarsym(p).vardef.needs_inittable then
  1108. finalize_sym(TAsmList(arg),tsym(p));
  1109. end;
  1110. procsym :
  1111. begin
  1112. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  1113. begin
  1114. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  1115. if assigned(pd.localst) and
  1116. (pd.procsym=tprocsym(p)) and
  1117. (pd.localst.symtabletype<>staticsymtable) then
  1118. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  1119. end;
  1120. end;
  1121. end;
  1122. end;
  1123. { generates the code for incrementing the reference count of parameters and
  1124. initialize out parameters }
  1125. procedure init_paras(p:TObject;arg:pointer);
  1126. var
  1127. href : treference;
  1128. tmpreg : tregister;
  1129. list : TAsmList;
  1130. needs_inittable,
  1131. do_trashing : boolean;
  1132. begin
  1133. list:=TAsmList(arg);
  1134. if (tsym(p).typ=paravarsym) then
  1135. begin
  1136. needs_inittable :=
  1137. not is_class(tparavarsym(p).vardef) and
  1138. tparavarsym(p).vardef.needs_inittable;
  1139. do_trashing :=
  1140. (localvartrashing <> -1) and
  1141. (not assigned(tparavarsym(p).defaultconstsym)) and
  1142. not needs_inittable;
  1143. case tparavarsym(p).varspez of
  1144. vs_value :
  1145. if needs_inittable then
  1146. begin
  1147. { variants are already handled by the call to fpc_variant_copy_overwrite if
  1148. they are passed by reference }
  1149. if not((tparavarsym(p).vardef.typ=variantdef) and
  1150. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  1151. begin
  1152. location_get_data_ref(list,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  1153. cg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  1154. end;
  1155. end;
  1156. vs_out :
  1157. begin
  1158. if needs_inittable or
  1159. do_trashing then
  1160. begin
  1161. tmpreg:=cg.getaddressregister(list);
  1162. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1163. { we have no idea about the alignment at the callee side,
  1164. and the user also cannot specify "unaligned" here, so
  1165. assume worst case }
  1166. reference_reset_base(href,tmpreg,0,1);
  1167. if do_trashing and
  1168. { needs separate implementation to trash open arrays }
  1169. { since their size is only known at run time }
  1170. not is_special_array(tparavarsym(p).vardef) then
  1171. { may be an open string, even if is_open_string() returns }
  1172. { false (for some helpers in the system unit) }
  1173. if not is_shortstring(tparavarsym(p).vardef) then
  1174. trash_reference(list,href,tparavarsym(p).vardef.size)
  1175. else
  1176. trash_reference(list,href,2);
  1177. if needs_inittable then
  1178. cg.g_initialize(list,tparavarsym(p).vardef,href);
  1179. end;
  1180. end;
  1181. else if do_trashing and
  1182. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  1183. begin
  1184. tmpreg:=cg.getaddressregister(list);
  1185. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1186. { should always have standard alignment. If a function is assigned
  1187. to a non-aligned variable, the optimisation to pass this variable
  1188. directly as hidden function result must/cannot be performed
  1189. (see tcallnode.funcret_can_be_reused)
  1190. }
  1191. reference_reset_base(href,tmpreg,0,
  1192. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  1193. { may be an open string, even if is_open_string() returns }
  1194. { false (for some helpers in the system unit) }
  1195. if not is_shortstring(tparavarsym(p).vardef) then
  1196. trash_reference(list,href,tparavarsym(p).vardef.size)
  1197. else
  1198. { an open string has at least size 2 }
  1199. trash_reference(list,href,2);
  1200. end
  1201. end;
  1202. end;
  1203. end;
  1204. { generates the code for decrementing the reference count of parameters }
  1205. procedure final_paras(p:TObject;arg:pointer);
  1206. var
  1207. list : TAsmList;
  1208. href : treference;
  1209. begin
  1210. if not(tsym(p).typ=paravarsym) then
  1211. exit;
  1212. list:=TAsmList(arg);
  1213. if not is_class(tparavarsym(p).vardef) and
  1214. tparavarsym(p).vardef.needs_inittable then
  1215. begin
  1216. if (tparavarsym(p).varspez=vs_value) then
  1217. begin
  1218. include(current_procinfo.flags,pi_needs_implicit_finally);
  1219. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  1220. cg.g_decrrefcount(list,tparavarsym(p).vardef,href);
  1221. end;
  1222. end;
  1223. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  1224. if (tparavarsym(p).varspez=vs_value) and
  1225. (is_open_array(tparavarsym(p).vardef) or
  1226. is_array_of_const(tparavarsym(p).vardef)) then
  1227. begin
  1228. { cdecl functions don't have a high pointer so it is not possible to generate
  1229. a local copy }
  1230. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1231. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  1232. end;
  1233. end;
  1234. { Initialize temp ansi/widestrings,interfaces }
  1235. procedure inittempvariables(list:TAsmList);
  1236. var
  1237. hp : ptemprecord;
  1238. href : treference;
  1239. begin
  1240. hp:=tg.templist;
  1241. while assigned(hp) do
  1242. begin
  1243. if assigned(hp^.def) and
  1244. hp^.def.needs_inittable then
  1245. begin
  1246. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1247. cg.g_initialize(list,hp^.def,href);
  1248. end;
  1249. hp:=hp^.next;
  1250. end;
  1251. end;
  1252. procedure finalizetempvariables(list:TAsmList);
  1253. var
  1254. hp : ptemprecord;
  1255. href : treference;
  1256. begin
  1257. hp:=tg.templist;
  1258. while assigned(hp) do
  1259. begin
  1260. if assigned(hp^.def) and
  1261. hp^.def.needs_inittable then
  1262. begin
  1263. include(current_procinfo.flags,pi_needs_implicit_finally);
  1264. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1265. cg.g_finalize(list,hp^.def,href);
  1266. end;
  1267. hp:=hp^.next;
  1268. end;
  1269. end;
  1270. procedure gen_load_return_value(list:TAsmList);
  1271. var
  1272. href : treference;
  1273. ressym : tabstractnormalvarsym;
  1274. resloc,
  1275. restmploc : tlocation;
  1276. hreg : tregister;
  1277. funcretloc : tlocation;
  1278. begin
  1279. { Is the loading needed? }
  1280. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1281. (
  1282. (po_assembler in current_procinfo.procdef.procoptions) and
  1283. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1284. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1285. ) then
  1286. exit;
  1287. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1288. { constructors return self }
  1289. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1290. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  1291. else
  1292. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1293. if (ressym.refs>0) or (ressym.vardef.needs_inittable) then
  1294. begin
  1295. {$ifdef OLDREGVARS}
  1296. case ressym.localloc.loc of
  1297. LOC_CFPUREGISTER,
  1298. LOC_FPUREGISTER:
  1299. begin
  1300. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1301. restmploc.register:=ressym.localloc.register;
  1302. end;
  1303. LOC_CREGISTER,
  1304. LOC_REGISTER:
  1305. begin
  1306. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1307. restmploc.register:=ressym.localloc.register;
  1308. end;
  1309. LOC_MMREGISTER:
  1310. begin
  1311. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1312. restmploc.register:=ressym.localloc.register;
  1313. end;
  1314. LOC_REFERENCE:
  1315. begin
  1316. location_reset_ref(restmploc,LOC_REFERENCE,funcretloc^.size,0);
  1317. restmploc.reference:=ressym.localloc.reference;
  1318. end;
  1319. else
  1320. internalerror(200309184);
  1321. end;
  1322. {$else}
  1323. restmploc:=ressym.localloc;
  1324. {$endif}
  1325. { Here, we return the function result. In most architectures, the value is
  1326. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1327. function returns in a register and the caller receives it in an other one }
  1328. case funcretloc.loc of
  1329. LOC_REGISTER:
  1330. begin
  1331. {$ifdef cpu64bitaddr}
  1332. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_128,OS_S128] then
  1333. begin
  1334. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1335. if resloc.loc<>LOC_REGISTER then
  1336. internalerror(200409141);
  1337. { Load low and high register separate to generate better register
  1338. allocation info }
  1339. if getsupreg(resloc.register)<first_int_imreg then
  1340. begin
  1341. cg.getcpuregister(list,resloc.register);
  1342. end;
  1343. case restmploc.loc of
  1344. LOC_REFERENCE :
  1345. begin
  1346. href:=restmploc.reference;
  1347. if target_info.endian=ENDIAN_BIG then
  1348. inc(href.offset,8);
  1349. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.register);
  1350. end;
  1351. LOC_CREGISTER :
  1352. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.register,resloc.register);
  1353. else
  1354. internalerror(200409203);
  1355. end;
  1356. if getsupreg(resloc.registerhi)<first_int_imreg then
  1357. begin
  1358. cg.getcpuregister(list,resloc.registerhi);
  1359. end;
  1360. case restmploc.loc of
  1361. LOC_REFERENCE :
  1362. begin
  1363. href:=restmploc.reference;
  1364. if target_info.endian=ENDIAN_LITTLE then
  1365. inc(href.offset,8);
  1366. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.registerhi);
  1367. end;
  1368. LOC_CREGISTER :
  1369. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.registerhi,resloc.registerhi);
  1370. else
  1371. internalerror(200409204);
  1372. end;
  1373. end
  1374. else
  1375. { this code is for structures etc. being returned in registers and having odd sizes }
  1376. if (current_procinfo.procdef.funcretloc[calleeside].size=OS_64) and
  1377. (restmploc.size<>OS_64) then
  1378. begin
  1379. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1380. if resloc.loc<>LOC_REGISTER then
  1381. internalerror(200409141);
  1382. { Load low and high register separate to generate better register
  1383. allocation info }
  1384. if getsupreg(resloc.register)<first_int_imreg then
  1385. begin
  1386. cg.getcpuregister(list,resloc.register);
  1387. end;
  1388. case restmploc.loc of
  1389. LOC_REFERENCE :
  1390. begin
  1391. href:=restmploc.reference;
  1392. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.register);
  1393. end;
  1394. LOC_CREGISTER :
  1395. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.register,resloc.register);
  1396. else
  1397. internalerror(200409203);
  1398. end;
  1399. end
  1400. else
  1401. {$else cpu64bitaddr}
  1402. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1403. begin
  1404. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1405. if resloc.loc<>LOC_REGISTER then
  1406. internalerror(200409141);
  1407. { Load low and high register separate to generate better register
  1408. allocation info }
  1409. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1410. begin
  1411. cg.getcpuregister(list,resloc.register64.reglo);
  1412. end;
  1413. case restmploc.loc of
  1414. LOC_REFERENCE :
  1415. begin
  1416. href:=restmploc.reference;
  1417. if target_info.endian=ENDIAN_BIG then
  1418. inc(href.offset,4);
  1419. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1420. end;
  1421. LOC_CREGISTER :
  1422. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1423. LOC_CMMREGISTER :
  1424. { perform the whole move at once below, both result
  1425. registers are required (and since restmploc is an mmreg
  1426. and resloc intregs, they don't conflict anyway) }
  1427. ;
  1428. else
  1429. internalerror(200409203);
  1430. end;
  1431. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1432. begin
  1433. cg.getcpuregister(list,resloc.register64.reghi);
  1434. end;
  1435. case restmploc.loc of
  1436. LOC_REFERENCE :
  1437. begin
  1438. href:=restmploc.reference;
  1439. if target_info.endian=ENDIAN_LITTLE then
  1440. inc(href.offset,4);
  1441. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1442. end;
  1443. LOC_CREGISTER :
  1444. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1445. LOC_CMMREGISTER :
  1446. cg64.a_loadmm_reg_intreg64(list,restmploc.size,restmploc.register,resloc.register64);
  1447. else
  1448. internalerror(200409204);
  1449. end;
  1450. end
  1451. else
  1452. {$endif not cpu64bitaddr}
  1453. { this code is for structures etc. being returned in registers and having odd sizes }
  1454. if (current_procinfo.procdef.funcretloc[calleeside].size=OS_32) and
  1455. not(restmploc.size in [OS_S32,OS_32]) then
  1456. begin
  1457. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1458. if resloc.loc<>LOC_REGISTER then
  1459. internalerror(200409141);
  1460. { Load low and high register separate to generate better register
  1461. allocation info }
  1462. if getsupreg(resloc.register)<first_int_imreg then
  1463. begin
  1464. cg.getcpuregister(list,resloc.register);
  1465. end;
  1466. case restmploc.loc of
  1467. LOC_REFERENCE :
  1468. begin
  1469. href:=restmploc.reference;
  1470. resloc.register:=cg.makeregsize(list,resloc.register,OS_32);
  1471. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register);
  1472. end;
  1473. LOC_CREGISTER :
  1474. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register,resloc.register);
  1475. LOC_CMMREGISTER :
  1476. cg.a_loadmm_reg_intreg(list,restmploc.size,resloc.size,restmploc.register,resloc.register,mms_movescalar);
  1477. else
  1478. internalerror(200409203);
  1479. end;
  1480. end
  1481. else
  1482. begin
  1483. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1484. if getsupreg(funcretloc.register)<first_int_imreg then
  1485. begin
  1486. cg.getcpuregister(list,funcretloc.register);
  1487. end;
  1488. { it could be that a structure is passed in memory but the function is expected to
  1489. return a pointer to this memory }
  1490. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1491. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1492. else
  1493. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1494. end;
  1495. end;
  1496. LOC_FPUREGISTER:
  1497. begin
  1498. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1499. begin
  1500. cg.getcpuregister(list,funcretloc.register);
  1501. end;
  1502. { we can't do direct moves between fpu and mm registers }
  1503. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1504. location_force_fpureg(list,restmploc,false);
  1505. cg.a_loadfpu_loc_reg(list,funcretloc.size,restmploc,funcretloc.register);
  1506. end;
  1507. LOC_MMREGISTER:
  1508. begin
  1509. if getsupreg(funcretloc.register)<first_mm_imreg then
  1510. begin
  1511. cg.getcpuregister(list,funcretloc.register);
  1512. end;
  1513. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1514. end;
  1515. LOC_INVALID,
  1516. LOC_REFERENCE:
  1517. ;
  1518. else
  1519. internalerror(200405025);
  1520. end;
  1521. end
  1522. {$ifdef x86}
  1523. else
  1524. begin
  1525. { the caller will pop a value off the cpu stack }
  1526. if (funcretloc.loc = LOC_FPUREGISTER) then
  1527. list.concat(taicpu.op_none(A_FLDZ));
  1528. end;
  1529. {$endif x86}
  1530. end;
  1531. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1532. begin
  1533. case sym.initialloc.loc of
  1534. LOC_CREGISTER:
  1535. begin
  1536. {$ifndef cpu64bitalu}
  1537. if sym.initialloc.size in [OS_64,OS_S64] then
  1538. begin
  1539. sym.initialloc.register64.reglo:=cg.getintregister(list,OS_32);
  1540. sym.initialloc.register64.reghi:=cg.getintregister(list,OS_32);
  1541. end
  1542. else
  1543. {$endif cpu64bitalu}
  1544. sym.initialloc.register:=cg.getintregister(list,sym.initialloc.size);
  1545. end;
  1546. LOC_CFPUREGISTER:
  1547. begin
  1548. sym.initialloc.register:=cg.getfpuregister(list,sym.initialloc.size);
  1549. end;
  1550. LOC_CMMREGISTER:
  1551. begin
  1552. sym.initialloc.register:=cg.getmmregister(list,sym.initialloc.size);
  1553. end;
  1554. end;
  1555. if (pi_has_goto in current_procinfo.flags) then
  1556. begin
  1557. { Allocate register already, to prevent first allocation to be
  1558. inside a loop }
  1559. {$ifndef cpu64bitalu}
  1560. if sym.initialloc.size in [OS_64,OS_S64] then
  1561. begin
  1562. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  1563. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  1564. end
  1565. else
  1566. {$endif not cpu64bitalu}
  1567. cg.a_reg_sync(list,sym.initialloc.register);
  1568. end;
  1569. sym.localloc:=sym.initialloc;
  1570. end;
  1571. procedure gen_load_para_value(list:TAsmList);
  1572. procedure get_para(const paraloc:TCGParaLocation);
  1573. begin
  1574. case paraloc.loc of
  1575. LOC_REGISTER :
  1576. begin
  1577. if getsupreg(paraloc.register)<first_int_imreg then
  1578. cg.getcpuregister(list,paraloc.register);
  1579. end;
  1580. LOC_MMREGISTER :
  1581. begin
  1582. if getsupreg(paraloc.register)<first_mm_imreg then
  1583. cg.getcpuregister(list,paraloc.register);
  1584. end;
  1585. LOC_FPUREGISTER :
  1586. begin
  1587. if getsupreg(paraloc.register)<first_fpu_imreg then
  1588. cg.getcpuregister(list,paraloc.register);
  1589. end;
  1590. end;
  1591. end;
  1592. procedure unget_para(const paraloc:TCGParaLocation);
  1593. begin
  1594. case paraloc.loc of
  1595. LOC_REGISTER :
  1596. begin
  1597. if getsupreg(paraloc.register)<first_int_imreg then
  1598. cg.ungetcpuregister(list,paraloc.register);
  1599. end;
  1600. LOC_MMREGISTER :
  1601. begin
  1602. if getsupreg(paraloc.register)<first_mm_imreg then
  1603. cg.ungetcpuregister(list,paraloc.register);
  1604. end;
  1605. LOC_FPUREGISTER :
  1606. begin
  1607. if getsupreg(paraloc.register)<first_fpu_imreg then
  1608. cg.ungetcpuregister(list,paraloc.register);
  1609. end;
  1610. end;
  1611. end;
  1612. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint; alignment: longint);
  1613. var
  1614. href : treference;
  1615. begin
  1616. case paraloc.loc of
  1617. LOC_REGISTER :
  1618. begin
  1619. {$IFDEF POWERPC64}
  1620. if (paraloc.shiftval <> 0) then
  1621. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1622. {$ENDIF POWERPC64}
  1623. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1624. end;
  1625. LOC_MMREGISTER :
  1626. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1627. LOC_FPUREGISTER :
  1628. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1629. LOC_REFERENCE :
  1630. begin
  1631. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,alignment);
  1632. { use concatcopy, because it can also be a float which fails when
  1633. load_ref_ref is used. Don't copy data when the references are equal }
  1634. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1635. cg.g_concatcopy(list,href,ref,sizeleft);
  1636. end;
  1637. else
  1638. internalerror(2002081302);
  1639. end;
  1640. end;
  1641. procedure gen_load_reg(const paraloc:TCGParaLocation; regsize: tcgsize; reg:tregister; alignment: longint);
  1642. var
  1643. href : treference;
  1644. begin
  1645. case paraloc.loc of
  1646. LOC_REGISTER :
  1647. begin
  1648. case getregtype(reg) of
  1649. R_INTREGISTER:
  1650. cg.a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1651. R_MMREGISTER:
  1652. cg.a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1653. else
  1654. internalerror(2009112422);
  1655. end;
  1656. end;
  1657. LOC_MMREGISTER :
  1658. cg.a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1659. LOC_FPUREGISTER :
  1660. cg.a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1661. LOC_REFERENCE :
  1662. begin
  1663. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,alignment);
  1664. case getregtype(reg) of
  1665. R_INTREGISTER :
  1666. cg.a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1667. R_FPUREGISTER :
  1668. cg.a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1669. R_MMREGISTER :
  1670. { not paraloc.size, because it may be OS_64 instead of
  1671. OS_F64 in case the parameter is passed using integer
  1672. conventions (e.g., on ARM) }
  1673. cg.a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1674. else
  1675. internalerror(2004101012);
  1676. end;
  1677. end;
  1678. else
  1679. internalerror(2002081302);
  1680. end;
  1681. end;
  1682. var
  1683. i : longint;
  1684. currpara : tparavarsym;
  1685. paraloc : pcgparalocation;
  1686. href : treference;
  1687. sizeleft : aint;
  1688. {$if defined(sparc) or defined(arm)}
  1689. tempref : treference;
  1690. {$endif sparc}
  1691. {$ifndef cpu64bitalu}
  1692. reg64: tregister64;
  1693. {$endif not cpu64bitalu}
  1694. begin
  1695. if (po_assembler in current_procinfo.procdef.procoptions) then
  1696. exit;
  1697. { Allocate registers used by parameters }
  1698. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1699. begin
  1700. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1701. paraloc:=currpara.paraloc[calleeside].location;
  1702. while assigned(paraloc) do
  1703. begin
  1704. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1705. get_para(paraloc^);
  1706. paraloc:=paraloc^.next;
  1707. end;
  1708. end;
  1709. { Copy parameters to local references/registers }
  1710. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1711. begin
  1712. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1713. paraloc:=currpara.paraloc[calleeside].location;
  1714. { skip e.g. empty records }
  1715. if not assigned(paraloc) then
  1716. internalerror(200408203);
  1717. if (paraloc^.loc = LOC_VOID) then
  1718. continue;
  1719. case currpara.initialloc.loc of
  1720. LOC_REFERENCE :
  1721. begin
  1722. { If the parameter location is reused we don't need to copy
  1723. anything }
  1724. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1725. begin
  1726. href:=currpara.initialloc.reference;
  1727. sizeleft:=currpara.paraloc[calleeside].intsize;
  1728. while assigned(paraloc) do
  1729. begin
  1730. if (paraloc^.size=OS_NO) then
  1731. begin
  1732. { Can only be a reference that contains the rest
  1733. of the parameter }
  1734. if (paraloc^.loc<>LOC_REFERENCE) or
  1735. assigned(paraloc^.next) then
  1736. internalerror(2005013010);
  1737. gen_load_ref(paraloc^,href,sizeleft,currpara.initialloc.reference.alignment);
  1738. inc(href.offset,sizeleft);
  1739. sizeleft:=0;
  1740. end
  1741. else
  1742. begin
  1743. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size],currpara.initialloc.reference.alignment);
  1744. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1745. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1746. end;
  1747. unget_para(paraloc^);
  1748. paraloc:=paraloc^.next;
  1749. end;
  1750. end;
  1751. end;
  1752. LOC_CREGISTER :
  1753. begin
  1754. {$ifndef cpu64bitalu}
  1755. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1756. is_64bit(currpara.vardef) then
  1757. begin
  1758. case paraloc^.loc of
  1759. LOC_REGISTER:
  1760. begin
  1761. if not assigned(paraloc^.next) then
  1762. internalerror(200410104);
  1763. if (target_info.endian=ENDIAN_BIG) then
  1764. begin
  1765. { paraloc^ -> high
  1766. paraloc^.next -> low }
  1767. unget_para(paraloc^);
  1768. gen_alloc_regvar(list,currpara);
  1769. { reg->reg, alignment is irrelevant }
  1770. gen_load_reg(paraloc^,OS_32,currpara.initialloc.register64.reghi,4);
  1771. unget_para(paraloc^.next^);
  1772. gen_load_reg(paraloc^.next^,OS_32,currpara.initialloc.register64.reglo,4);
  1773. end
  1774. else
  1775. begin
  1776. { paraloc^ -> low
  1777. paraloc^.next -> high }
  1778. unget_para(paraloc^);
  1779. gen_alloc_regvar(list,currpara);
  1780. gen_load_reg(paraloc^,OS_32,currpara.initialloc.register64.reglo,4);
  1781. unget_para(paraloc^.next^);
  1782. gen_load_reg(paraloc^.next^,OS_32,currpara.initialloc.register64.reghi,4);
  1783. end;
  1784. end;
  1785. LOC_REFERENCE:
  1786. begin
  1787. gen_alloc_regvar(list,currpara);
  1788. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,currpara.paraloc[calleeside].alignment);
  1789. cg64.a_load64_ref_reg(list,href,currpara.initialloc.register64);
  1790. unget_para(paraloc^);
  1791. end;
  1792. else
  1793. internalerror(2005101501);
  1794. end
  1795. end
  1796. else
  1797. {$endif not cpu64bitalu}
  1798. begin
  1799. if assigned(paraloc^.next) then
  1800. internalerror(200410105);
  1801. unget_para(paraloc^);
  1802. gen_alloc_regvar(list,currpara);
  1803. gen_load_reg(paraloc^,currpara.initialloc.size,currpara.initialloc.register,sizeof(aint));
  1804. end;
  1805. end;
  1806. LOC_CFPUREGISTER :
  1807. begin
  1808. {$if defined(sparc) or defined(arm)}
  1809. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1810. we need a temp }
  1811. sizeleft := TCGSize2Size[currpara.initialloc.size];
  1812. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1813. href:=tempref;
  1814. while assigned(paraloc) do
  1815. begin
  1816. unget_para(paraloc^);
  1817. gen_load_ref(paraloc^,href,sizeleft,currpara.initialloc.reference.alignment);
  1818. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1819. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1820. paraloc:=paraloc^.next;
  1821. end;
  1822. gen_alloc_regvar(list,currpara);
  1823. cg.a_loadfpu_ref_reg(list,currpara.initialloc.size,currpara.initialloc.size,tempref,currpara.initialloc.register);
  1824. tg.UnGetTemp(list,tempref);
  1825. {$else sparc}
  1826. unget_para(paraloc^);
  1827. gen_alloc_regvar(list,currpara);
  1828. { from register to register -> alignment is irrelevant }
  1829. gen_load_reg(paraloc^,currpara.initialloc.size,currpara.initialloc.register,0);
  1830. if assigned(paraloc^.next) then
  1831. internalerror(200410109);
  1832. {$endif sparc}
  1833. end;
  1834. LOC_CMMREGISTER :
  1835. begin
  1836. {$ifndef cpu64bitalu}
  1837. { ARM vfp floats are passed in integer registers }
  1838. if (currpara.paraloc[calleeside].size=OS_F64) and
  1839. (paraloc^.size in [OS_32,OS_S32]) and
  1840. use_vectorfpu(currpara.vardef) then
  1841. begin
  1842. { we need 2x32bit reg }
  1843. if not assigned(paraloc^.next) or
  1844. assigned(paraloc^.next^.next) then
  1845. internalerror(2009112421);
  1846. unget_para(paraloc^);
  1847. unget_para(paraloc^.next^);
  1848. gen_alloc_regvar(list,currpara);
  1849. if (target_info.endian=endian_big) then
  1850. { paraloc^ -> high
  1851. paraloc^.next -> low }
  1852. reg64:=joinreg64(paraloc^.next^.register,paraloc^.register)
  1853. else
  1854. reg64:=joinreg64(paraloc^.register,paraloc^.next^.register);
  1855. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,currpara.initialloc.register);
  1856. end
  1857. else
  1858. {$endif not cpu64bitalu}
  1859. begin
  1860. unget_para(paraloc^);
  1861. gen_alloc_regvar(list,currpara);
  1862. { from register to register -> alignment is irrelevant }
  1863. gen_load_reg(paraloc^,currpara.initialloc.size,currpara.initialloc.register,0);
  1864. { data could come in two memory locations, for now
  1865. we simply ignore the sanity check (FK)
  1866. if assigned(paraloc^.next) then
  1867. internalerror(200410108);
  1868. }
  1869. end;
  1870. end;
  1871. end;
  1872. end;
  1873. { generate copies of call by value parameters, must be done before
  1874. the initialization and body is parsed because the refcounts are
  1875. incremented using the local copies }
  1876. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1877. {$ifdef powerpc}
  1878. { unget the register that contains the stack pointer before the procedure entry, }
  1879. { which is used to access the parameters in their original callee-side location }
  1880. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1881. cg.a_reg_dealloc(list,NR_R12);
  1882. {$endif powerpc}
  1883. {$ifdef powerpc64}
  1884. { unget the register that contains the stack pointer before the procedure entry, }
  1885. { which is used to access the parameters in their original callee-side location }
  1886. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1887. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1888. {$endif powerpc64}
  1889. end;
  1890. procedure gen_initialize_code(list:TAsmList);
  1891. begin
  1892. { initialize local data like ansistrings }
  1893. case current_procinfo.procdef.proctypeoption of
  1894. potype_unitinit:
  1895. begin
  1896. { this is also used for initialization of variables in a
  1897. program which does not have a globalsymtable }
  1898. if assigned(current_module.globalsymtable) then
  1899. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1900. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1901. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1902. end;
  1903. { units have seperate code for initilization and finalization }
  1904. potype_unitfinalize: ;
  1905. { program init/final is generated in separate procedure }
  1906. potype_proginit:
  1907. begin
  1908. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1909. end;
  1910. else
  1911. begin
  1912. if (localvartrashing <> -1) and
  1913. not(po_assembler in current_procinfo.procdef.procoptions) then
  1914. current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  1915. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1916. end;
  1917. end;
  1918. { initialisizes temp. ansi/wide string data }
  1919. inittempvariables(list);
  1920. { initialize ansi/widesstring para's }
  1921. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1922. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1923. {$ifdef OLDREGVARS}
  1924. load_regvars(list,nil);
  1925. {$endif OLDREGVARS}
  1926. end;
  1927. procedure gen_finalize_code(list:TAsmList);
  1928. begin
  1929. {$ifdef OLDREGVARS}
  1930. cleanup_regvars(list);
  1931. {$endif OLDREGVARS}
  1932. { finalize temporary data }
  1933. finalizetempvariables(list);
  1934. { finalize local data like ansistrings}
  1935. case current_procinfo.procdef.proctypeoption of
  1936. potype_unitfinalize:
  1937. begin
  1938. { this is also used for initialization of variables in a
  1939. program which does not have a globalsymtable }
  1940. if assigned(current_module.globalsymtable) then
  1941. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1942. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1943. end;
  1944. { units/progs have separate code for initialization and finalization }
  1945. potype_unitinit: ;
  1946. { program init/final is generated in separate procedure }
  1947. potype_proginit: ;
  1948. else
  1949. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  1950. end;
  1951. { finalize paras data }
  1952. if assigned(current_procinfo.procdef.parast) and
  1953. not(po_assembler in current_procinfo.procdef.procoptions) then
  1954. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  1955. end;
  1956. procedure gen_entry_code(list:TAsmList);
  1957. begin
  1958. { the actual profile code can clobber some registers,
  1959. therefore if the context must be saved, do it before
  1960. the actual call to the profile code
  1961. }
  1962. if (cs_profile in current_settings.moduleswitches) and
  1963. not(po_assembler in current_procinfo.procdef.procoptions) then
  1964. begin
  1965. { non-win32 can call mcout even in main }
  1966. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1967. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1968. begin
  1969. cg.g_profilecode(list);
  1970. end;
  1971. end;
  1972. { call startup helpers from main program }
  1973. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1974. begin
  1975. { initialize units }
  1976. cg.allocallcpuregisters(list);
  1977. if not(current_module.islibrary) then
  1978. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  1979. else
  1980. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  1981. cg.deallocallcpuregisters(list);
  1982. end;
  1983. list.concat(Tai_force_line.Create);
  1984. {$ifdef OLDREGVARS}
  1985. load_regvars(list,nil);
  1986. {$endif OLDREGVARS}
  1987. end;
  1988. procedure gen_exit_code(list:TAsmList);
  1989. begin
  1990. { call __EXIT for main program }
  1991. if (not DLLsource) and
  1992. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1993. cg.a_call_name(list,'FPC_DO_EXIT',false);
  1994. end;
  1995. {****************************************************************************
  1996. Entry/Exit
  1997. ****************************************************************************}
  1998. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1999. var
  2000. item : TCmdStrListItem;
  2001. begin
  2002. result:=true;
  2003. if pd.mangledname=s then
  2004. exit;
  2005. item := TCmdStrListItem(pd.aliasnames.first);
  2006. while assigned(item) do
  2007. begin
  2008. if item.str=s then
  2009. exit;
  2010. item := TCmdStrListItem(item.next);
  2011. end;
  2012. result:=false;
  2013. end;
  2014. procedure alloc_proc_symbol(pd: tprocdef);
  2015. var
  2016. item : TCmdStrListItem;
  2017. begin
  2018. item := TCmdStrListItem(pd.aliasnames.first);
  2019. while assigned(item) do
  2020. begin
  2021. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  2022. item := TCmdStrListItem(item.next);
  2023. end;
  2024. end;
  2025. procedure gen_proc_symbol(list:TAsmList);
  2026. var
  2027. item,
  2028. previtem : TCmdStrListItem;
  2029. begin
  2030. previtem:=nil;
  2031. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  2032. while assigned(item) do
  2033. begin
  2034. {$ifdef arm}
  2035. if current_settings.cputype in cpu_thumb2 then
  2036. list.concat(tai_thumb_func.create);
  2037. {$endif arm}
  2038. { "double link" all procedure entry symbols via .reference }
  2039. { directives on darwin, because otherwise the linker }
  2040. { sometimes strips the procedure if only on of the symbols }
  2041. { is referenced }
  2042. if assigned(previtem) and
  2043. (target_info.system in systems_darwin) then
  2044. list.concat(tai_directive.create(asd_reference,item.str));
  2045. if (cs_profile in current_settings.moduleswitches) or
  2046. (po_global in current_procinfo.procdef.procoptions) then
  2047. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  2048. else
  2049. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  2050. if assigned(previtem) and
  2051. (target_info.system in systems_darwin) then
  2052. list.concat(tai_directive.create(asd_reference,previtem.str));
  2053. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  2054. list.concat(Tai_function_name.create(item.str));
  2055. previtem:=item;
  2056. item := TCmdStrListItem(item.next);
  2057. end;
  2058. current_procinfo.procdef.procstarttai:=tai(list.last);
  2059. end;
  2060. procedure gen_proc_symbol_end(list:TAsmList);
  2061. begin
  2062. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  2063. current_procinfo.procdef.procendtai:=tai(list.last);
  2064. if (current_module.islibrary) then
  2065. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  2066. exportlib.setinitname(list,current_procinfo.procdef.mangledname);
  2067. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2068. begin
  2069. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  2070. not(current_module.islibrary) then
  2071. begin
  2072. list.concat(tai_section.create(sec_code,'',4));
  2073. list.concat(tai_symbol.createname_global(
  2074. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  2075. { keep argc, argv and envp properly on the stack }
  2076. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  2077. end;
  2078. end;
  2079. end;
  2080. procedure gen_proc_entry_code(list:TAsmList);
  2081. var
  2082. hitemp,
  2083. lotemp : longint;
  2084. begin
  2085. { generate call frame marker for dwarf call frame info }
  2086. current_asmdata.asmcfi.start_frame(list);
  2087. { All temps are know, write offsets used for information }
  2088. if (cs_asm_source in current_settings.globalswitches) then
  2089. begin
  2090. if tg.direction>0 then
  2091. begin
  2092. lotemp:=current_procinfo.tempstart;
  2093. hitemp:=tg.lasttemp;
  2094. end
  2095. else
  2096. begin
  2097. lotemp:=tg.lasttemp;
  2098. hitemp:=current_procinfo.tempstart;
  2099. end;
  2100. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  2101. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  2102. end;
  2103. { generate target specific proc entry code }
  2104. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  2105. end;
  2106. procedure gen_proc_exit_code(list:TAsmList);
  2107. var
  2108. parasize : longint;
  2109. begin
  2110. { c style clearstack does not need to remove parameters from the stack, only the
  2111. return value when it was pushed by arguments }
  2112. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  2113. begin
  2114. parasize:=0;
  2115. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  2116. inc(parasize,sizeof(pint));
  2117. end
  2118. else
  2119. parasize:=current_procinfo.para_stack_size;
  2120. { generate target specific proc exit code }
  2121. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  2122. { release return registers, needed for optimizer }
  2123. if not is_void(current_procinfo.procdef.returndef) then
  2124. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  2125. { end of frame marker for call frame info }
  2126. current_asmdata.asmcfi.end_frame(list);
  2127. end;
  2128. procedure gen_stack_check_size_para(list:TAsmList);
  2129. var
  2130. paraloc1 : tcgpara;
  2131. begin
  2132. paraloc1.init;
  2133. paramanager.getintparaloc(pocall_default,1,paraloc1);
  2134. paramanager.allocparaloc(list,paraloc1);
  2135. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  2136. paramanager.freeparaloc(list,paraloc1);
  2137. paraloc1.done;
  2138. end;
  2139. procedure gen_stack_check_call(list:TAsmList);
  2140. var
  2141. paraloc1 : tcgpara;
  2142. begin
  2143. paraloc1.init;
  2144. { Also alloc the register needed for the parameter }
  2145. paramanager.getintparaloc(pocall_default,1,paraloc1);
  2146. paramanager.allocparaloc(list,paraloc1);
  2147. paramanager.freeparaloc(list,paraloc1);
  2148. { Call the helper }
  2149. cg.allocallcpuregisters(list);
  2150. cg.a_call_name(list,'FPC_STACKCHECK',false);
  2151. cg.deallocallcpuregisters(list);
  2152. paraloc1.done;
  2153. end;
  2154. procedure gen_save_used_regs(list:TAsmList);
  2155. begin
  2156. { Pure assembler routines need to save the registers themselves }
  2157. if (po_assembler in current_procinfo.procdef.procoptions) then
  2158. exit;
  2159. { oldfpccall expects all registers to be destroyed }
  2160. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  2161. cg.g_save_registers(list);
  2162. end;
  2163. procedure gen_restore_used_regs(list:TAsmList);
  2164. begin
  2165. { Pure assembler routines need to save the registers themselves }
  2166. if (po_assembler in current_procinfo.procdef.procoptions) then
  2167. exit;
  2168. { oldfpccall expects all registers to be destroyed }
  2169. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  2170. cg.g_restore_registers(list);
  2171. end;
  2172. {****************************************************************************
  2173. External handling
  2174. ****************************************************************************}
  2175. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  2176. begin
  2177. create_codegen;
  2178. { add the procedure to the al_procedures }
  2179. maybe_new_object_file(list);
  2180. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  2181. list.concat(Tai_align.create(current_settings.alignment.procalign));
  2182. if (po_global in pd.procoptions) then
  2183. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  2184. else
  2185. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  2186. cg.g_external_wrapper(list,pd,externalname);
  2187. destroy_codegen;
  2188. end;
  2189. {****************************************************************************
  2190. Const Data
  2191. ****************************************************************************}
  2192. procedure insertbssdata(sym : tstaticvarsym);
  2193. var
  2194. l : aint;
  2195. varalign : shortint;
  2196. storefilepos : tfileposinfo;
  2197. list : TAsmList;
  2198. sectype : TAsmSectiontype;
  2199. begin
  2200. storefilepos:=current_filepos;
  2201. current_filepos:=sym.fileinfo;
  2202. l:=sym.getsize;
  2203. varalign:=sym.vardef.alignment;
  2204. if (varalign=0) then
  2205. varalign:=var_align_size(l)
  2206. else
  2207. varalign:=var_align(varalign);
  2208. if tf_section_threadvars in target_info.flags then
  2209. begin
  2210. if (vo_is_thread_var in sym.varoptions) then
  2211. begin
  2212. list:=current_asmdata.asmlists[al_threadvars];
  2213. sectype:=sec_threadvar;
  2214. end
  2215. else
  2216. begin
  2217. list:=current_asmdata.asmlists[al_globals];
  2218. sectype:=sec_bss;
  2219. end;
  2220. end
  2221. else
  2222. begin
  2223. if (vo_is_thread_var in sym.varoptions) then
  2224. begin
  2225. inc(l,sizeof(pint));
  2226. { it doesn't help to set a higher alignment, as }
  2227. { the first sizeof(pint) bytes field will offset }
  2228. { everything anyway }
  2229. varalign:=sizeof(pint);
  2230. end;
  2231. list:=current_asmdata.asmlists[al_globals];
  2232. sectype:=sec_bss;
  2233. end;
  2234. maybe_new_object_file(list);
  2235. new_section(list,sectype,lower(sym.mangledname),varalign);
  2236. if (sym.owner.symtabletype=globalsymtable) or
  2237. create_smartlink or
  2238. DLLSource or
  2239. (assigned(current_procinfo) and
  2240. (po_inline in current_procinfo.procdef.procoptions)) or
  2241. (vo_is_public in sym.varoptions) then
  2242. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  2243. else
  2244. list.concat(Tai_datablock.create(sym.mangledname,l));
  2245. current_filepos:=storefilepos;
  2246. end;
  2247. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  2248. procedure setlocalloc(vs:tabstractnormalvarsym);
  2249. begin
  2250. if cs_asm_source in current_settings.globalswitches then
  2251. begin
  2252. case vs.initialloc.loc of
  2253. LOC_REFERENCE :
  2254. begin
  2255. if not assigned(vs.initialloc.reference.symbol) then
  2256. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  2257. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  2258. end;
  2259. end;
  2260. end;
  2261. vs.localloc:=vs.initialloc;
  2262. end;
  2263. var
  2264. i : longint;
  2265. sym : tsym;
  2266. vs : tabstractnormalvarsym;
  2267. isaddr : boolean;
  2268. begin
  2269. for i:=0 to st.SymList.Count-1 do
  2270. begin
  2271. sym:=tsym(st.SymList[i]);
  2272. case sym.typ of
  2273. staticvarsym :
  2274. begin
  2275. vs:=tabstractnormalvarsym(sym);
  2276. { The code in loadnode.pass_generatecode will create the
  2277. LOC_REFERENCE instead for all none register variables. This is
  2278. required because we can't store an asmsymbol in the localloc because
  2279. the asmsymbol is invalid after an unit is compiled. This gives
  2280. problems when this procedure is inlined in an other unit (PFV) }
  2281. if vs.is_regvar(false) then
  2282. begin
  2283. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2284. vs.initialloc.size:=def_cgsize(vs.vardef);
  2285. gen_alloc_regvar(list,vs);
  2286. setlocalloc(vs);
  2287. end;
  2288. end;
  2289. paravarsym :
  2290. begin
  2291. vs:=tabstractnormalvarsym(sym);
  2292. { Parameters passed to assembler procedures need to be kept
  2293. in the original location }
  2294. if (po_assembler in current_procinfo.procdef.procoptions) then
  2295. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  2296. else
  2297. begin
  2298. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  2299. if isaddr then
  2300. vs.initialloc.size:=OS_ADDR
  2301. else
  2302. vs.initialloc.size:=def_cgsize(vs.vardef);
  2303. if vs.is_regvar(isaddr) then
  2304. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  2305. else
  2306. begin
  2307. vs.initialloc.loc:=LOC_REFERENCE;
  2308. { Reuse the parameter location for values to are at a single location on the stack }
  2309. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  2310. begin
  2311. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  2312. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  2313. end
  2314. else
  2315. begin
  2316. if isaddr then
  2317. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  2318. else
  2319. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  2320. end;
  2321. end;
  2322. end;
  2323. setlocalloc(vs);
  2324. end;
  2325. localvarsym :
  2326. begin
  2327. vs:=tabstractnormalvarsym(sym);
  2328. vs.initialloc.size:=def_cgsize(vs.vardef);
  2329. if (m_delphi in current_settings.modeswitches) and
  2330. (po_assembler in current_procinfo.procdef.procoptions) and
  2331. (vo_is_funcret in vs.varoptions) and
  2332. (vs.refs=0) then
  2333. begin
  2334. { not referenced, so don't allocate. Use dummy to }
  2335. { avoid ie's later on because of LOC_INVALID }
  2336. vs.initialloc.loc:=LOC_REGISTER;
  2337. vs.initialloc.size:=OS_INT;
  2338. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  2339. end
  2340. else if vs.is_regvar(false) then
  2341. begin
  2342. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2343. gen_alloc_regvar(list,vs);
  2344. end
  2345. else
  2346. begin
  2347. vs.initialloc.loc:=LOC_REFERENCE;
  2348. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  2349. end;
  2350. setlocalloc(vs);
  2351. end;
  2352. end;
  2353. end;
  2354. end;
  2355. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  2356. begin
  2357. case location.loc of
  2358. LOC_CREGISTER:
  2359. {$ifndef cpu64bitalu}
  2360. if location.size in [OS_64,OS_S64] then
  2361. begin
  2362. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  2363. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  2364. end
  2365. else
  2366. {$endif not cpu64bitalu}
  2367. rv.intregvars.addnodup(getsupreg(location.register));
  2368. LOC_CFPUREGISTER:
  2369. rv.fpuregvars.addnodup(getsupreg(location.register));
  2370. LOC_CMMREGISTER:
  2371. rv.mmregvars.addnodup(getsupreg(location.register));
  2372. end;
  2373. end;
  2374. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  2375. var
  2376. rv: pusedregvars absolute arg;
  2377. begin
  2378. case (n.nodetype) of
  2379. temprefn:
  2380. { We only have to synchronise a tempnode before a loop if it is }
  2381. { not created inside the loop, and only synchronise after the }
  2382. { loop if it's not destroyed inside the loop. If it's created }
  2383. { before the loop and not yet destroyed, then before the loop }
  2384. { is secondpassed tempinfo^.valid will be true, and we get the }
  2385. { correct registers. If it's not destroyed inside the loop, }
  2386. { then after the loop has been secondpassed tempinfo^.valid }
  2387. { be true and we also get the right registers. In other cases, }
  2388. { tempinfo^.valid will be false and so we do not add }
  2389. { unnecessary registers. This way, we don't have to look at }
  2390. { tempcreate and tempdestroy nodes to get this info (JM) }
  2391. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  2392. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  2393. loadn:
  2394. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2395. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  2396. vecn:
  2397. { range checks sometimes need the high parameter }
  2398. if (cs_check_range in current_settings.localswitches) and
  2399. (is_open_array(tvecnode(n).left.resultdef) or
  2400. is_array_of_const(tvecnode(n).left.resultdef)) and
  2401. not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2402. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  2403. end;
  2404. result := fen_true;
  2405. end;
  2406. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  2407. begin
  2408. foreachnodestatic(n,@do_get_used_regvars,@rv);
  2409. end;
  2410. (*
  2411. See comments at declaration of pusedregvarscommon
  2412. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  2413. var
  2414. rv: pusedregvarscommon absolute arg;
  2415. begin
  2416. if (n.nodetype = loadn) and
  2417. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2418. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2419. case loc of
  2420. LOC_CREGISTER:
  2421. { if not yet encountered in this node tree }
  2422. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2423. { but nevertheless already encountered somewhere }
  2424. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2425. { then it's a regvar used in two or more node trees }
  2426. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2427. LOC_CFPUREGISTER:
  2428. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2429. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2430. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2431. LOC_CMMREGISTER:
  2432. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2433. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2434. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2435. end;
  2436. result := fen_true;
  2437. end;
  2438. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2439. begin
  2440. rv.myregvars.intregvars.clear;
  2441. rv.myregvars.fpuregvars.clear;
  2442. rv.myregvars.mmregvars.clear;
  2443. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2444. end;
  2445. *)
  2446. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2447. var
  2448. count: longint;
  2449. begin
  2450. for count := 1 to rv.intregvars.length do
  2451. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2452. for count := 1 to rv.fpuregvars.length do
  2453. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2454. for count := 1 to rv.mmregvars.length do
  2455. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2456. end;
  2457. {*****************************************************************************
  2458. SSA support
  2459. *****************************************************************************}
  2460. type
  2461. preplaceregrec = ^treplaceregrec;
  2462. treplaceregrec = record
  2463. old, new: tregister;
  2464. {$ifndef cpu64bitalu}
  2465. oldhi, newhi: tregister;
  2466. {$endif not cpu64bitalu}
  2467. ressym: tsym;
  2468. end;
  2469. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  2470. var
  2471. rr: preplaceregrec absolute para;
  2472. begin
  2473. result := fen_false;
  2474. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  2475. exit;
  2476. case n.nodetype of
  2477. loadn:
  2478. begin
  2479. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  2480. not assigned(tloadnode(n).left) and
  2481. ((tloadnode(n).symtableentry <> rr^.ressym) or
  2482. not(fc_exit in flowcontrol)
  2483. ) and
  2484. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2485. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  2486. begin
  2487. {$ifndef cpu64bitalu}
  2488. { it's possible a 64 bit location was shifted and/xor typecasted }
  2489. { in a 32 bit value, so only 1 register was left in the location }
  2490. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  2491. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  2492. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  2493. else
  2494. exit;
  2495. {$endif not cpu64bitalu}
  2496. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  2497. result := fen_norecurse_true;
  2498. end;
  2499. end;
  2500. temprefn:
  2501. begin
  2502. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  2503. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2504. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  2505. begin
  2506. {$ifndef cpu64bitalu}
  2507. { it's possible a 64 bit location was shifted and/xor typecasted }
  2508. { in a 32 bit value, so only 1 register was left in the location }
  2509. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  2510. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  2511. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  2512. else
  2513. exit;
  2514. {$endif not cpu64bitalu}
  2515. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  2516. result := fen_norecurse_true;
  2517. end;
  2518. end;
  2519. { optimize the searching a bit }
  2520. derefn,addrn,
  2521. calln,inlinen,casen,
  2522. addn,subn,muln,
  2523. andn,orn,xorn,
  2524. ltn,lten,gtn,gten,equaln,unequaln,
  2525. slashn,divn,shrn,shln,notn,
  2526. inn,
  2527. asn,isn:
  2528. result := fen_norecurse_false;
  2529. end;
  2530. end;
  2531. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  2532. var
  2533. rr: treplaceregrec;
  2534. begin
  2535. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  2536. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  2537. exit;
  2538. rr.old := n.location.register;
  2539. rr.ressym := nil;
  2540. {$ifndef cpu64bitalu}
  2541. rr.oldhi := NR_NO;
  2542. {$endif not cpu64bitalu}
  2543. case n.location.loc of
  2544. LOC_CREGISTER:
  2545. begin
  2546. {$ifndef cpu64bitalu}
  2547. if (n.location.size in [OS_64,OS_S64]) then
  2548. begin
  2549. rr.oldhi := n.location.register64.reghi;
  2550. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2551. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2552. end
  2553. else
  2554. {$endif not cpu64bitalu}
  2555. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  2556. end;
  2557. LOC_CFPUREGISTER:
  2558. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  2559. {$ifdef SUPPORT_MMX}
  2560. LOC_CMMXREGISTER:
  2561. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  2562. {$endif SUPPORT_MMX}
  2563. LOC_CMMREGISTER:
  2564. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  2565. else
  2566. exit;
  2567. end;
  2568. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  2569. assigned(current_procinfo.procdef.funcretsym) and
  2570. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  2571. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2572. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  2573. else
  2574. rr.ressym:=current_procinfo.procdef.funcretsym;
  2575. if not foreachnodestatic(n,@doreplace,@rr) then
  2576. exit;
  2577. if reload then
  2578. case n.location.loc of
  2579. LOC_CREGISTER:
  2580. begin
  2581. {$ifndef cpu64bitalu}
  2582. if (n.location.size in [OS_64,OS_S64]) then
  2583. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  2584. else
  2585. {$endif not cpu64bitalu}
  2586. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2587. end;
  2588. LOC_CFPUREGISTER:
  2589. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2590. {$ifdef SUPPORT_MMX}
  2591. LOC_CMMXREGISTER:
  2592. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  2593. {$endif SUPPORT_MMX}
  2594. LOC_CMMREGISTER:
  2595. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  2596. else
  2597. internalerror(2006090920);
  2598. end;
  2599. { now that we've change the loadn/temp, also change the node result location }
  2600. {$ifndef cpu64bitalu}
  2601. if (n.location.size in [OS_64,OS_S64]) then
  2602. begin
  2603. n.location.register64.reglo := rr.new;
  2604. n.location.register64.reghi := rr.newhi;
  2605. end
  2606. else
  2607. {$endif not cpu64bitalu}
  2608. n.location.register := rr.new;
  2609. end;
  2610. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  2611. var
  2612. i : longint;
  2613. sym : tsym;
  2614. begin
  2615. for i:=0 to st.SymList.Count-1 do
  2616. begin
  2617. sym:=tsym(st.SymList[i]);
  2618. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  2619. begin
  2620. with tabstractnormalvarsym(sym) do
  2621. begin
  2622. { Note: We need to keep the data available in memory
  2623. for the sub procedures that can access local data
  2624. in the parent procedures }
  2625. case localloc.loc of
  2626. LOC_CREGISTER :
  2627. if (pi_has_goto in current_procinfo.flags) then
  2628. {$ifndef cpu64bitalu}
  2629. if def_cgsize(vardef) in [OS_64,OS_S64] then
  2630. begin
  2631. cg.a_reg_sync(list,localloc.register64.reglo);
  2632. cg.a_reg_sync(list,localloc.register64.reghi);
  2633. end
  2634. else
  2635. {$endif not cpu64bitalu}
  2636. cg.a_reg_sync(list,localloc.register);
  2637. LOC_CFPUREGISTER,
  2638. LOC_CMMREGISTER:
  2639. if (pi_has_goto in current_procinfo.flags) then
  2640. cg.a_reg_sync(list,localloc.register);
  2641. LOC_REFERENCE :
  2642. begin
  2643. if typ in [localvarsym,paravarsym] then
  2644. tg.Ungetlocal(list,localloc.reference);
  2645. end;
  2646. end;
  2647. end;
  2648. end;
  2649. end;
  2650. end;
  2651. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2652. var
  2653. i,j : longint;
  2654. tmps : string;
  2655. pd : TProcdef;
  2656. ImplIntf : TImplementedInterface;
  2657. begin
  2658. for i:=0 to _class.ImplementedInterfaces.count-1 do
  2659. begin
  2660. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  2661. if (ImplIntf=ImplIntf.VtblImplIntf) and
  2662. assigned(ImplIntf.ProcDefs) then
  2663. begin
  2664. maybe_new_object_file(list);
  2665. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  2666. begin
  2667. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  2668. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2669. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  2670. { create wrapper code }
  2671. new_section(list,sec_code,tmps,0);
  2672. cg.init_register_allocators;
  2673. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  2674. cg.done_register_allocators;
  2675. end;
  2676. end;
  2677. end;
  2678. end;
  2679. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  2680. var
  2681. i : longint;
  2682. def : tdef;
  2683. begin
  2684. create_codegen;
  2685. for i:=0 to st.DefList.Count-1 do
  2686. begin
  2687. def:=tdef(st.DefList[i]);
  2688. if is_class(def) then
  2689. gen_intf_wrapper(list,tobjectdef(def));
  2690. end;
  2691. destroy_codegen;
  2692. end;
  2693. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2694. var
  2695. href : treference;
  2696. begin
  2697. if is_object(objdef) then
  2698. begin
  2699. case selfloc.loc of
  2700. LOC_CREFERENCE,
  2701. LOC_REFERENCE:
  2702. begin
  2703. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2704. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2705. end;
  2706. else
  2707. internalerror(200305056);
  2708. end;
  2709. end
  2710. else
  2711. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  2712. and the first "field" of an Objective-C class instance is a pointer
  2713. to its "meta-class". }
  2714. begin
  2715. case selfloc.loc of
  2716. LOC_REGISTER:
  2717. begin
  2718. {$ifdef cpu_uses_separate_address_registers}
  2719. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2720. begin
  2721. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2722. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2723. end
  2724. else
  2725. {$endif cpu_uses_separate_address_registers}
  2726. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  2727. end;
  2728. LOC_CREGISTER,
  2729. LOC_CREFERENCE,
  2730. LOC_REFERENCE:
  2731. begin
  2732. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2733. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2734. end;
  2735. else
  2736. internalerror(200305057);
  2737. end;
  2738. end;
  2739. vmtreg:=cg.getaddressregister(list);
  2740. cg.g_maybe_testself(list,href.base);
  2741. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2742. { test validity of VMT }
  2743. if not(is_interface(objdef)) and
  2744. not(is_cppclass(objdef)) and
  2745. not(is_objc_class_or_protocol(objdef)) then
  2746. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2747. end;
  2748. function getprocalign : shortint;
  2749. begin
  2750. { gprof uses 16 byte granularity }
  2751. if (cs_profile in current_settings.moduleswitches) then
  2752. result:=16
  2753. else
  2754. result:=current_settings.alignment.procalign;
  2755. end;
  2756. procedure gen_pic_helpers(list : TAsmList);
  2757. {$ifdef i386}
  2758. var
  2759. href : treference;
  2760. {$endif i386}
  2761. begin
  2762. { if other cpus require such helpers as well, it can be solved more cleanly }
  2763. {$ifdef i386}
  2764. if current_module.requires_ebx_pic_helper then
  2765. begin
  2766. new_section(list,sec_code,'fpc_geteipasebx',0);
  2767. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2768. reference_reset(href,sizeof(pint));
  2769. href.base:=NR_ESP;
  2770. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2771. list.concat(taicpu.op_none(A_RET,S_NO));
  2772. end;
  2773. if current_module.requires_ecx_pic_helper then
  2774. begin
  2775. new_section(list,sec_code,'fpc_geteipasecx',0);
  2776. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2777. reference_reset(href,sizeof(pint));
  2778. href.base:=NR_ESP;
  2779. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2780. list.concat(taicpu.op_none(A_RET,S_NO));
  2781. end;
  2782. {$endif i386}
  2783. end;
  2784. end.