ncgutil.pas 109 KB

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