2
0

ncgutil.pas 88 KB

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