ncgutil.pas 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657
  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 cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  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. { Retrieve the location of the data pointed to in location l, when the location is
  54. a register it is expected to contain the address of the data }
  55. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  56. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  57. procedure alloc_proc_symbol(pd: tprocdef);
  58. procedure gen_proc_symbol(list:TAsmList);
  59. procedure gen_proc_symbol_end(list:TAsmList);
  60. procedure gen_proc_entry_code(list:TAsmList);
  61. procedure gen_proc_exit_code(list:TAsmList);
  62. procedure gen_stack_check_size_para(list:TAsmList);
  63. procedure gen_stack_check_call(list:TAsmList);
  64. procedure gen_save_used_regs(list:TAsmList);
  65. procedure gen_restore_used_regs(list:TAsmList);
  66. procedure gen_initialize_code(list:TAsmList);
  67. procedure gen_finalize_code(list:TAsmList);
  68. procedure gen_entry_code(list:TAsmList);
  69. procedure gen_exit_code(list:TAsmList);
  70. procedure gen_load_para_value(list:TAsmList);
  71. procedure gen_load_return_value(list:TAsmList);
  72. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  73. procedure gen_intf_wrappers(list:TAsmList;st:tsymtable);
  74. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  75. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  76. { adds the regvars used in n and its children to rv.allregvars,
  77. those which were already in rv.allregvars to rv.commonregvars and
  78. uses rv.myregvars as scratch (so that two uses of the same regvar
  79. in a single tree to make it appear in commonregvars). Useful to
  80. find out which regvars are used in two different node trees
  81. (e.g. in the "else" and "then" path, or in various case blocks }
  82. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  83. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  84. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  85. { loadn and change its location to a new register (= SSA) }
  86. procedure maybechangeloadnodereg(var n: tnode);
  87. {#
  88. Allocate the buffers for exception management and setjmp environment.
  89. Return a pointer to these buffers, send them to the utility routine
  90. so they are registered, and then call setjmp.
  91. Then compare the result of setjmp with 0, and if not equal
  92. to zero, then jump to exceptlabel.
  93. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  94. It is to note that this routine may be called *after* the stackframe of a
  95. routine has been called, therefore on machines where the stack cannot
  96. be modified, all temps should be allocated on the heap instead of the
  97. stack.
  98. }
  99. const
  100. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  101. type
  102. texceptiontemps=record
  103. jmpbuf,
  104. envbuf,
  105. reasonbuf : treference;
  106. end;
  107. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  108. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  109. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  110. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  111. procedure insertbssdata(sym : tglobalvarsym);
  112. procedure gen_alloc_symtable(list:TAsmList;st:tsymtable);
  113. procedure gen_free_symtable(list:TAsmList;st:tsymtable);
  114. { rtti and init/final }
  115. procedure generate_rtti(p:Ttypesym);
  116. procedure generate_inittable(p:tsym);
  117. procedure location_free(list: TAsmList; const location : TLocation);
  118. function getprocalign : shortint;
  119. procedure gen_pic_helpers(list : TAsmList);
  120. procedure gen_got_load(list : TAsmList);
  121. implementation
  122. uses
  123. version,
  124. cutils,cclasses,
  125. globals,systems,verbose,
  126. ppu,defutil,
  127. procinfo,paramgr,fmodule,
  128. regvars,dbgbase,
  129. pass_1,pass_2,
  130. nbas,ncon,nld,nutils,
  131. tgobj,cgobj
  132. {$ifdef powerpc}
  133. , cpupi
  134. {$endif}
  135. {$ifdef powerpc64}
  136. , cpupi
  137. {$endif}
  138. {$ifdef SUPPORT_MMX}
  139. , cgx86
  140. {$endif SUPPORT_MMX}
  141. ;
  142. {*****************************************************************************
  143. Misc Helpers
  144. *****************************************************************************}
  145. procedure location_free(list: TAsmList; const location : TLocation);
  146. begin
  147. case location.loc of
  148. LOC_VOID:
  149. ;
  150. LOC_REGISTER,
  151. LOC_CREGISTER:
  152. begin
  153. if getsupreg(location.register)<first_int_imreg then
  154. cg.ungetcpuregister(list,location.register);
  155. end;
  156. LOC_FPUREGISTER,
  157. LOC_CFPUREGISTER:
  158. begin
  159. if getsupreg(location.register)<first_fpu_imreg then
  160. cg.ungetcpuregister(list,location.register);
  161. end;
  162. LOC_MMREGISTER,
  163. LOC_CMMREGISTER :
  164. begin
  165. if getsupreg(location.register)<first_mm_imreg then
  166. cg.ungetcpuregister(list,location.register);
  167. end;
  168. LOC_REFERENCE,
  169. LOC_CREFERENCE :
  170. begin
  171. if use_fixed_stack then
  172. location_freetemp(list,location);
  173. end;
  174. else
  175. internalerror(2004110211);
  176. end;
  177. end;
  178. { DO NOT RELY on the fact that the tnode is not yet swaped
  179. because of inlining code PM }
  180. procedure firstcomplex(p : tbinarynode);
  181. var
  182. hp : tnode;
  183. begin
  184. { always calculate boolean AND and OR from left to right }
  185. if (p.nodetype in [orn,andn]) and
  186. is_boolean(p.left.resulttype.def) then
  187. begin
  188. if nf_swaped in p.flags then
  189. internalerror(234234);
  190. end
  191. else
  192. if (
  193. (p.expectloc=LOC_FPUREGISTER) and
  194. (p.right.registersfpu > p.left.registersfpu)
  195. ) or
  196. (
  197. (
  198. (
  199. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  200. (p.expectloc<>LOC_FPUREGISTER)
  201. ) and
  202. (p.left.registersint<p.right.registersint)
  203. )
  204. ) then
  205. begin
  206. hp:=p.left;
  207. p.left:=p.right;
  208. p.right:=hp;
  209. if nf_swaped in p.flags then
  210. exclude(p.flags,nf_swaped)
  211. else
  212. include(p.flags,nf_swaped);
  213. end;
  214. end;
  215. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  216. {
  217. produces jumps to true respectively false labels using boolean expressions
  218. depending on whether the loading of regvars is currently being
  219. synchronized manually (such as in an if-node) or automatically (most of
  220. the other cases where this procedure is called), loadregvars can be
  221. "lr_load_regvars" or "lr_dont_load_regvars"
  222. }
  223. var
  224. opsize : tcgsize;
  225. storepos : tfileposinfo;
  226. tmpreg : tregister;
  227. begin
  228. if nf_error in p.flags then
  229. exit;
  230. storepos:=aktfilepos;
  231. aktfilepos:=p.fileinfo;
  232. if is_boolean(p.resulttype.def) then
  233. begin
  234. {$ifdef OLDREGVARS}
  235. if loadregvars = lr_load_regvars then
  236. load_all_regvars(list);
  237. {$endif OLDREGVARS}
  238. if is_constboolnode(p) then
  239. begin
  240. if tordconstnode(p).value<>0 then
  241. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  242. else
  243. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  244. end
  245. else
  246. begin
  247. opsize:=def_cgsize(p.resulttype.def);
  248. case p.location.loc of
  249. LOC_SUBSETREG,LOC_CSUBSETREG:
  250. begin
  251. tmpreg := cg.getintregister(list,OS_INT);
  252. cg.a_load_subsetreg_reg(list,p.location.subsetregsize,p.location.size,p.location.startbit,OS_INT,p.location.subsetreg,tmpreg);
  253. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  254. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  255. end;
  256. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  257. begin
  258. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  259. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  260. end;
  261. LOC_JUMP:
  262. ;
  263. {$ifdef cpuflags}
  264. LOC_FLAGS :
  265. begin
  266. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  267. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  268. end;
  269. {$endif cpuflags}
  270. else
  271. begin
  272. printnode(output,p);
  273. internalerror(200308241);
  274. end;
  275. end;
  276. end;
  277. end
  278. else
  279. internalerror(200112305);
  280. aktfilepos:=storepos;
  281. end;
  282. (*
  283. This code needs fixing. It is not safe to use rgint; on the m68000 it
  284. would be rgaddr.
  285. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  286. begin
  287. case t.loc of
  288. LOC_REGISTER:
  289. begin
  290. { can't be a regvar, since it would be LOC_CREGISTER then }
  291. exclude(regs,getsupreg(t.register));
  292. if t.register64.reghi<>NR_NO then
  293. exclude(regs,getsupreg(t.register64.reghi));
  294. end;
  295. LOC_CREFERENCE,LOC_REFERENCE:
  296. begin
  297. if not(cs_opt_regvar in aktoptimizerswitches) or
  298. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  299. exclude(regs,getsupreg(t.reference.base));
  300. if not(cs_opt_regvar in aktoptimizerswitches) or
  301. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  302. exclude(regs,getsupreg(t.reference.index));
  303. end;
  304. end;
  305. end;
  306. *)
  307. {*****************************************************************************
  308. EXCEPTION MANAGEMENT
  309. *****************************************************************************}
  310. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  311. var
  312. srsym : ttypesym;
  313. begin
  314. if jmp_buf_size=-1 then
  315. begin
  316. srsym:=search_system_type('JMP_BUF');
  317. jmp_buf_size:=srsym.restype.def.size;
  318. end;
  319. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  320. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  321. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  322. end;
  323. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  324. begin
  325. tg.Ungettemp(list,t.jmpbuf);
  326. tg.ungettemp(list,t.envbuf);
  327. tg.ungettemp(list,t.reasonbuf);
  328. end;
  329. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  330. var
  331. paraloc1,paraloc2,paraloc3 : tcgpara;
  332. begin
  333. paraloc1.init;
  334. paraloc2.init;
  335. paraloc3.init;
  336. paramanager.getintparaloc(pocall_default,1,paraloc1);
  337. paramanager.getintparaloc(pocall_default,2,paraloc2);
  338. paramanager.getintparaloc(pocall_default,3,paraloc3);
  339. paramanager.allocparaloc(list,paraloc3);
  340. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  341. paramanager.allocparaloc(list,paraloc2);
  342. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  343. { push type of exceptionframe }
  344. paramanager.allocparaloc(list,paraloc1);
  345. cg.a_param_const(list,OS_S32,1,paraloc1);
  346. paramanager.freeparaloc(list,paraloc3);
  347. paramanager.freeparaloc(list,paraloc2);
  348. paramanager.freeparaloc(list,paraloc1);
  349. cg.allocallcpuregisters(list);
  350. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  351. cg.deallocallcpuregisters(list);
  352. paramanager.getintparaloc(pocall_default,1,paraloc1);
  353. paramanager.allocparaloc(list,paraloc1);
  354. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  355. paramanager.freeparaloc(list,paraloc1);
  356. cg.allocallcpuregisters(list);
  357. cg.a_call_name(list,'FPC_SETJMP');
  358. cg.deallocallcpuregisters(list);
  359. cg.g_exception_reason_save(list, t.reasonbuf);
  360. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  361. paraloc1.done;
  362. paraloc2.done;
  363. paraloc3.done;
  364. end;
  365. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  366. begin
  367. cg.allocallcpuregisters(list);
  368. cg.a_call_name(list,'FPC_POPADDRSTACK');
  369. cg.deallocallcpuregisters(list);
  370. if not onlyfree then
  371. begin
  372. cg.g_exception_reason_load(list, t.reasonbuf);
  373. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  374. end;
  375. end;
  376. {*****************************************************************************
  377. TLocation
  378. *****************************************************************************}
  379. {$ifndef cpu64bit}
  380. { 32-bit version }
  381. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  382. var
  383. hregister,
  384. hregisterhi : tregister;
  385. hreg64 : tregister64;
  386. hl : tasmlabel;
  387. oldloc : tlocation;
  388. const_location: boolean;
  389. begin
  390. oldloc:=l;
  391. if dst_size=OS_NO then
  392. internalerror(200309144);
  393. { handle transformations to 64bit separate }
  394. if dst_size in [OS_64,OS_S64] then
  395. begin
  396. if not (l.size in [OS_64,OS_S64]) then
  397. begin
  398. { load a smaller size to OS_64 }
  399. if l.loc=LOC_REGISTER then
  400. begin
  401. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  402. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  403. end
  404. else
  405. hregister:=cg.getintregister(list,OS_INT);
  406. { load value in low register }
  407. case l.loc of
  408. LOC_FLAGS :
  409. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  410. LOC_JUMP :
  411. begin
  412. cg.a_label(list,current_procinfo.CurrTrueLabel);
  413. cg.a_load_const_reg(list,OS_INT,1,hregister);
  414. current_asmdata.getjumplabel(hl);
  415. cg.a_jmp_always(list,hl);
  416. cg.a_label(list,current_procinfo.CurrFalseLabel);
  417. cg.a_load_const_reg(list,OS_INT,0,hregister);
  418. cg.a_label(list,hl);
  419. end;
  420. else
  421. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  422. end;
  423. { reset hi part, take care of the signed bit of the current value }
  424. hregisterhi:=cg.getintregister(list,OS_INT);
  425. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  426. begin
  427. if l.loc=LOC_CONSTANT then
  428. begin
  429. if (longint(l.value)<0) then
  430. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  431. else
  432. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  433. end
  434. else
  435. begin
  436. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  437. hregisterhi);
  438. end;
  439. end
  440. else
  441. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  442. location_reset(l,LOC_REGISTER,dst_size);
  443. l.register64.reglo:=hregister;
  444. l.register64.reghi:=hregisterhi;
  445. end
  446. else
  447. begin
  448. { 64bit to 64bit }
  449. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  450. begin
  451. hregister:=l.register64.reglo;
  452. hregisterhi:=l.register64.reghi;
  453. const_location := true;
  454. end
  455. else
  456. begin
  457. hregister:=cg.getintregister(list,OS_INT);
  458. hregisterhi:=cg.getintregister(list,OS_INT);
  459. const_location := false;
  460. end;
  461. hreg64.reglo:=hregister;
  462. hreg64.reghi:=hregisterhi;
  463. { load value in new register }
  464. cg64.a_load64_loc_reg(list,l,hreg64);
  465. if not const_location then
  466. location_reset(l,LOC_REGISTER,dst_size)
  467. else
  468. location_reset(l,LOC_CREGISTER,dst_size);
  469. l.register64.reglo:=hregister;
  470. l.register64.reghi:=hregisterhi;
  471. end;
  472. end
  473. else
  474. begin
  475. {Do not bother to recycle the existing register. The register
  476. allocator eliminates unnecessary moves, so it's not needed
  477. and trying to recycle registers can cause problems because
  478. the registers changes size and may need aditional constraints.
  479. Not if it's about LOC_CREGISTER's (JM)
  480. }
  481. const_location :=
  482. (maybeconst) and
  483. (l.loc = LOC_CREGISTER) and
  484. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  485. ((l.size = dst_size) or
  486. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  487. if not const_location then
  488. hregister:=cg.getintregister(list,dst_size)
  489. else
  490. hregister := l.register;
  491. { load value in new register }
  492. case l.loc of
  493. LOC_FLAGS :
  494. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  495. LOC_JUMP :
  496. begin
  497. cg.a_label(list,current_procinfo.CurrTrueLabel);
  498. cg.a_load_const_reg(list,dst_size,1,hregister);
  499. current_asmdata.getjumplabel(hl);
  500. cg.a_jmp_always(list,hl);
  501. cg.a_label(list,current_procinfo.CurrFalseLabel);
  502. cg.a_load_const_reg(list,dst_size,0,hregister);
  503. cg.a_label(list,hl);
  504. end;
  505. else
  506. begin
  507. { load_loc_reg can only handle size >= l.size, when the
  508. new size is smaller then we need to adjust the size
  509. of the orignal and maybe recalculate l.register for i386 }
  510. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  511. begin
  512. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  513. l.register:=cg.makeregsize(list,l.register,dst_size);
  514. { for big endian systems, the reference's offset must }
  515. { be increased in this case, since they have the }
  516. { MSB first in memory and e.g. byte(word_var) should }
  517. { return the second byte in this case (JM) }
  518. if (target_info.endian = ENDIAN_BIG) and
  519. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  520. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  521. {$ifdef x86}
  522. if not (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  523. l.size:=dst_size;
  524. {$endif x86}
  525. end;
  526. cg.a_load_loc_reg(list,dst_size,l,hregister);
  527. {$ifndef x86}
  528. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  529. l.size:=dst_size;
  530. {$endif not x86}
  531. end;
  532. end;
  533. if not const_location then
  534. location_reset(l,LOC_REGISTER,dst_size)
  535. else
  536. location_reset(l,LOC_CREGISTER,dst_size);
  537. l.register:=hregister;
  538. end;
  539. { Release temp when it was a reference }
  540. if oldloc.loc=LOC_REFERENCE then
  541. location_freetemp(list,oldloc);
  542. end;
  543. {$else cpu64bit}
  544. { 64-bit version }
  545. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  546. var
  547. hregister : tregister;
  548. hl : tasmlabel;
  549. oldloc : tlocation;
  550. begin
  551. oldloc:=l;
  552. hregister:=cg.getintregister(list,dst_size);
  553. { load value in new register }
  554. case l.loc of
  555. LOC_FLAGS :
  556. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  557. LOC_JUMP :
  558. begin
  559. cg.a_label(list,current_procinfo.CurrTrueLabel);
  560. cg.a_load_const_reg(list,dst_size,1,hregister);
  561. current_asmdata.getjumplabel(hl);
  562. cg.a_jmp_always(list,hl);
  563. cg.a_label(list,current_procinfo.CurrFalseLabel);
  564. cg.a_load_const_reg(list,dst_size,0,hregister);
  565. cg.a_label(list,hl);
  566. end;
  567. else
  568. begin
  569. { load_loc_reg can only handle size >= l.size, when the
  570. new size is smaller then we need to adjust the size
  571. of the orignal and maybe recalculate l.register for i386 }
  572. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  573. begin
  574. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  575. l.register:=cg.makeregsize(list,l.register,dst_size);
  576. { for big endian systems, the reference's offset must }
  577. { be increased in this case, since they have the }
  578. { MSB first in memory and e.g. byte(word_var) should }
  579. { return the second byte in this case (JM) }
  580. if (target_info.endian = ENDIAN_BIG) and
  581. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  582. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  583. {$ifdef x86}
  584. l.size:=dst_size;
  585. {$endif x86}
  586. end;
  587. cg.a_load_loc_reg(list,dst_size,l,hregister);
  588. {$ifndef x86}
  589. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  590. l.size:=dst_size;
  591. {$endif not x86}
  592. end;
  593. end;
  594. if (l.loc <> LOC_CREGISTER) or
  595. not maybeconst then
  596. location_reset(l,LOC_REGISTER,dst_size)
  597. else
  598. location_reset(l,LOC_CREGISTER,dst_size);
  599. l.register:=hregister;
  600. { Release temp when it was a reference }
  601. if oldloc.loc=LOC_REFERENCE then
  602. location_freetemp(list,oldloc);
  603. end;
  604. {$endif cpu64bit}
  605. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  606. var
  607. reg : tregister;
  608. href : treference;
  609. begin
  610. if (l.loc<>LOC_FPUREGISTER) and
  611. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  612. begin
  613. { if it's in an mm register, store to memory first }
  614. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  615. begin
  616. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  617. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  618. location_reset(l,LOC_REFERENCE,l.size);
  619. l.reference:=href;
  620. end;
  621. reg:=cg.getfpuregister(list,l.size);
  622. cg.a_loadfpu_loc_reg(list,l,reg);
  623. location_freetemp(list,l);
  624. location_reset(l,LOC_FPUREGISTER,l.size);
  625. l.register:=reg;
  626. end;
  627. end;
  628. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  629. var
  630. reg : tregister;
  631. href : treference;
  632. begin
  633. if (l.loc<>LOC_MMREGISTER) and
  634. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  635. begin
  636. { if it's in an fpu register, store to memory first }
  637. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  638. begin
  639. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  640. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  641. location_reset(l,LOC_REFERENCE,l.size);
  642. l.reference:=href;
  643. end;
  644. reg:=cg.getmmregister(list,l.size);
  645. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  646. location_freetemp(list,l);
  647. location_reset(l,LOC_MMREGISTER,l.size);
  648. l.register:=reg;
  649. end;
  650. end;
  651. procedure location_force_mem(list:TAsmList;var l:tlocation);
  652. var
  653. r : treference;
  654. begin
  655. case l.loc of
  656. LOC_FPUREGISTER,
  657. LOC_CFPUREGISTER :
  658. begin
  659. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  660. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  661. location_reset(l,LOC_REFERENCE,l.size);
  662. l.reference:=r;
  663. end;
  664. LOC_MMREGISTER,
  665. LOC_CMMREGISTER:
  666. begin
  667. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  668. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  669. location_reset(l,LOC_REFERENCE,l.size);
  670. l.reference:=r;
  671. end;
  672. LOC_CONSTANT,
  673. LOC_REGISTER,
  674. LOC_CREGISTER :
  675. begin
  676. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  677. {$ifndef cpu64bit}
  678. if l.size in [OS_64,OS_S64] then
  679. cg64.a_load64_loc_ref(list,l,r)
  680. else
  681. {$endif cpu64bit}
  682. cg.a_load_loc_ref(list,l.size,l,r);
  683. location_reset(l,LOC_REFERENCE,l.size);
  684. l.reference:=r;
  685. end;
  686. LOC_SUBSETREG,
  687. LOC_CSUBSETREG:
  688. begin
  689. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  690. cg.a_load_subsetreg_ref(list,l.subsetregsize,l.size,l.startbit,l.size,l.subsetreg,r);
  691. location_reset(l,LOC_REFERENCE,l.size);
  692. l.reference:=r;
  693. end;
  694. LOC_CREFERENCE,
  695. LOC_REFERENCE : ;
  696. else
  697. internalerror(200203219);
  698. end;
  699. end;
  700. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  701. begin
  702. case l.loc of
  703. LOC_REGISTER,
  704. LOC_CREGISTER :
  705. begin
  706. if not loadref then
  707. internalerror(200410231);
  708. reference_reset_base(ref,l.register,0);
  709. end;
  710. LOC_REFERENCE,
  711. LOC_CREFERENCE :
  712. begin
  713. if loadref then
  714. begin
  715. reference_reset_base(ref,cg.getaddressregister(list),0);
  716. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  717. end
  718. else
  719. ref:=l.reference;
  720. end;
  721. else
  722. internalerror(200309181);
  723. end;
  724. end;
  725. {*****************************************************************************
  726. Maybe_Save
  727. *****************************************************************************}
  728. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  729. begin
  730. {$ifdef i386}
  731. if (needed>=maxfpuregs) and
  732. (l.loc = LOC_FPUREGISTER) then
  733. begin
  734. location_force_mem(list,l);
  735. maybe_pushfpu:=true;
  736. end
  737. else
  738. maybe_pushfpu:=false;
  739. {$else i386}
  740. maybe_pushfpu:=false;
  741. {$endif i386}
  742. end;
  743. {****************************************************************************
  744. Init/Finalize Code
  745. ****************************************************************************}
  746. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  747. var
  748. href : treference;
  749. hreg : tregister;
  750. list : TAsmList;
  751. hsym : tparavarsym;
  752. l : longint;
  753. localcopyloc : tlocation;
  754. begin
  755. list:=TAsmList(arg);
  756. if (tsym(p).typ=paravarsym) and
  757. (tparavarsym(p).varspez=vs_value) and
  758. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  759. begin
  760. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  761. if is_open_array(tparavarsym(p).vartype.def) or
  762. is_array_of_const(tparavarsym(p).vartype.def) then
  763. begin
  764. { cdecl functions don't have a high pointer so it is not possible to generate
  765. a local copy }
  766. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  767. begin
  768. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  769. if not assigned(hsym) then
  770. internalerror(200306061);
  771. hreg:=cg.getaddressregister(list);
  772. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  773. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  774. end;
  775. end
  776. else
  777. begin
  778. { Allocate space for the local copy }
  779. l:=tparavarsym(p).getsize;
  780. localcopyloc.loc:=LOC_REFERENCE;
  781. localcopyloc.size:=int_cgsize(l);
  782. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  783. { Copy data }
  784. if is_shortstring(tparavarsym(p).vartype.def) then
  785. begin
  786. { this code is only executed before the code for the body and the entry/exit code is generated
  787. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  788. }
  789. include(current_procinfo.flags,pi_do_call);
  790. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  791. end
  792. else
  793. begin
  794. { pass proper alignment info }
  795. localcopyloc.reference.alignment:=tparavarsym(p).vartype.def.alignment;
  796. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  797. end;
  798. { update localloc of varsym }
  799. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  800. tparavarsym(p).localloc:=localcopyloc;
  801. end;
  802. end;
  803. end;
  804. { initializes the regvars from staticsymtable with 0 }
  805. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  806. begin
  807. if (tsym(p).typ=globalvarsym) then
  808. begin
  809. case tglobalvarsym(p).localloc.loc of
  810. LOC_CREGISTER :
  811. cg.a_load_const_reg(TAsmList(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  812. tglobalvarsym(p).localloc.register);
  813. LOC_REFERENCE : ;
  814. LOC_CMMREGISTER :
  815. { clear the whole register }
  816. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  817. tglobalvarsym(p).localloc.register,
  818. tglobalvarsym(p).localloc.register,
  819. nil);
  820. LOC_CFPUREGISTER :
  821. ;
  822. else
  823. internalerror(200410124);
  824. end;
  825. end;
  826. end;
  827. { generates the code for initialisation of local data }
  828. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  829. var
  830. OldAsmList : TAsmList;
  831. hp : tnode;
  832. begin
  833. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  834. (tabstractvarsym(p).refs>0) and
  835. not(vo_is_external in tabstractvarsym(p).varoptions) and
  836. not(is_class(tabstractvarsym(p).vartype.def)) and
  837. tabstractvarsym(p).vartype.def.needs_inittable then
  838. begin
  839. OldAsmList:=current_asmdata.CurrAsmList;
  840. current_asmdata.CurrAsmList:=TAsmList(arg);
  841. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  842. firstpass(hp);
  843. secondpass(hp);
  844. hp.free;
  845. current_asmdata.CurrAsmList:=OldAsmList;
  846. end;
  847. end;
  848. procedure finalize_sym(asmlist:TAsmList;sym:tsym);
  849. var
  850. hp : tnode;
  851. OldAsmList : TAsmList;
  852. begin
  853. include(current_procinfo.flags,pi_needs_implicit_finally);
  854. OldAsmList:=current_asmdata.CurrAsmList;
  855. current_asmdata.CurrAsmList:=asmlist;
  856. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  857. firstpass(hp);
  858. secondpass(hp);
  859. hp.free;
  860. current_asmdata.CurrAsmList:=OldAsmList;
  861. end;
  862. { generates the code for finalisation of local variables }
  863. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  864. begin
  865. if (tsym(p).typ=localvarsym) and
  866. (tlocalvarsym(p).refs>0) and
  867. not(vo_is_external in tlocalvarsym(p).varoptions) and
  868. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  869. not(is_class(tlocalvarsym(p).vartype.def)) and
  870. tlocalvarsym(p).vartype.def.needs_inittable then
  871. finalize_sym(TAsmList(arg),tsym(p));
  872. end;
  873. { generates the code for finalisation of local typedconsts }
  874. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  875. var
  876. i : longint;
  877. pd : tprocdef;
  878. begin
  879. case tsym(p).typ of
  880. typedconstsym :
  881. begin
  882. if ttypedconstsym(p).is_writable and
  883. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  884. finalize_sym(TAsmList(arg),tsym(p));
  885. end;
  886. procsym :
  887. begin
  888. for i:=1 to tprocsym(p).procdef_count do
  889. begin
  890. pd:=tprocsym(p).procdef[i];
  891. if assigned(pd.localst) and
  892. (pd.procsym=tprocsym(p)) and
  893. (pd.localst.symtabletype<>staticsymtable) then
  894. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  895. end;
  896. end;
  897. end;
  898. end;
  899. { generates the code for finalization of static symtable and
  900. all local (static) typedconsts }
  901. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  902. var
  903. i : longint;
  904. pd : tprocdef;
  905. begin
  906. case tsym(p).typ of
  907. globalvarsym :
  908. begin
  909. if (tglobalvarsym(p).refs>0) and
  910. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  911. not(vo_is_external in tglobalvarsym(p).varoptions) and
  912. not(is_class(tglobalvarsym(p).vartype.def)) and
  913. tglobalvarsym(p).vartype.def.needs_inittable then
  914. finalize_sym(TAsmList(arg),tsym(p));
  915. end;
  916. typedconstsym :
  917. begin
  918. if ttypedconstsym(p).is_writable and
  919. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  920. finalize_sym(TAsmList(arg),tsym(p));
  921. end;
  922. procsym :
  923. begin
  924. for i:=1 to tprocsym(p).procdef_count do
  925. begin
  926. pd:=tprocsym(p).procdef[i];
  927. if assigned(pd.localst) and
  928. (pd.procsym=tprocsym(p)) and
  929. (pd.localst.symtabletype<>staticsymtable) then
  930. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  931. end;
  932. end;
  933. end;
  934. end;
  935. { generates the code for incrementing the reference count of parameters and
  936. initialize out parameters }
  937. procedure init_paras(p : tnamedindexitem;arg:pointer);
  938. var
  939. href : treference;
  940. tmpreg : tregister;
  941. list : TAsmList;
  942. begin
  943. list:=TAsmList(arg);
  944. if (tsym(p).typ=paravarsym) and
  945. not is_class_or_interface(tparavarsym(p).vartype.def) and
  946. tparavarsym(p).vartype.def.needs_inittable then
  947. begin
  948. case tparavarsym(p).varspez of
  949. vs_value :
  950. begin
  951. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  952. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  953. end;
  954. vs_out :
  955. begin
  956. tmpreg:=cg.getaddressregister(list);
  957. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  958. reference_reset_base(href,tmpreg,0);
  959. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  960. end;
  961. end;
  962. end;
  963. end;
  964. { generates the code for decrementing the reference count of parameters }
  965. procedure final_paras(p : tnamedindexitem;arg:pointer);
  966. var
  967. list : TAsmList;
  968. href : treference;
  969. begin
  970. if not(tsym(p).typ=paravarsym) then
  971. exit;
  972. list:=TAsmList(arg);
  973. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  974. tparavarsym(p).vartype.def.needs_inittable then
  975. begin
  976. if (tparavarsym(p).varspez=vs_value) then
  977. begin
  978. include(current_procinfo.flags,pi_needs_implicit_finally);
  979. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  980. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  981. end;
  982. end
  983. else
  984. if (tparavarsym(p).varspez=vs_value) and
  985. (is_open_array(tparavarsym(p).vartype.def) or
  986. is_array_of_const(tparavarsym(p).vartype.def)) then
  987. begin
  988. { cdecl functions don't have a high pointer so it is not possible to generate
  989. a local copy }
  990. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  991. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  992. end;
  993. end;
  994. { Initialize temp ansi/widestrings,interfaces }
  995. procedure inittempvariables(list:TAsmList);
  996. var
  997. hp : ptemprecord;
  998. href : treference;
  999. begin
  1000. hp:=tg.templist;
  1001. while assigned(hp) do
  1002. begin
  1003. if assigned(hp^.def) and
  1004. hp^.def.needs_inittable then
  1005. begin
  1006. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1007. cg.g_initialize(list,hp^.def,href);
  1008. end;
  1009. hp:=hp^.next;
  1010. end;
  1011. end;
  1012. procedure finalizetempvariables(list:TAsmList);
  1013. var
  1014. hp : ptemprecord;
  1015. href : treference;
  1016. begin
  1017. hp:=tg.templist;
  1018. while assigned(hp) do
  1019. begin
  1020. if assigned(hp^.def) and
  1021. hp^.def.needs_inittable then
  1022. begin
  1023. include(current_procinfo.flags,pi_needs_implicit_finally);
  1024. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1025. cg.g_finalize(list,hp^.def,href);
  1026. end;
  1027. hp:=hp^.next;
  1028. end;
  1029. end;
  1030. procedure gen_load_return_value(list:TAsmList);
  1031. var
  1032. {$ifndef cpu64bit}
  1033. href : treference;
  1034. {$endif cpu64bit}
  1035. ressym : tabstractnormalvarsym;
  1036. resloc,
  1037. restmploc : tlocation;
  1038. hreg : tregister;
  1039. funcretloc : tlocation;
  1040. begin
  1041. { Is the loading needed? }
  1042. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1043. (
  1044. (po_assembler in current_procinfo.procdef.procoptions) and
  1045. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1046. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1047. ) then
  1048. exit;
  1049. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1050. { constructors return self }
  1051. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1052. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  1053. else
  1054. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1055. if (ressym.refs>0) then
  1056. begin
  1057. {$ifdef OLDREGVARS}
  1058. case ressym.localloc.loc of
  1059. LOC_CFPUREGISTER,
  1060. LOC_FPUREGISTER:
  1061. begin
  1062. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1063. restmploc.register:=ressym.localloc.register;
  1064. end;
  1065. LOC_CREGISTER,
  1066. LOC_REGISTER:
  1067. begin
  1068. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1069. restmploc.register:=ressym.localloc.register;
  1070. end;
  1071. LOC_MMREGISTER:
  1072. begin
  1073. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1074. restmploc.register:=ressym.localloc.register;
  1075. end;
  1076. LOC_REFERENCE:
  1077. begin
  1078. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1079. restmploc.reference:=ressym.localloc.reference;
  1080. end;
  1081. else
  1082. internalerror(200309184);
  1083. end;
  1084. {$else}
  1085. restmploc:=ressym.localloc;
  1086. {$endif}
  1087. { Here, we return the function result. In most architectures, the value is
  1088. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1089. function returns in a register and the caller receives it in an other one }
  1090. case funcretloc.loc of
  1091. LOC_REGISTER:
  1092. begin
  1093. {$ifndef cpu64bit}
  1094. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1095. begin
  1096. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1097. if resloc.loc<>LOC_REGISTER then
  1098. internalerror(200409141);
  1099. { Load low and high register separate to generate better register
  1100. allocation info }
  1101. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1102. begin
  1103. cg.getcpuregister(list,resloc.register64.reglo);
  1104. end;
  1105. case restmploc.loc of
  1106. LOC_REFERENCE :
  1107. begin
  1108. href:=restmploc.reference;
  1109. if target_info.endian=ENDIAN_BIG then
  1110. inc(href.offset,4);
  1111. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1112. end;
  1113. LOC_CREGISTER :
  1114. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1115. else
  1116. internalerror(200409203);
  1117. end;
  1118. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1119. begin
  1120. cg.getcpuregister(list,resloc.register64.reghi);
  1121. end;
  1122. case restmploc.loc of
  1123. LOC_REFERENCE :
  1124. begin
  1125. href:=restmploc.reference;
  1126. if target_info.endian=ENDIAN_LITTLE then
  1127. inc(href.offset,4);
  1128. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1129. end;
  1130. LOC_CREGISTER :
  1131. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1132. else
  1133. internalerror(200409204);
  1134. end;
  1135. end
  1136. else
  1137. {$endif cpu64bit}
  1138. begin
  1139. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1140. if getsupreg(funcretloc.register)<first_int_imreg then
  1141. begin
  1142. cg.getcpuregister(list,funcretloc.register);
  1143. end;
  1144. { it could be that a structure is passed in memory but the function is expected to
  1145. return a pointer to this memory }
  1146. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1147. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1148. else
  1149. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1150. end;
  1151. end;
  1152. LOC_FPUREGISTER:
  1153. begin
  1154. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1155. begin
  1156. cg.getcpuregister(list,funcretloc.register);
  1157. end;
  1158. { we can't do direct moves between fpu and mm registers }
  1159. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1160. location_force_fpureg(list,restmploc,false);
  1161. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1162. end;
  1163. LOC_MMREGISTER:
  1164. begin
  1165. if getsupreg(funcretloc.register)<first_mm_imreg then
  1166. begin
  1167. cg.getcpuregister(list,funcretloc.register);
  1168. end;
  1169. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1170. end;
  1171. LOC_INVALID,
  1172. LOC_REFERENCE:
  1173. ;
  1174. else
  1175. internalerror(200405025);
  1176. end;
  1177. end
  1178. {$ifdef x86}
  1179. else
  1180. begin
  1181. { the caller will pop a value off the cpu stack }
  1182. if (funcretloc.loc = LOC_FPUREGISTER) then
  1183. list.concat(taicpu.op_none(A_FLDZ));
  1184. end;
  1185. {$endif x86}
  1186. end;
  1187. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1188. begin
  1189. case sym.localloc.loc of
  1190. LOC_CREGISTER:
  1191. begin
  1192. {$ifndef cpu64bit}
  1193. if sym.localloc.size in [OS_64,OS_S64] then
  1194. begin
  1195. sym.localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1196. sym.localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1197. end
  1198. else
  1199. {$endif cpu64bit}
  1200. sym.localloc.register:=cg.getintregister(list,sym.localloc.size);
  1201. end;
  1202. LOC_CFPUREGISTER:
  1203. begin
  1204. sym.localloc.register:=cg.getfpuregister(list,sym.localloc.size);
  1205. end;
  1206. LOC_CMMREGISTER:
  1207. begin
  1208. sym. localloc.register:=cg.getmmregister(list,sym.localloc.size);
  1209. end;
  1210. end;
  1211. if (pi_has_goto in current_procinfo.flags) then
  1212. begin
  1213. { Allocate register already, to prevent first allocation to be
  1214. inside a loop }
  1215. {$ifndef cpu64bit}
  1216. if sym.localloc.size in [OS_64,OS_S64] then
  1217. begin
  1218. cg.a_reg_sync(list,sym.localloc.register64.reglo);
  1219. cg.a_reg_sync(list,sym.localloc.register64.reghi);
  1220. end
  1221. else
  1222. {$endif cpu64bit}
  1223. cg.a_reg_sync(list,sym.localloc.register);
  1224. end;
  1225. end;
  1226. procedure gen_load_para_value(list:TAsmList);
  1227. procedure get_para(const paraloc:TCGParaLocation);
  1228. begin
  1229. case paraloc.loc of
  1230. LOC_REGISTER :
  1231. begin
  1232. if getsupreg(paraloc.register)<first_int_imreg then
  1233. cg.getcpuregister(list,paraloc.register);
  1234. end;
  1235. LOC_MMREGISTER :
  1236. begin
  1237. if getsupreg(paraloc.register)<first_mm_imreg then
  1238. cg.getcpuregister(list,paraloc.register);
  1239. end;
  1240. LOC_FPUREGISTER :
  1241. begin
  1242. if getsupreg(paraloc.register)<first_fpu_imreg then
  1243. cg.getcpuregister(list,paraloc.register);
  1244. end;
  1245. end;
  1246. end;
  1247. procedure unget_para(const paraloc:TCGParaLocation);
  1248. begin
  1249. case paraloc.loc of
  1250. LOC_REGISTER :
  1251. begin
  1252. if getsupreg(paraloc.register)<first_int_imreg then
  1253. cg.ungetcpuregister(list,paraloc.register);
  1254. end;
  1255. LOC_MMREGISTER :
  1256. begin
  1257. if getsupreg(paraloc.register)<first_mm_imreg then
  1258. cg.ungetcpuregister(list,paraloc.register);
  1259. end;
  1260. LOC_FPUREGISTER :
  1261. begin
  1262. if getsupreg(paraloc.register)<first_fpu_imreg then
  1263. cg.ungetcpuregister(list,paraloc.register);
  1264. end;
  1265. end;
  1266. end;
  1267. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1268. var
  1269. href : treference;
  1270. begin
  1271. case paraloc.loc of
  1272. LOC_REGISTER :
  1273. begin
  1274. {$IFDEF POWERPC64}
  1275. if (paraloc.shiftval <> 0) then
  1276. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1277. {$ENDIF POWERPC64}
  1278. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1279. end;
  1280. LOC_MMREGISTER :
  1281. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1282. LOC_FPUREGISTER :
  1283. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1284. LOC_REFERENCE :
  1285. begin
  1286. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1287. { use concatcopy, because it can also be a float which fails when
  1288. load_ref_ref is used. Don't copy data when the references are equal }
  1289. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1290. cg.g_concatcopy(list,href,ref,sizeleft);
  1291. end;
  1292. else
  1293. internalerror(2002081302);
  1294. end;
  1295. end;
  1296. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1297. var
  1298. href : treference;
  1299. begin
  1300. case paraloc.loc of
  1301. LOC_REGISTER :
  1302. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1303. LOC_MMREGISTER :
  1304. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1305. LOC_FPUREGISTER :
  1306. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1307. LOC_REFERENCE :
  1308. begin
  1309. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1310. case getregtype(reg) of
  1311. R_INTREGISTER :
  1312. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1313. R_FPUREGISTER :
  1314. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1315. R_MMREGISTER :
  1316. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1317. else
  1318. internalerror(2004101012);
  1319. end;
  1320. end;
  1321. else
  1322. internalerror(2002081302);
  1323. end;
  1324. end;
  1325. var
  1326. i : longint;
  1327. currpara : tparavarsym;
  1328. paraloc : pcgparalocation;
  1329. href : treference;
  1330. sizeleft : aint;
  1331. {$ifdef sparc}
  1332. tempref : treference;
  1333. {$endif sparc}
  1334. begin
  1335. if (po_assembler in current_procinfo.procdef.procoptions) then
  1336. exit;
  1337. { Allocate registers used by parameters }
  1338. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1339. begin
  1340. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1341. paraloc:=currpara.paraloc[calleeside].location;
  1342. while assigned(paraloc) do
  1343. begin
  1344. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1345. get_para(paraloc^);
  1346. paraloc:=paraloc^.next;
  1347. end;
  1348. end;
  1349. { Copy parameters to local references/registers }
  1350. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1351. begin
  1352. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1353. paraloc:=currpara.paraloc[calleeside].location;
  1354. { skip e.g. empty records }
  1355. if not assigned(paraloc) then
  1356. internalerror(200408203);
  1357. if (paraloc^.loc = LOC_VOID) then
  1358. continue;
  1359. case currpara.localloc.loc of
  1360. LOC_REFERENCE :
  1361. begin
  1362. { If the parameter location is reused we don't need to copy
  1363. anything }
  1364. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1365. begin
  1366. href:=currpara.localloc.reference;
  1367. sizeleft:=currpara.paraloc[calleeside].intsize;
  1368. while assigned(paraloc) do
  1369. begin
  1370. unget_para(paraloc^);
  1371. if (paraloc^.size=OS_NO) then
  1372. begin
  1373. { Can only be a reference that contains the rest
  1374. of the parameter }
  1375. if (paraloc^.loc<>LOC_REFERENCE) or
  1376. assigned(paraloc^.next) then
  1377. internalerror(2005013010);
  1378. gen_load_ref(paraloc^,href,sizeleft);
  1379. inc(href.offset,sizeleft);
  1380. sizeleft:=0;
  1381. end
  1382. else
  1383. begin
  1384. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1385. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1386. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1387. end;
  1388. paraloc:=paraloc^.next;
  1389. end;
  1390. end;
  1391. end;
  1392. LOC_CREGISTER :
  1393. begin
  1394. {$ifndef cpu64bit}
  1395. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1396. is_64bit(currpara.vartype.def) then
  1397. begin
  1398. case paraloc^.loc of
  1399. LOC_REGISTER:
  1400. begin
  1401. if not assigned(paraloc^.next) then
  1402. internalerror(200410104);
  1403. if (target_info.endian=ENDIAN_BIG) then
  1404. begin
  1405. { paraloc^ -> high
  1406. paraloc^.next -> low }
  1407. unget_para(paraloc^);
  1408. gen_alloc_regvar(list,currpara);
  1409. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1410. unget_para(paraloc^.next^);
  1411. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1412. end
  1413. else
  1414. begin
  1415. { paraloc^ -> low
  1416. paraloc^.next -> high }
  1417. unget_para(paraloc^);
  1418. gen_alloc_regvar(list,currpara);
  1419. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1420. unget_para(paraloc^.next^);
  1421. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1422. end;
  1423. end;
  1424. LOC_REFERENCE:
  1425. begin
  1426. gen_alloc_regvar(list,currpara);
  1427. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1428. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1429. unget_para(paraloc^);
  1430. end;
  1431. else
  1432. internalerror(2005101501);
  1433. end
  1434. end
  1435. else
  1436. {$endif cpu64bit}
  1437. begin
  1438. if assigned(paraloc^.next) then
  1439. internalerror(200410105);
  1440. unget_para(paraloc^);
  1441. gen_alloc_regvar(list,currpara);
  1442. gen_load_reg(paraloc^,currpara.localloc.register);
  1443. end;
  1444. end;
  1445. LOC_CFPUREGISTER :
  1446. begin
  1447. {$ifdef sparc}
  1448. { Sparc passes floats in int registers, when loading to fpu register
  1449. we need a temp }
  1450. sizeleft := TCGSize2Size[currpara.localloc.size];
  1451. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1452. href:=tempref;
  1453. while assigned(paraloc) do
  1454. begin
  1455. unget_para(paraloc^);
  1456. gen_load_ref(paraloc^,href,sizeleft);
  1457. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1458. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1459. paraloc:=paraloc^.next;
  1460. end;
  1461. gen_alloc_regvar(list,currpara);
  1462. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1463. tg.UnGetTemp(list,tempref);
  1464. {$else sparc}
  1465. unget_para(paraloc^);
  1466. gen_alloc_regvar(list,currpara);
  1467. gen_load_reg(paraloc^,currpara.localloc.register);
  1468. if assigned(paraloc^.next) then
  1469. internalerror(200410109);
  1470. {$endif sparc}
  1471. end;
  1472. LOC_CMMREGISTER :
  1473. begin
  1474. unget_para(paraloc^);
  1475. gen_alloc_regvar(list,currpara);
  1476. gen_load_reg(paraloc^,currpara.localloc.register);
  1477. { data could come in two memory locations, for now
  1478. we simply ignore the sanity check (FK)
  1479. if assigned(paraloc^.next) then
  1480. internalerror(200410108);
  1481. }
  1482. end;
  1483. end;
  1484. end;
  1485. { generate copies of call by value parameters, must be done before
  1486. the initialization and body is parsed because the refcounts are
  1487. incremented using the local copies }
  1488. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1489. {$ifdef powerpc}
  1490. { unget the register that contains the stack pointer before the procedure entry, }
  1491. { which is used to access the parameters in their original callee-side location }
  1492. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1493. cg.a_reg_dealloc(list,NR_R12);
  1494. {$endif powerpc}
  1495. {$ifdef powerpc64}
  1496. { unget the register that contains the stack pointer before the procedure entry, }
  1497. { which is used to access the parameters in their original callee-side location }
  1498. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1499. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1500. {$endif powerpc64}
  1501. end;
  1502. procedure gen_initialize_code(list:TAsmList);
  1503. begin
  1504. { initialize local data like ansistrings }
  1505. case current_procinfo.procdef.proctypeoption of
  1506. potype_unitinit:
  1507. begin
  1508. { this is also used for initialization of variables in a
  1509. program which does not have a globalsymtable }
  1510. if assigned(current_module.globalsymtable) then
  1511. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1512. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1513. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1514. end;
  1515. { units have seperate code for initilization and finalization }
  1516. potype_unitfinalize: ;
  1517. { program init/final is generated in separate procedure }
  1518. potype_proginit:
  1519. begin
  1520. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1521. end;
  1522. else
  1523. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1524. end;
  1525. { initialisizes temp. ansi/wide string data }
  1526. inittempvariables(list);
  1527. { initialize ansi/widesstring para's }
  1528. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1529. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1530. {$ifdef OLDREGVARS}
  1531. load_regvars(list,nil);
  1532. {$endif OLDREGVARS}
  1533. end;
  1534. procedure gen_finalize_code(list:TAsmList);
  1535. begin
  1536. {$ifdef OLDREGVARS}
  1537. cleanup_regvars(list);
  1538. {$endif OLDREGVARS}
  1539. { finalize temporary data }
  1540. finalizetempvariables(list);
  1541. { finalize local data like ansistrings}
  1542. case current_procinfo.procdef.proctypeoption of
  1543. potype_unitfinalize:
  1544. begin
  1545. { this is also used for initialization of variables in a
  1546. program which does not have a globalsymtable }
  1547. if assigned(current_module.globalsymtable) then
  1548. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1549. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1550. end;
  1551. { units/progs have separate code for initialization and finalization }
  1552. potype_unitinit: ;
  1553. { program init/final is generated in separate procedure }
  1554. potype_proginit: ;
  1555. else
  1556. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1557. end;
  1558. { finalize paras data }
  1559. if assigned(current_procinfo.procdef.parast) and
  1560. not(po_assembler in current_procinfo.procdef.procoptions) then
  1561. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1562. end;
  1563. procedure gen_entry_code(list:TAsmList);
  1564. var
  1565. paraloc1,
  1566. paraloc2 : tcgpara;
  1567. begin
  1568. paraloc1.init;
  1569. paraloc2.init;
  1570. { the actual profile code can clobber some registers,
  1571. therefore if the context must be saved, do it before
  1572. the actual call to the profile code
  1573. }
  1574. if (cs_profile in aktmoduleswitches) and
  1575. not(po_assembler in current_procinfo.procdef.procoptions) then
  1576. begin
  1577. { non-win32 can call mcout even in main }
  1578. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1579. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1580. begin
  1581. cg.g_profilecode(list);
  1582. end;
  1583. end;
  1584. { call startup helpers from main program }
  1585. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1586. begin
  1587. { initialize units }
  1588. cg.allocallcpuregisters(list);
  1589. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1590. cg.deallocallcpuregisters(list);
  1591. end;
  1592. list.concat(Tai_force_line.Create);
  1593. {$ifdef OLDREGVARS}
  1594. load_regvars(list,nil);
  1595. {$endif OLDREGVARS}
  1596. paraloc1.done;
  1597. paraloc2.done;
  1598. end;
  1599. procedure gen_exit_code(list:TAsmList);
  1600. begin
  1601. { call __EXIT for main program }
  1602. if (not DLLsource) and
  1603. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1604. cg.a_call_name(list,'FPC_DO_EXIT');
  1605. end;
  1606. {****************************************************************************
  1607. Entry/Exit
  1608. ****************************************************************************}
  1609. procedure alloc_proc_symbol(pd: tprocdef);
  1610. var
  1611. item: tstringlistitem;
  1612. begin
  1613. item := tstringlistitem(pd.aliasnames.first);
  1614. while assigned(item) do
  1615. begin
  1616. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1617. item := tstringlistitem(item.next);
  1618. end;
  1619. end;
  1620. procedure gen_proc_symbol(list:TAsmList);
  1621. var
  1622. hs : string;
  1623. begin
  1624. repeat
  1625. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1626. if hs='' then
  1627. break;
  1628. if (cs_profile in aktmoduleswitches) or
  1629. (po_global in current_procinfo.procdef.procoptions) then
  1630. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1631. else
  1632. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1633. if tf_use_function_relative_addresses in target_info.flags then
  1634. list.concat(Tai_function_name.create(hs));
  1635. until false;
  1636. current_procinfo.procdef.procstarttai:=tai(list.last);
  1637. end;
  1638. procedure gen_proc_symbol_end(list:TAsmList);
  1639. begin
  1640. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1641. current_procinfo.procdef.procendtai:=tai(list.last);
  1642. { finalisation marker for Mac OS X }
  1643. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) and
  1644. (current_module.islibrary) and
  1645. (((current_module.flags and uf_finalize)<>0) or
  1646. (current_procinfo.procdef.proctypeoption = potype_proginit)) then
  1647. begin
  1648. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1649. list.concat(tai_directive.create(asd_mod_init_func,''))
  1650. else
  1651. list.concat(tai_directive.create(asd_mod_term_func,''));
  1652. list.concat(tai_align.create(4));
  1653. list.concat(Tai_const.Createname(current_procinfo.procdef.mangledname,0));
  1654. end;
  1655. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1656. begin
  1657. if (target_info.system in [system_powerpc_darwin,system_i386_darwin,system_powerpc_macos]) and
  1658. not(current_module.islibrary) then
  1659. begin
  1660. list.concat(tai_section.create(sec_code,'',4));
  1661. list.concat(tai_symbol.createname_global(
  1662. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1663. { keep argc, argv and envp properly on the stack }
  1664. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1665. end;
  1666. end;
  1667. end;
  1668. procedure gen_proc_entry_code(list:TAsmList);
  1669. var
  1670. hitemp,
  1671. lotemp : longint;
  1672. begin
  1673. { generate call frame marker for dwarf call frame info }
  1674. current_asmdata.asmcfi.start_frame(list);
  1675. { All temps are know, write offsets used for information }
  1676. if (cs_asm_source in aktglobalswitches) then
  1677. begin
  1678. if tg.direction>0 then
  1679. begin
  1680. lotemp:=current_procinfo.tempstart;
  1681. hitemp:=tg.lasttemp;
  1682. end
  1683. else
  1684. begin
  1685. lotemp:=tg.lasttemp;
  1686. hitemp:=current_procinfo.tempstart;
  1687. end;
  1688. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1689. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1690. end;
  1691. { generate target specific proc entry code }
  1692. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1693. end;
  1694. procedure gen_proc_exit_code(list:TAsmList);
  1695. var
  1696. parasize : longint;
  1697. begin
  1698. { c style clearstack does not need to remove parameters from the stack, only the
  1699. return value when it was pushed by arguments }
  1700. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1701. begin
  1702. parasize:=0;
  1703. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1704. inc(parasize,sizeof(aint));
  1705. end
  1706. else
  1707. parasize:=current_procinfo.para_stack_size;
  1708. { generate target specific proc exit code }
  1709. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1710. { release return registers, needed for optimizer }
  1711. if not is_void(current_procinfo.procdef.rettype.def) then
  1712. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1713. { end of frame marker for call frame info }
  1714. current_asmdata.asmcfi.end_frame(list);
  1715. end;
  1716. procedure gen_stack_check_size_para(list:TAsmList);
  1717. var
  1718. paraloc1 : tcgpara;
  1719. begin
  1720. paraloc1.init;
  1721. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1722. paramanager.allocparaloc(list,paraloc1);
  1723. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1724. paramanager.freeparaloc(list,paraloc1);
  1725. paraloc1.done;
  1726. end;
  1727. procedure gen_stack_check_call(list:TAsmList);
  1728. var
  1729. paraloc1 : tcgpara;
  1730. begin
  1731. paraloc1.init;
  1732. { Also alloc the register needed for the parameter }
  1733. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1734. paramanager.allocparaloc(list,paraloc1);
  1735. paramanager.freeparaloc(list,paraloc1);
  1736. { Call the helper }
  1737. cg.allocallcpuregisters(list);
  1738. cg.a_call_name(list,'FPC_STACKCHECK');
  1739. cg.deallocallcpuregisters(list);
  1740. paraloc1.done;
  1741. end;
  1742. procedure gen_save_used_regs(list:TAsmList);
  1743. begin
  1744. { Pure assembler routines need to save the registers themselves }
  1745. if (po_assembler in current_procinfo.procdef.procoptions) then
  1746. exit;
  1747. { oldfpccall expects all registers to be destroyed }
  1748. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1749. cg.g_save_standard_registers(list);
  1750. end;
  1751. procedure gen_restore_used_regs(list:TAsmList);
  1752. begin
  1753. { Pure assembler routines need to save the registers themselves }
  1754. if (po_assembler in current_procinfo.procdef.procoptions) then
  1755. exit;
  1756. { oldfpccall expects all registers to be destroyed }
  1757. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1758. cg.g_restore_standard_registers(list);
  1759. end;
  1760. procedure gen_got_load(list : TAsmList);
  1761. begin
  1762. { if loading got is necessary for more cpus, it can be moved
  1763. to the cg }
  1764. {$ifdef i386}
  1765. { allocate PIC register }
  1766. if (cs_create_pic in aktmoduleswitches) and
  1767. (tf_pic_uses_got in target_info.flags) and
  1768. (pi_needs_got in current_procinfo.flags) then
  1769. begin
  1770. current_module.requires_ebx_pic_helper:=true;
  1771. cg.a_call_name_static(list,'fpc_geteipasebx');
  1772. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),0,NR_PIC_OFFSET_REG));
  1773. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1774. { ecx could be used in leave procedures }
  1775. current_procinfo.got:=NR_EBX;
  1776. end;
  1777. {$endif i386}
  1778. end;
  1779. {****************************************************************************
  1780. External handling
  1781. ****************************************************************************}
  1782. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1783. var
  1784. ref : treference;
  1785. sym : tasmsymbol;
  1786. begin
  1787. { add the procedure to the al_procedures }
  1788. maybe_new_object_file(list);
  1789. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1790. list.concat(Tai_align.create(aktalignment.procalign));
  1791. if (po_global in pd.procoptions) then
  1792. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1793. else
  1794. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1795. {$ifdef x86}
  1796. { fix this for other CPUs as well }
  1797. sym:=current_asmdata.RefAsmSymbol(externalname);
  1798. reference_reset_symbol(ref,sym,0);
  1799. { create pic'ed? }
  1800. if cs_create_pic in aktmoduleswitches then
  1801. begin
  1802. { it could be that we're called from a procedure not having the
  1803. got loaded
  1804. }
  1805. gen_got_load(list);
  1806. ref.refaddr:=addr_pic;
  1807. end
  1808. else
  1809. ref.refaddr:=addr_full;
  1810. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1811. {$else x86}
  1812. cg.a_jmp_name(list,externalname);
  1813. {$endif x86}
  1814. end;
  1815. {****************************************************************************
  1816. Const Data
  1817. ****************************************************************************}
  1818. procedure insertbssdata(sym : tglobalvarsym);
  1819. var
  1820. l : aint;
  1821. varalign : shortint;
  1822. storefilepos : tfileposinfo;
  1823. list : TAsmList;
  1824. sectype : TAsmSectiontype;
  1825. begin
  1826. storefilepos:=aktfilepos;
  1827. aktfilepos:=sym.fileinfo;
  1828. l:=sym.getsize;
  1829. if tf_section_threadvars in target_info.flags then
  1830. begin
  1831. if (vo_is_thread_var in sym.varoptions) then
  1832. begin
  1833. list:=current_asmdata.asmlists[al_threadvars];
  1834. sectype:=sec_threadvar;
  1835. end
  1836. else
  1837. begin
  1838. list:=current_asmdata.asmlists[al_globals];
  1839. sectype:=sec_bss;
  1840. end;
  1841. end
  1842. else
  1843. begin
  1844. if (vo_is_thread_var in sym.varoptions) then
  1845. inc(l,sizeof(aint));
  1846. list:=current_asmdata.asmlists[al_globals];
  1847. sectype:=sec_bss;
  1848. end;
  1849. varalign:=var_align(size_2_align(l));
  1850. maybe_new_object_file(list);
  1851. new_section(list,sectype,lower(sym.mangledname),varalign);
  1852. if (sym.owner.symtabletype=globalsymtable) or
  1853. maybe_smartlink_symbol or
  1854. DLLSource or
  1855. (assigned(current_procinfo) and
  1856. (po_inline in current_procinfo.procdef.procoptions)) or
  1857. (vo_is_exported in sym.varoptions) or
  1858. (vo_is_C_var in sym.varoptions) then
  1859. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1860. else
  1861. list.concat(Tai_datablock.create(sym.mangledname,l));
  1862. aktfilepos:=storefilepos;
  1863. end;
  1864. procedure init_regvar_loc(sym:tabstractnormalvarsym;cgsize:tcgsize);
  1865. begin
  1866. with sym do
  1867. begin
  1868. localloc.size:=cgsize;
  1869. case varregable of
  1870. vr_intreg,
  1871. vr_addr :
  1872. begin
  1873. localloc.loc:=LOC_CREGISTER;
  1874. end;
  1875. vr_fpureg :
  1876. begin
  1877. localloc.loc:=LOC_CFPUREGISTER;
  1878. end;
  1879. vr_mmreg :
  1880. begin
  1881. localloc.loc:=LOC_CMMREGISTER;
  1882. end;
  1883. else
  1884. internalerror(2004101010);
  1885. end;
  1886. end;
  1887. end;
  1888. procedure gen_alloc_symtable(list:TAsmList;st:tsymtable);
  1889. var
  1890. sym : tsym;
  1891. isaddr : boolean;
  1892. cgsize : tcgsize;
  1893. begin
  1894. sym:=tsym(st.symindex.first);
  1895. while assigned(sym) do
  1896. begin
  1897. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1898. begin
  1899. with tabstractnormalvarsym(sym) do
  1900. begin
  1901. { Parameters passed to assembler procedures need to be kept
  1902. in the original location }
  1903. if (sym.typ=paravarsym) and
  1904. (po_assembler in current_procinfo.procdef.procoptions) then
  1905. begin
  1906. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1907. end
  1908. else
  1909. begin
  1910. isaddr:=(st.symtabletype=parasymtable) and
  1911. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1912. if isaddr then
  1913. cgsize:=OS_ADDR
  1914. else
  1915. cgsize:=def_cgsize(vartype.def);
  1916. {$ifndef OLDREGVARS}
  1917. { When there is assembler code we can't use regvars }
  1918. if is_regvar(isaddr) then
  1919. begin
  1920. init_regvar_loc(tabstractnormalvarsym(sym),cgsize);
  1921. if (st.symtabletype <> parasymtable) then
  1922. gen_alloc_regvar(list,tabstractnormalvarsym(sym));
  1923. end
  1924. else
  1925. {$endif NOT OLDREGVARS}
  1926. begin
  1927. localloc.loc:=LOC_REFERENCE;
  1928. localloc.size:=cgsize;
  1929. case st.symtabletype of
  1930. parasymtable :
  1931. begin
  1932. { Reuse the parameter location for values to are at a single location on the stack }
  1933. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1934. begin
  1935. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1936. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1937. end
  1938. else
  1939. begin
  1940. if isaddr then
  1941. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1942. else
  1943. tg.GetLocal(list,getsize,tparavarsym(sym).paraloc[calleeside].alignment,vartype.def,localloc.reference);
  1944. end;
  1945. end;
  1946. localsymtable,
  1947. stt_exceptsymtable :
  1948. begin
  1949. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1950. end;
  1951. staticsymtable :
  1952. begin
  1953. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1954. if not(vo_is_dll_var in varoptions) and ((tf_section_threadvars in target_info.flags) or
  1955. not(vo_is_thread_var in varoptions)) then
  1956. reference_reset_symbol(localloc.reference,current_asmdata.RefAsmSymbol(mangledname),0);
  1957. end;
  1958. else
  1959. internalerror(200410103);
  1960. end;
  1961. end;
  1962. end;
  1963. if cs_asm_source in aktglobalswitches then
  1964. begin
  1965. case localloc.loc of
  1966. LOC_REFERENCE :
  1967. begin
  1968. if not assigned(localloc.reference.symbol) then
  1969. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1970. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1971. end;
  1972. end;
  1973. end;
  1974. end;
  1975. end;
  1976. sym:=tsym(sym.indexnext);
  1977. end;
  1978. end;
  1979. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1980. begin
  1981. case location.loc of
  1982. LOC_CREGISTER:
  1983. {$ifndef cpu64bit}
  1984. if location.size in [OS_64,OS_S64] then
  1985. begin
  1986. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1987. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1988. end
  1989. else
  1990. {$endif cpu64bit}
  1991. rv.intregvars.addnodup(getsupreg(location.register));
  1992. LOC_CFPUREGISTER:
  1993. rv.fpuregvars.addnodup(getsupreg(location.register));
  1994. LOC_CMMREGISTER:
  1995. rv.mmregvars.addnodup(getsupreg(location.register));
  1996. end;
  1997. end;
  1998. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1999. var
  2000. rv: pusedregvars absolute arg;
  2001. begin
  2002. case (n.nodetype) of
  2003. temprefn:
  2004. { We only have to synchronise a tempnode before a loop if it is }
  2005. { not created inside the loop, and only synchronise after the }
  2006. { loop if it's not destroyed inside the loop. If it's created }
  2007. { before the loop and not yet destroyed, then before the loop }
  2008. { is secondpassed tempinfo^.valid will be true, and we get the }
  2009. { correct registers. If it's not destroyed inside the loop, }
  2010. { then after the loop has been secondpassed tempinfo^.valid }
  2011. { be true and we also get the right registers. In other cases, }
  2012. { tempinfo^.valid will be false and so we do not add }
  2013. { unnecessary registers. This way, we don't have to look at }
  2014. { tempcreate and tempdestroy nodes to get this info (JM) }
  2015. if (ttemprefnode(n).tempinfo^.valid) then
  2016. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  2017. loadn:
  2018. if (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  2019. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  2020. end;
  2021. result := fen_true;
  2022. end;
  2023. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  2024. begin
  2025. foreachnodestatic(n,@do_get_used_regvars,@rv);
  2026. end;
  2027. (*
  2028. See comments at declaration of pusedregvarscommon
  2029. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  2030. var
  2031. rv: pusedregvarscommon absolute arg;
  2032. begin
  2033. if (n.nodetype = loadn) and
  2034. (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  2035. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2036. case loc of
  2037. LOC_CREGISTER:
  2038. { if not yet encountered in this node tree }
  2039. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2040. { but nevertheless already encountered somewhere }
  2041. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2042. { then it's a regvar used in two or more node trees }
  2043. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2044. LOC_CFPUREGISTER:
  2045. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2046. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2047. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2048. LOC_CMMREGISTER:
  2049. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2050. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2051. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2052. end;
  2053. result := fen_true;
  2054. end;
  2055. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2056. begin
  2057. rv.myregvars.intregvars.clear;
  2058. rv.myregvars.fpuregvars.clear;
  2059. rv.myregvars.mmregvars.clear;
  2060. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2061. end;
  2062. *)
  2063. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2064. var
  2065. count: longint;
  2066. begin
  2067. for count := 1 to rv.intregvars.length do
  2068. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2069. for count := 1 to rv.fpuregvars.length do
  2070. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2071. for count := 1 to rv.mmregvars.length do
  2072. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2073. end;
  2074. {*****************************************************************************
  2075. SSA support
  2076. *****************************************************************************}
  2077. type
  2078. preplaceregrec = ^treplaceregrec;
  2079. treplaceregrec = record
  2080. old, new: tregister;
  2081. {$ifndef cpu64bit}
  2082. oldhi, newhi: tregister;
  2083. {$endif cpu64bit}
  2084. ressym: tsym;
  2085. end;
  2086. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  2087. var
  2088. rr: preplaceregrec absolute para;
  2089. begin
  2090. result := fen_false;
  2091. case n.nodetype of
  2092. loadn:
  2093. begin
  2094. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  2095. not assigned(tloadnode(n).left) and
  2096. (((tloadnode(n).symtableentry <> rr^.ressym) and
  2097. not(vo_is_funcret in tabstractvarsym(tloadnode(n).symtableentry).varoptions)) or
  2098. not(fc_exit in flowcontrol)) and
  2099. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2100. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  2101. begin
  2102. {$ifndef cpu64bit}
  2103. { it's possible a 64 bit location was shifted and/xor typecasted }
  2104. { in a 32 bit value, so only 1 register was left in the location }
  2105. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  2106. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  2107. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  2108. else
  2109. exit;
  2110. {$endif cpu64bit}
  2111. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  2112. result := fen_norecurse_true;
  2113. end;
  2114. end;
  2115. temprefn:
  2116. begin
  2117. if (ttemprefnode(n).tempinfo^.valid) and
  2118. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2119. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  2120. begin
  2121. {$ifndef cpu64bit}
  2122. { it's possible a 64 bit location was shifted and/xor typecasted }
  2123. { in a 32 bit value, so only 1 register was left in the location }
  2124. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  2125. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  2126. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  2127. else
  2128. exit;
  2129. {$endif cpu64bit}
  2130. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  2131. result := fen_norecurse_true;
  2132. end;
  2133. end;
  2134. end;
  2135. end;
  2136. procedure maybechangeloadnodereg(var n: tnode);
  2137. var
  2138. rr: treplaceregrec;
  2139. begin
  2140. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  2141. ([fc_inflowcontrol,fc_gotolabel] * flowcontrol <> []) then
  2142. exit;
  2143. rr.old := n.location.register;
  2144. rr.ressym := nil;
  2145. {$ifndef cpu64bit}
  2146. rr.oldhi := NR_NO;
  2147. {$endif cpu64bit}
  2148. case n.location.loc of
  2149. LOC_CREGISTER:
  2150. begin
  2151. {$ifndef cpu64bit}
  2152. if (n.location.size in [OS_64,OS_S64]) then
  2153. begin
  2154. rr.oldhi := n.location.register64.reghi;
  2155. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2156. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2157. end
  2158. else
  2159. {$endif cpu64bit}
  2160. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  2161. end;
  2162. LOC_CFPUREGISTER:
  2163. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  2164. {$ifdef SUPPORT_MMX}
  2165. LOC_CMMXREGISTER:
  2166. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  2167. {$endif SUPPORT_MMX}
  2168. LOC_CMMREGISTER:
  2169. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  2170. else
  2171. exit;
  2172. end;
  2173. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  2174. assigned(current_procinfo.procdef.funcretsym) and
  2175. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  2176. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2177. rr.ressym:=tsym(current_procinfo.procdef.parast.search('self'))
  2178. else
  2179. rr.ressym:=current_procinfo.procdef.funcretsym;
  2180. if not foreachnodestatic(n,@doreplace,@rr) then
  2181. exit;
  2182. { now that we've change the loadn/temp, also change the node result location }
  2183. {$ifndef cpu64bit}
  2184. if (n.location.size in [OS_64,OS_S64]) then
  2185. begin
  2186. n.location.register64.reglo := rr.new;
  2187. n.location.register64.reghi := rr.newhi;
  2188. end
  2189. else
  2190. {$endif cpu64bit}
  2191. n.location.register := rr.new;
  2192. end;
  2193. procedure gen_free_symtable(list:TAsmList;st:tsymtable);
  2194. var
  2195. sym : tsym;
  2196. begin
  2197. sym:=tsym(st.symindex.first);
  2198. while assigned(sym) do
  2199. begin
  2200. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  2201. begin
  2202. with tabstractnormalvarsym(sym) do
  2203. begin
  2204. { Note: We need to keep the data available in memory
  2205. for the sub procedures that can access local data
  2206. in the parent procedures }
  2207. case localloc.loc of
  2208. LOC_CREGISTER :
  2209. {$ifndef cpu64bit}
  2210. if (pi_has_goto in current_procinfo.flags) then
  2211. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  2212. begin
  2213. cg.a_reg_sync(list,localloc.register64.reglo);
  2214. cg.a_reg_sync(list,localloc.register64.reghi);
  2215. end
  2216. else
  2217. {$endif cpu64bit}
  2218. cg.a_reg_sync(list,localloc.register);
  2219. LOC_CFPUREGISTER,
  2220. LOC_CMMREGISTER:
  2221. if (pi_has_goto in current_procinfo.flags) then
  2222. cg.a_reg_sync(list,localloc.register);
  2223. LOC_REFERENCE :
  2224. begin
  2225. case st.symtabletype of
  2226. localsymtable,
  2227. parasymtable,
  2228. stt_exceptsymtable :
  2229. tg.Ungetlocal(list,localloc.reference);
  2230. end;
  2231. end;
  2232. end;
  2233. end;
  2234. end;
  2235. sym:=tsym(sym.indexnext);
  2236. end;
  2237. end;
  2238. { persistent rtti generation }
  2239. procedure generate_rtti(p:Ttypesym);
  2240. var
  2241. rsym : trttisym;
  2242. def : tstoreddef;
  2243. begin
  2244. { rtti can only be generated for classes that are always typesyms }
  2245. def:=tstoreddef(ttypesym(p).restype.def);
  2246. { there is an error, skip rtti info }
  2247. if (def.deftype=errordef) or (Errorcount>0) then
  2248. exit;
  2249. { only create rtti once for each definition }
  2250. if not(df_has_rttitable in def.defoptions) then
  2251. begin
  2252. { definition should be in the same symtable as the symbol }
  2253. if p.owner<>def.owner then
  2254. internalerror(200108262);
  2255. { create rttisym }
  2256. rsym:=trttisym.create(p.name,fullrtti);
  2257. p.owner.insert(rsym);
  2258. { register rttisym in definition }
  2259. include(def.defoptions,df_has_rttitable);
  2260. def.rttitablesym:=rsym;
  2261. { write rtti data }
  2262. def.write_child_rtti_data(fullrtti);
  2263. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  2264. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2265. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2266. def.write_rtti_data(fullrtti);
  2267. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2268. end;
  2269. end;
  2270. { persistent init table generation }
  2271. procedure generate_inittable(p:tsym);
  2272. var
  2273. rsym : trttisym;
  2274. def : tstoreddef;
  2275. begin
  2276. { anonymous types are also allowed for records that can be varsym }
  2277. case p.typ of
  2278. typesym :
  2279. def:=tstoreddef(ttypesym(p).restype.def);
  2280. globalvarsym,
  2281. localvarsym,
  2282. paravarsym :
  2283. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2284. else
  2285. internalerror(200108263);
  2286. end;
  2287. { only create inittable once for each definition }
  2288. if not(df_has_inittable in def.defoptions) then
  2289. begin
  2290. { definition should be in the same symtable as the symbol }
  2291. if p.owner<>def.owner then
  2292. internalerror(200108264);
  2293. { create rttisym }
  2294. rsym:=trttisym.create(p.name,initrtti);
  2295. p.owner.insert(rsym);
  2296. { register rttisym in definition }
  2297. include(def.defoptions,df_has_inittable);
  2298. def.inittablesym:=rsym;
  2299. { write inittable data }
  2300. def.write_child_rtti_data(initrtti);
  2301. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  2302. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2303. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2304. def.write_rtti_data(initrtti);
  2305. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2306. end;
  2307. end;
  2308. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2309. var
  2310. i,j,
  2311. proccount : longint;
  2312. tmps : string;
  2313. begin
  2314. for i:=1 to _class.implementedinterfaces.count do
  2315. begin
  2316. { only if implemented by this class }
  2317. if _class.implementedinterfaces.implindex(i)=i then
  2318. begin
  2319. proccount:=_class.implementedinterfaces.implproccount(i);
  2320. for j:=1 to proccount do
  2321. begin
  2322. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2323. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2324. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2325. { create wrapper code }
  2326. new_section(list,sec_code,lower(tmps),0);
  2327. cg.init_register_allocators;
  2328. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2329. cg.done_register_allocators;
  2330. end;
  2331. end;
  2332. end;
  2333. end;
  2334. procedure gen_intf_wrappers(list:TAsmList;st:tsymtable);
  2335. var
  2336. def : tstoreddef;
  2337. begin
  2338. def:=tstoreddef(st.defindex.first);
  2339. while assigned(def) do
  2340. begin
  2341. if is_class(def) then
  2342. gen_intf_wrapper(list,tobjectdef(def));
  2343. def:=tstoreddef(def.indexnext);
  2344. end;
  2345. end;
  2346. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2347. var
  2348. href : treference;
  2349. begin
  2350. if is_object(objdef) then
  2351. begin
  2352. case selfloc.loc of
  2353. LOC_CREFERENCE,
  2354. LOC_REFERENCE:
  2355. begin
  2356. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2357. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2358. end;
  2359. else
  2360. internalerror(200305056);
  2361. end;
  2362. end
  2363. else
  2364. begin
  2365. case selfloc.loc of
  2366. LOC_REGISTER:
  2367. begin
  2368. {$ifdef cpu_uses_separate_address_registers}
  2369. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2370. begin
  2371. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2372. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2373. end
  2374. else
  2375. {$endif cpu_uses_separate_address_registers}
  2376. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2377. end;
  2378. LOC_CREGISTER,
  2379. LOC_CREFERENCE,
  2380. LOC_REFERENCE:
  2381. begin
  2382. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2383. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2384. end;
  2385. else
  2386. internalerror(200305057);
  2387. end;
  2388. end;
  2389. vmtreg:=cg.getaddressregister(list);
  2390. cg.g_maybe_testself(list,href.base);
  2391. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2392. { test validity of VMT }
  2393. if not(is_interface(objdef)) and
  2394. not(is_cppclass(objdef)) then
  2395. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2396. end;
  2397. function getprocalign : shortint;
  2398. begin
  2399. { gprof uses 16 byte granularity }
  2400. if (cs_profile in aktmoduleswitches) then
  2401. result:=16
  2402. else
  2403. result:=aktalignment.procalign;
  2404. end;
  2405. procedure gen_pic_helpers(list : TAsmList);
  2406. var
  2407. href : treference;
  2408. begin
  2409. { if other cpus require such helpers as well, it can be solved more cleaner }
  2410. {$ifdef i386}
  2411. if current_module.requires_ebx_pic_helper then
  2412. begin
  2413. new_section(list,sec_code,'fpc_geteipasebx',0);
  2414. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2415. reference_reset(href);
  2416. href.base:=NR_ESP;
  2417. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2418. list.concat(taicpu.op_none(A_RET,S_NO));
  2419. end;
  2420. if current_module.requires_ecx_pic_helper then
  2421. begin
  2422. new_section(list,sec_code,'fpc_geteipasecx',0);
  2423. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2424. reference_reset(href);
  2425. href.base:=NR_ESP;
  2426. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2427. list.concat(taicpu.op_none(A_RET,S_NO));
  2428. end;
  2429. {$endif i386}
  2430. end;
  2431. end.