ncgutil.pas 95 KB

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