ncgutil.pas 94 KB

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