ncgutil.pas 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  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. { Retrieve the location of the data pointed to in location l, when the location is
  55. a register it is expected to contain the address of the data }
  56. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  57. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  58. function has_alias_name(pd:tprocdef;const s:string):boolean;
  59. procedure alloc_proc_symbol(pd: tprocdef);
  60. procedure gen_proc_symbol(list:TAsmList);
  61. procedure gen_proc_symbol_end(list:TAsmList);
  62. procedure gen_proc_entry_code(list:TAsmList);
  63. procedure gen_proc_exit_code(list:TAsmList);
  64. procedure gen_stack_check_size_para(list:TAsmList);
  65. procedure gen_stack_check_call(list:TAsmList);
  66. procedure gen_save_used_regs(list:TAsmList);
  67. procedure gen_restore_used_regs(list:TAsmList);
  68. procedure gen_initialize_code(list:TAsmList);
  69. procedure gen_finalize_code(list:TAsmList);
  70. procedure gen_entry_code(list:TAsmList);
  71. procedure gen_exit_code(list:TAsmList);
  72. procedure gen_load_para_value(list:TAsmList);
  73. procedure gen_load_return_value(list:TAsmList);
  74. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  75. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  76. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  77. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  78. { adds the regvars used in n and its children to rv.allregvars,
  79. those which were already in rv.allregvars to rv.commonregvars and
  80. uses rv.myregvars as scratch (so that two uses of the same regvar
  81. in a single tree to make it appear in commonregvars). Useful to
  82. find out which regvars are used in two different node trees
  83. (e.g. in the "else" and "then" path, or in various case blocks }
  84. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  85. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  86. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  87. { loadn and change its location to a new register (= SSA). In case reload }
  88. { is true, transfer the old to the new register }
  89. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  90. {#
  91. Allocate the buffers for exception management and setjmp environment.
  92. Return a pointer to these buffers, send them to the utility routine
  93. so they are registered, and then call setjmp.
  94. Then compare the result of setjmp with 0, and if not equal
  95. to zero, then jump to exceptlabel.
  96. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  97. It is to note that this routine may be called *after* the stackframe of a
  98. routine has been called, therefore on machines where the stack cannot
  99. be modified, all temps should be allocated on the heap instead of the
  100. stack.
  101. }
  102. const
  103. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  104. type
  105. texceptiontemps=record
  106. jmpbuf,
  107. envbuf,
  108. reasonbuf : treference;
  109. end;
  110. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  111. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  112. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  113. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  114. procedure insertbssdata(sym : tstaticvarsym);
  115. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  116. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  117. procedure location_free(list: TAsmList; const location : TLocation);
  118. function getprocalign : shortint;
  119. procedure gen_pic_helpers(list : TAsmList);
  120. procedure gen_got_load(list : TAsmList);
  121. implementation
  122. uses
  123. version,
  124. cutils,cclasses,
  125. globals,systems,verbose,
  126. ppu,defutil,
  127. procinfo,paramgr,fmodule,
  128. regvars,dbgbase,
  129. pass_1,pass_2,
  130. nbas,ncon,nld,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 location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  659. var
  660. reg : tregister;
  661. begin
  662. if (l.loc<>LOC_MMREGISTER) and
  663. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  664. begin
  665. reg:=cg.getmmregister(list,OS_VECTOR);
  666. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  667. location_freetemp(list,l);
  668. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  669. l.register:=reg;
  670. end;
  671. end;
  672. procedure location_force_mem(list:TAsmList;var l:tlocation);
  673. var
  674. r : treference;
  675. begin
  676. case l.loc of
  677. LOC_FPUREGISTER,
  678. LOC_CFPUREGISTER :
  679. begin
  680. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  681. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,r);
  682. location_reset(l,LOC_REFERENCE,l.size);
  683. l.reference:=r;
  684. end;
  685. LOC_MMREGISTER,
  686. LOC_CMMREGISTER:
  687. begin
  688. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  689. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  690. location_reset(l,LOC_REFERENCE,l.size);
  691. l.reference:=r;
  692. end;
  693. LOC_CONSTANT,
  694. LOC_REGISTER,
  695. LOC_CREGISTER :
  696. begin
  697. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  698. {$ifndef cpu64bit}
  699. if l.size in [OS_64,OS_S64] then
  700. cg64.a_load64_loc_ref(list,l,r)
  701. else
  702. {$endif cpu64bit}
  703. cg.a_load_loc_ref(list,l.size,l,r);
  704. location_reset(l,LOC_REFERENCE,l.size);
  705. l.reference:=r;
  706. end;
  707. LOC_SUBSETREG,
  708. LOC_CSUBSETREG,
  709. LOC_SUBSETREF,
  710. LOC_CSUBSETREF:
  711. begin
  712. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  713. cg.a_load_loc_ref(list,l.size,l,r);
  714. location_reset(l,LOC_REFERENCE,l.size);
  715. l.reference:=r;
  716. end;
  717. LOC_CREFERENCE,
  718. LOC_REFERENCE : ;
  719. else
  720. internalerror(200203219);
  721. end;
  722. end;
  723. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  724. begin
  725. case l.loc of
  726. LOC_REGISTER,
  727. LOC_CREGISTER :
  728. begin
  729. if not loadref then
  730. internalerror(200410231);
  731. reference_reset_base(ref,l.register,0);
  732. end;
  733. LOC_REFERENCE,
  734. LOC_CREFERENCE :
  735. begin
  736. if loadref then
  737. begin
  738. reference_reset_base(ref,cg.getaddressregister(list),0);
  739. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  740. end
  741. else
  742. ref:=l.reference;
  743. end;
  744. else
  745. internalerror(200309181);
  746. end;
  747. end;
  748. {*****************************************************************************
  749. Maybe_Save
  750. *****************************************************************************}
  751. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  752. begin
  753. {$ifdef i386}
  754. if (needed>=maxfpuregs) and
  755. (l.loc = LOC_FPUREGISTER) then
  756. begin
  757. location_force_mem(list,l);
  758. maybe_pushfpu:=true;
  759. end
  760. else
  761. maybe_pushfpu:=false;
  762. {$else i386}
  763. maybe_pushfpu:=false;
  764. {$endif i386}
  765. end;
  766. {****************************************************************************
  767. Init/Finalize Code
  768. ****************************************************************************}
  769. procedure copyvalueparas(p:TObject;arg:pointer);
  770. var
  771. href : treference;
  772. hreg : tregister;
  773. list : TAsmList;
  774. hsym : tparavarsym;
  775. l : longint;
  776. localcopyloc : tlocation;
  777. begin
  778. list:=TAsmList(arg);
  779. if (tsym(p).typ=paravarsym) and
  780. (tparavarsym(p).varspez=vs_value) and
  781. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  782. begin
  783. location_get_data_ref(list,tparavarsym(p).initialloc,href,true);
  784. if is_open_array(tparavarsym(p).vardef) or
  785. is_array_of_const(tparavarsym(p).vardef) then
  786. begin
  787. { cdecl functions don't have a high pointer so it is not possible to generate
  788. a local copy }
  789. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  790. begin
  791. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  792. if not assigned(hsym) then
  793. internalerror(200306061);
  794. hreg:=cg.getaddressregister(list);
  795. if not is_packed_array(tparavarsym(p).vardef) then
  796. cg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef).elesize,hreg)
  797. else
  798. internalerror(2006080401);
  799. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  800. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).initialloc);
  801. end;
  802. end
  803. else
  804. begin
  805. { Allocate space for the local copy }
  806. l:=tparavarsym(p).getsize;
  807. localcopyloc.loc:=LOC_REFERENCE;
  808. localcopyloc.size:=int_cgsize(l);
  809. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  810. { Copy data }
  811. if is_shortstring(tparavarsym(p).vardef) then
  812. begin
  813. { this code is only executed before the code for the body and the entry/exit code is generated
  814. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  815. }
  816. include(current_procinfo.flags,pi_do_call);
  817. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef).len)
  818. end
  819. else if tparavarsym(p).vardef.typ = variantdef then
  820. begin
  821. { this code is only executed before the code for the body and the entry/exit code is generated
  822. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  823. }
  824. include(current_procinfo.flags,pi_do_call);
  825. cg.g_copyvariant(list,href,localcopyloc.reference)
  826. end
  827. else
  828. begin
  829. { pass proper alignment info }
  830. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  831. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  832. end;
  833. { update localloc of varsym }
  834. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  835. tparavarsym(p).localloc:=localcopyloc;
  836. tparavarsym(p).initialloc:=localcopyloc;
  837. end;
  838. end;
  839. end;
  840. const
  841. {$ifdef cpu64bit}
  842. trashintvalues: array[0..nroftrashvalues-1] of aint = ($5555555555555555,aint($AAAAAAAAAAAAAAAA),aint($EFEFEFEFEFEFEFEF),0);
  843. {$else cpu64bit}
  844. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55555555,aint($AAAAAAAA),aint($EFEFEFEF),0);
  845. {$endif cpu64bit}
  846. procedure trash_reference(list: TAsmList; const ref: treference; size: aint);
  847. var
  848. countreg, valuereg: tregister;
  849. hl: tasmlabel;
  850. trashintval: aint;
  851. tmpref: treference;
  852. begin
  853. trashintval := trashintvalues[localvartrashing];
  854. case size of
  855. 0: ; { empty record }
  856. 1: cg.a_load_const_ref(list,OS_8,byte(trashintval),ref);
  857. 2: cg.a_load_const_ref(list,OS_16,word(trashintval),ref);
  858. 4: cg.a_load_const_ref(list,OS_32,longint(trashintval),ref);
  859. {$ifdef cpu64bit}
  860. 8: cg.a_load_const_ref(list,OS_64,int64(trashintval),ref);
  861. {$endif cpu64bit}
  862. else
  863. begin
  864. countreg := cg.getintregister(list,OS_ADDR);
  865. valuereg := cg.getintregister(list,OS_8);
  866. cg.a_load_const_reg(list,OS_INT,size,countreg);
  867. cg.a_load_const_reg(list,OS_8,byte(trashintval),valuereg);
  868. current_asmdata.getjumplabel(hl);
  869. tmpref := ref;
  870. if (tmpref.index <> NR_NO) then
  871. internalerror(200607201);
  872. tmpref.index := countreg;
  873. dec(tmpref.offset);
  874. cg.a_label(list,hl);
  875. cg.a_load_reg_ref(list,OS_8,OS_8,valuereg,tmpref);
  876. cg.a_op_const_reg(list,OP_SUB,OS_INT,1,countreg);
  877. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,countreg,hl);
  878. cg.a_reg_sync(list,tmpref.base);
  879. cg.a_reg_sync(list,valuereg);
  880. end;
  881. end;
  882. end;
  883. { trash contents of local variables or parameters (function result) }
  884. procedure trash_variable(p:TObject;arg:pointer);
  885. var
  886. trashintval: aint;
  887. list: TAsmList absolute arg;
  888. begin
  889. if ((tsym(p).typ=localvarsym) or
  890. ((tsym(p).typ=paravarsym) and
  891. (vo_is_funcret in tparavarsym(p).varoptions))) and
  892. not(tabstractnormalvarsym(p).vardef.needs_inittable) and
  893. not(assigned(tabstractnormalvarsym(p).defaultconstsym)) then
  894. begin
  895. trashintval := trashintvalues[localvartrashing];
  896. case tabstractnormalvarsym(p).initialloc.loc of
  897. LOC_CREGISTER :
  898. {$ifopt q+}
  899. {$define overflowon}
  900. {$q-}
  901. {$endif}
  902. cg.a_load_const_reg(list,reg_cgsize(tabstractnormalvarsym(p).initialloc.register),
  903. trashintval and (aword(1) shl (tcgsize2size[reg_cgsize(tabstractnormalvarsym(p).initialloc.register)] * 8) - 1),
  904. tabstractnormalvarsym(p).initialloc.register);
  905. {$ifdef overflowon}
  906. {$undef overflowon}
  907. {$q+}
  908. {$endif}
  909. LOC_REFERENCE :
  910. begin
  911. if ((tsym(p).typ=localvarsym) and
  912. not(vo_is_funcret in tabstractvarsym(p).varoptions)) or
  913. not is_shortstring(tabstractnormalvarsym(p).vardef) then
  914. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  915. tlocalvarsym(p).getsize)
  916. else
  917. { may be an open string, even if is_open_string() returns }
  918. { false for some helpers in the system unit }
  919. { an open string has at least size 2 }
  920. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  921. 2);
  922. end;
  923. LOC_CMMREGISTER :
  924. ;
  925. LOC_CFPUREGISTER :
  926. ;
  927. else
  928. internalerror(200410124);
  929. end;
  930. end;
  931. end;
  932. { initializes the regvars from staticsymtable with 0 }
  933. procedure initialize_regvars(p:TObject;arg:pointer);
  934. var
  935. href : treference;
  936. begin
  937. if (tsym(p).typ=staticvarsym) then
  938. begin
  939. { Static variables can have the initialloc only set to LOC_CxREGISTER
  940. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  941. case tstaticvarsym(p).initialloc.loc of
  942. LOC_CREGISTER :
  943. begin
  944. {$ifndef cpu64bit}
  945. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  946. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  947. else
  948. {$endif not cpu64bit}
  949. cg.a_load_const_reg(TAsmList(arg),reg_cgsize(tstaticvarsym(p).initialloc.register),0,
  950. tstaticvarsym(p).initialloc.register);
  951. end;
  952. LOC_CMMREGISTER :
  953. { clear the whole register }
  954. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  955. tstaticvarsym(p).initialloc.register,
  956. tstaticvarsym(p).initialloc.register,
  957. nil);
  958. LOC_CFPUREGISTER :
  959. begin
  960. { initialize fpu regvar by loading from memory }
  961. reference_reset_symbol(href,
  962. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0);
  963. cg.a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).initialloc.size,
  964. tstaticvarsym(p).initialloc.size, href, tstaticvarsym(p).initialloc.register);
  965. end;
  966. LOC_INVALID :
  967. ;
  968. else
  969. internalerror(200410124);
  970. end;
  971. end;
  972. end;
  973. { generates the code for initialisation of local data }
  974. procedure initialize_data(p:TObject;arg:pointer);
  975. var
  976. OldAsmList : TAsmList;
  977. hp : tnode;
  978. begin
  979. if (tsym(p).typ in [staticvarsym,localvarsym]) and
  980. (tabstractvarsym(p).refs>0) and
  981. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  982. not(vo_is_external in tabstractvarsym(p).varoptions) and
  983. not(is_class(tabstractvarsym(p).vardef)) and
  984. tabstractvarsym(p).vardef.needs_inittable then
  985. begin
  986. OldAsmList:=current_asmdata.CurrAsmList;
  987. current_asmdata.CurrAsmList:=TAsmList(arg);
  988. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  989. firstpass(hp);
  990. secondpass(hp);
  991. hp.free;
  992. current_asmdata.CurrAsmList:=OldAsmList;
  993. end;
  994. end;
  995. procedure finalize_sym(asmlist:TAsmList;sym:tsym);
  996. var
  997. hp : tnode;
  998. OldAsmList : TAsmList;
  999. begin
  1000. include(current_procinfo.flags,pi_needs_implicit_finally);
  1001. OldAsmList:=current_asmdata.CurrAsmList;
  1002. current_asmdata.CurrAsmList:=asmlist;
  1003. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  1004. firstpass(hp);
  1005. secondpass(hp);
  1006. hp.free;
  1007. current_asmdata.CurrAsmList:=OldAsmList;
  1008. end;
  1009. { generates the code for finalisation of local variables }
  1010. procedure finalize_local_vars(p:TObject;arg:pointer);
  1011. begin
  1012. if (tsym(p).typ=localvarsym) and
  1013. (tlocalvarsym(p).refs>0) and
  1014. not(vo_is_external in tlocalvarsym(p).varoptions) and
  1015. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  1016. not(is_class(tlocalvarsym(p).vardef)) and
  1017. tlocalvarsym(p).vardef.needs_inittable then
  1018. finalize_sym(TAsmList(arg),tsym(p));
  1019. end;
  1020. { generates the code for finalization of static symtable and
  1021. all local (static) typed consts }
  1022. procedure finalize_static_data(p:TObject;arg:pointer);
  1023. var
  1024. i : longint;
  1025. pd : tprocdef;
  1026. begin
  1027. case tsym(p).typ of
  1028. staticvarsym :
  1029. begin
  1030. if (tstaticvarsym(p).refs>0) and
  1031. (tstaticvarsym(p).varspez<>vs_const) and
  1032. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  1033. not(vo_is_external in tstaticvarsym(p).varoptions) and
  1034. not(is_class(tstaticvarsym(p).vardef)) and
  1035. tstaticvarsym(p).vardef.needs_inittable then
  1036. finalize_sym(TAsmList(arg),tsym(p));
  1037. end;
  1038. procsym :
  1039. begin
  1040. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  1041. begin
  1042. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  1043. if assigned(pd.localst) and
  1044. (pd.procsym=tprocsym(p)) and
  1045. (pd.localst.symtabletype<>staticsymtable) then
  1046. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  1047. end;
  1048. end;
  1049. end;
  1050. end;
  1051. { generates the code for incrementing the reference count of parameters and
  1052. initialize out parameters }
  1053. procedure init_paras(p:TObject;arg:pointer);
  1054. var
  1055. href : treference;
  1056. tmpreg : tregister;
  1057. list : TAsmList;
  1058. needs_inittable,
  1059. do_trashing : boolean;
  1060. begin
  1061. list:=TAsmList(arg);
  1062. if (tsym(p).typ=paravarsym) then
  1063. begin
  1064. needs_inittable :=
  1065. not is_class_or_interface(tparavarsym(p).vardef) and
  1066. tparavarsym(p).vardef.needs_inittable;
  1067. do_trashing :=
  1068. (localvartrashing <> -1) and
  1069. (not assigned(tparavarsym(p).defaultconstsym)) and
  1070. (not tparavarsym(p).vardef.needs_inittable or
  1071. is_class(tparavarsym(p).vardef));
  1072. case tparavarsym(p).varspez of
  1073. vs_value :
  1074. if needs_inittable then
  1075. begin
  1076. { variants are already handled by the call to fpc_variant_copy_overwrite }
  1077. if tparavarsym(p).vardef.typ <> variantdef then begin
  1078. location_get_data_ref(list,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef));
  1079. cg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  1080. end;
  1081. end;
  1082. vs_out :
  1083. begin
  1084. if needs_inittable or
  1085. do_trashing then
  1086. begin
  1087. tmpreg:=cg.getaddressregister(list);
  1088. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1089. reference_reset_base(href,tmpreg,0);
  1090. if do_trashing and
  1091. { needs separate implementation to trash open arrays }
  1092. { since their size is only known at run time }
  1093. not is_special_array(tparavarsym(p).vardef) then
  1094. trash_reference(list,href,tparavarsym(p).vardef.size);
  1095. if needs_inittable then
  1096. cg.g_initialize(list,tparavarsym(p).vardef,href);
  1097. end;
  1098. end;
  1099. else if do_trashing and
  1100. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  1101. begin
  1102. tmpreg:=cg.getaddressregister(list);
  1103. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1104. reference_reset_base(href,tmpreg,0);
  1105. { may be an open string, even if is_open_string() returns }
  1106. { false for some helpers in the system unit }
  1107. if not is_shortstring(tparavarsym(p).vardef) then
  1108. trash_reference(list,href,tparavarsym(p).vardef.size)
  1109. else
  1110. { an open string has at least size 2 }
  1111. trash_reference(list,href,2);
  1112. end
  1113. end;
  1114. end;
  1115. end;
  1116. { generates the code for decrementing the reference count of parameters }
  1117. procedure final_paras(p:TObject;arg:pointer);
  1118. var
  1119. list : TAsmList;
  1120. href : treference;
  1121. begin
  1122. if not(tsym(p).typ=paravarsym) then
  1123. exit;
  1124. list:=TAsmList(arg);
  1125. if not is_class_or_interface(tparavarsym(p).vardef) and
  1126. tparavarsym(p).vardef.needs_inittable then
  1127. begin
  1128. if (tparavarsym(p).varspez=vs_value) then
  1129. begin
  1130. include(current_procinfo.flags,pi_needs_implicit_finally);
  1131. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef));
  1132. cg.g_decrrefcount(list,tparavarsym(p).vardef,href);
  1133. end;
  1134. end;
  1135. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  1136. if (tparavarsym(p).varspez=vs_value) and
  1137. (is_open_array(tparavarsym(p).vardef) or
  1138. is_array_of_const(tparavarsym(p).vardef)) then
  1139. begin
  1140. { cdecl functions don't have a high pointer so it is not possible to generate
  1141. a local copy }
  1142. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1143. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  1144. end;
  1145. end;
  1146. { Initialize temp ansi/widestrings,interfaces }
  1147. procedure inittempvariables(list:TAsmList);
  1148. var
  1149. hp : ptemprecord;
  1150. href : treference;
  1151. begin
  1152. hp:=tg.templist;
  1153. while assigned(hp) do
  1154. begin
  1155. if assigned(hp^.def) and
  1156. hp^.def.needs_inittable then
  1157. begin
  1158. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1159. cg.g_initialize(list,hp^.def,href);
  1160. end;
  1161. hp:=hp^.next;
  1162. end;
  1163. end;
  1164. procedure finalizetempvariables(list:TAsmList);
  1165. var
  1166. hp : ptemprecord;
  1167. href : treference;
  1168. begin
  1169. hp:=tg.templist;
  1170. while assigned(hp) do
  1171. begin
  1172. if assigned(hp^.def) and
  1173. hp^.def.needs_inittable then
  1174. begin
  1175. include(current_procinfo.flags,pi_needs_implicit_finally);
  1176. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1177. cg.g_finalize(list,hp^.def,href);
  1178. end;
  1179. hp:=hp^.next;
  1180. end;
  1181. end;
  1182. procedure gen_load_return_value(list:TAsmList);
  1183. var
  1184. {$ifndef cpu64bit}
  1185. href : treference;
  1186. {$endif cpu64bit}
  1187. ressym : tabstractnormalvarsym;
  1188. resloc,
  1189. restmploc : tlocation;
  1190. hreg : tregister;
  1191. funcretloc : tlocation;
  1192. begin
  1193. { Is the loading needed? }
  1194. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1195. (
  1196. (po_assembler in current_procinfo.procdef.procoptions) and
  1197. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1198. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1199. ) then
  1200. exit;
  1201. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1202. { constructors return self }
  1203. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1204. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  1205. else
  1206. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1207. if (ressym.refs>0) then
  1208. begin
  1209. {$ifdef OLDREGVARS}
  1210. case ressym.localloc.loc of
  1211. LOC_CFPUREGISTER,
  1212. LOC_FPUREGISTER:
  1213. begin
  1214. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1215. restmploc.register:=ressym.localloc.register;
  1216. end;
  1217. LOC_CREGISTER,
  1218. LOC_REGISTER:
  1219. begin
  1220. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1221. restmploc.register:=ressym.localloc.register;
  1222. end;
  1223. LOC_MMREGISTER:
  1224. begin
  1225. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1226. restmploc.register:=ressym.localloc.register;
  1227. end;
  1228. LOC_REFERENCE:
  1229. begin
  1230. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1231. restmploc.reference:=ressym.localloc.reference;
  1232. end;
  1233. else
  1234. internalerror(200309184);
  1235. end;
  1236. {$else}
  1237. restmploc:=ressym.localloc;
  1238. {$endif}
  1239. { Here, we return the function result. In most architectures, the value is
  1240. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1241. function returns in a register and the caller receives it in an other one }
  1242. case funcretloc.loc of
  1243. LOC_REGISTER:
  1244. begin
  1245. {$ifndef cpu64bit}
  1246. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1247. begin
  1248. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1249. if resloc.loc<>LOC_REGISTER then
  1250. internalerror(200409141);
  1251. { Load low and high register separate to generate better register
  1252. allocation info }
  1253. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1254. begin
  1255. cg.getcpuregister(list,resloc.register64.reglo);
  1256. end;
  1257. case restmploc.loc of
  1258. LOC_REFERENCE :
  1259. begin
  1260. href:=restmploc.reference;
  1261. if target_info.endian=ENDIAN_BIG then
  1262. inc(href.offset,4);
  1263. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1264. end;
  1265. LOC_CREGISTER :
  1266. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1267. else
  1268. internalerror(200409203);
  1269. end;
  1270. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1271. begin
  1272. cg.getcpuregister(list,resloc.register64.reghi);
  1273. end;
  1274. case restmploc.loc of
  1275. LOC_REFERENCE :
  1276. begin
  1277. href:=restmploc.reference;
  1278. if target_info.endian=ENDIAN_LITTLE then
  1279. inc(href.offset,4);
  1280. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1281. end;
  1282. LOC_CREGISTER :
  1283. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1284. else
  1285. internalerror(200409204);
  1286. end;
  1287. end
  1288. else
  1289. {$endif cpu64bit}
  1290. begin
  1291. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1292. if getsupreg(funcretloc.register)<first_int_imreg then
  1293. begin
  1294. cg.getcpuregister(list,funcretloc.register);
  1295. end;
  1296. { it could be that a structure is passed in memory but the function is expected to
  1297. return a pointer to this memory }
  1298. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1299. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1300. else
  1301. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1302. end;
  1303. end;
  1304. LOC_FPUREGISTER:
  1305. begin
  1306. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1307. begin
  1308. cg.getcpuregister(list,funcretloc.register);
  1309. end;
  1310. { we can't do direct moves between fpu and mm registers }
  1311. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1312. location_force_fpureg(list,restmploc,false);
  1313. cg.a_loadfpu_loc_reg(list,funcretloc.size,restmploc,funcretloc.register);
  1314. end;
  1315. LOC_MMREGISTER:
  1316. begin
  1317. if getsupreg(funcretloc.register)<first_mm_imreg then
  1318. begin
  1319. cg.getcpuregister(list,funcretloc.register);
  1320. end;
  1321. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1322. end;
  1323. LOC_INVALID,
  1324. LOC_REFERENCE:
  1325. ;
  1326. else
  1327. internalerror(200405025);
  1328. end;
  1329. end
  1330. {$ifdef x86}
  1331. else
  1332. begin
  1333. { the caller will pop a value off the cpu stack }
  1334. if (funcretloc.loc = LOC_FPUREGISTER) then
  1335. list.concat(taicpu.op_none(A_FLDZ));
  1336. end;
  1337. {$endif x86}
  1338. end;
  1339. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1340. begin
  1341. case sym.initialloc.loc of
  1342. LOC_CREGISTER:
  1343. begin
  1344. {$ifndef cpu64bit}
  1345. if sym.initialloc.size in [OS_64,OS_S64] then
  1346. begin
  1347. sym.initialloc.register64.reglo:=cg.getintregister(list,OS_32);
  1348. sym.initialloc.register64.reghi:=cg.getintregister(list,OS_32);
  1349. end
  1350. else
  1351. {$endif cpu64bit}
  1352. sym.initialloc.register:=cg.getintregister(list,sym.initialloc.size);
  1353. end;
  1354. LOC_CFPUREGISTER:
  1355. begin
  1356. sym.initialloc.register:=cg.getfpuregister(list,sym.initialloc.size);
  1357. end;
  1358. LOC_CMMREGISTER:
  1359. begin
  1360. sym.initialloc.register:=cg.getmmregister(list,sym.initialloc.size);
  1361. end;
  1362. end;
  1363. if (pi_has_goto in current_procinfo.flags) then
  1364. begin
  1365. { Allocate register already, to prevent first allocation to be
  1366. inside a loop }
  1367. {$ifndef cpu64bit}
  1368. if sym.initialloc.size in [OS_64,OS_S64] then
  1369. begin
  1370. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  1371. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  1372. end
  1373. else
  1374. {$endif cpu64bit}
  1375. cg.a_reg_sync(list,sym.initialloc.register);
  1376. end;
  1377. sym.localloc:=sym.initialloc;
  1378. end;
  1379. procedure gen_load_para_value(list:TAsmList);
  1380. procedure get_para(const paraloc:TCGParaLocation);
  1381. begin
  1382. case paraloc.loc of
  1383. LOC_REGISTER :
  1384. begin
  1385. if getsupreg(paraloc.register)<first_int_imreg then
  1386. cg.getcpuregister(list,paraloc.register);
  1387. end;
  1388. LOC_MMREGISTER :
  1389. begin
  1390. if getsupreg(paraloc.register)<first_mm_imreg then
  1391. cg.getcpuregister(list,paraloc.register);
  1392. end;
  1393. LOC_FPUREGISTER :
  1394. begin
  1395. if getsupreg(paraloc.register)<first_fpu_imreg then
  1396. cg.getcpuregister(list,paraloc.register);
  1397. end;
  1398. end;
  1399. end;
  1400. procedure unget_para(const paraloc:TCGParaLocation);
  1401. begin
  1402. case paraloc.loc of
  1403. LOC_REGISTER :
  1404. begin
  1405. if getsupreg(paraloc.register)<first_int_imreg then
  1406. cg.ungetcpuregister(list,paraloc.register);
  1407. end;
  1408. LOC_MMREGISTER :
  1409. begin
  1410. if getsupreg(paraloc.register)<first_mm_imreg then
  1411. cg.ungetcpuregister(list,paraloc.register);
  1412. end;
  1413. LOC_FPUREGISTER :
  1414. begin
  1415. if getsupreg(paraloc.register)<first_fpu_imreg then
  1416. cg.ungetcpuregister(list,paraloc.register);
  1417. end;
  1418. end;
  1419. end;
  1420. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1421. var
  1422. href : treference;
  1423. begin
  1424. case paraloc.loc of
  1425. LOC_REGISTER :
  1426. begin
  1427. {$IFDEF POWERPC64}
  1428. if (paraloc.shiftval <> 0) then
  1429. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1430. {$ENDIF POWERPC64}
  1431. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1432. end;
  1433. LOC_MMREGISTER :
  1434. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1435. LOC_FPUREGISTER :
  1436. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1437. LOC_REFERENCE :
  1438. begin
  1439. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1440. { use concatcopy, because it can also be a float which fails when
  1441. load_ref_ref is used. Don't copy data when the references are equal }
  1442. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1443. cg.g_concatcopy(list,href,ref,sizeleft);
  1444. end;
  1445. else
  1446. internalerror(2002081302);
  1447. end;
  1448. end;
  1449. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1450. var
  1451. href : treference;
  1452. begin
  1453. case paraloc.loc of
  1454. LOC_REGISTER :
  1455. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1456. LOC_MMREGISTER :
  1457. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1458. LOC_FPUREGISTER :
  1459. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1460. LOC_REFERENCE :
  1461. begin
  1462. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1463. case getregtype(reg) of
  1464. R_INTREGISTER :
  1465. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1466. R_FPUREGISTER :
  1467. cg.a_loadfpu_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1468. R_MMREGISTER :
  1469. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1470. else
  1471. internalerror(2004101012);
  1472. end;
  1473. end;
  1474. else
  1475. internalerror(2002081302);
  1476. end;
  1477. end;
  1478. var
  1479. i : longint;
  1480. currpara : tparavarsym;
  1481. paraloc : pcgparalocation;
  1482. href : treference;
  1483. sizeleft : aint;
  1484. {$if defined(sparc) or defined(arm)}
  1485. tempref : treference;
  1486. {$endif sparc}
  1487. begin
  1488. if (po_assembler in current_procinfo.procdef.procoptions) then
  1489. exit;
  1490. { Allocate registers used by parameters }
  1491. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1492. begin
  1493. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1494. paraloc:=currpara.paraloc[calleeside].location;
  1495. while assigned(paraloc) do
  1496. begin
  1497. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1498. get_para(paraloc^);
  1499. paraloc:=paraloc^.next;
  1500. end;
  1501. end;
  1502. { Copy parameters to local references/registers }
  1503. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1504. begin
  1505. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1506. paraloc:=currpara.paraloc[calleeside].location;
  1507. { skip e.g. empty records }
  1508. if not assigned(paraloc) then
  1509. internalerror(200408203);
  1510. if (paraloc^.loc = LOC_VOID) then
  1511. continue;
  1512. case currpara.initialloc.loc of
  1513. LOC_REFERENCE :
  1514. begin
  1515. { If the parameter location is reused we don't need to copy
  1516. anything }
  1517. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1518. begin
  1519. href:=currpara.initialloc.reference;
  1520. sizeleft:=currpara.paraloc[calleeside].intsize;
  1521. while assigned(paraloc) do
  1522. begin
  1523. if (paraloc^.size=OS_NO) then
  1524. begin
  1525. { Can only be a reference that contains the rest
  1526. of the parameter }
  1527. if (paraloc^.loc<>LOC_REFERENCE) or
  1528. assigned(paraloc^.next) then
  1529. internalerror(2005013010);
  1530. gen_load_ref(paraloc^,href,sizeleft);
  1531. inc(href.offset,sizeleft);
  1532. sizeleft:=0;
  1533. end
  1534. else
  1535. begin
  1536. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1537. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1538. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1539. end;
  1540. unget_para(paraloc^);
  1541. paraloc:=paraloc^.next;
  1542. end;
  1543. end;
  1544. end;
  1545. LOC_CREGISTER :
  1546. begin
  1547. {$ifndef cpu64bit}
  1548. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1549. is_64bit(currpara.vardef) then
  1550. begin
  1551. case paraloc^.loc of
  1552. LOC_REGISTER:
  1553. begin
  1554. if not assigned(paraloc^.next) then
  1555. internalerror(200410104);
  1556. if (target_info.endian=ENDIAN_BIG) then
  1557. begin
  1558. { paraloc^ -> high
  1559. paraloc^.next -> low }
  1560. unget_para(paraloc^);
  1561. gen_alloc_regvar(list,currpara);
  1562. gen_load_reg(paraloc^,currpara.initialloc.register64.reghi);
  1563. unget_para(paraloc^.next^);
  1564. gen_load_reg(paraloc^.next^,currpara.initialloc.register64.reglo);
  1565. end
  1566. else
  1567. begin
  1568. { paraloc^ -> low
  1569. paraloc^.next -> high }
  1570. unget_para(paraloc^);
  1571. gen_alloc_regvar(list,currpara);
  1572. gen_load_reg(paraloc^,currpara.initialloc.register64.reglo);
  1573. unget_para(paraloc^.next^);
  1574. gen_load_reg(paraloc^.next^,currpara.initialloc.register64.reghi);
  1575. end;
  1576. end;
  1577. LOC_REFERENCE:
  1578. begin
  1579. gen_alloc_regvar(list,currpara);
  1580. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1581. cg64.a_load64_ref_reg(list,href,currpara.initialloc.register64);
  1582. unget_para(paraloc^);
  1583. end;
  1584. else
  1585. internalerror(2005101501);
  1586. end
  1587. end
  1588. else
  1589. {$endif cpu64bit}
  1590. begin
  1591. if assigned(paraloc^.next) then
  1592. internalerror(200410105);
  1593. unget_para(paraloc^);
  1594. gen_alloc_regvar(list,currpara);
  1595. gen_load_reg(paraloc^,currpara.initialloc.register);
  1596. end;
  1597. end;
  1598. LOC_CFPUREGISTER :
  1599. begin
  1600. {$if defined(sparc) or defined(arm)}
  1601. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1602. we need a temp }
  1603. sizeleft := TCGSize2Size[currpara.initialloc.size];
  1604. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1605. href:=tempref;
  1606. while assigned(paraloc) do
  1607. begin
  1608. unget_para(paraloc^);
  1609. gen_load_ref(paraloc^,href,sizeleft);
  1610. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1611. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1612. paraloc:=paraloc^.next;
  1613. end;
  1614. gen_alloc_regvar(list,currpara);
  1615. cg.a_loadfpu_ref_reg(list,currpara.initialloc.size,currpara.initialloc.size,tempref,currpara.initialloc.register);
  1616. tg.UnGetTemp(list,tempref);
  1617. {$else sparc}
  1618. unget_para(paraloc^);
  1619. gen_alloc_regvar(list,currpara);
  1620. gen_load_reg(paraloc^,currpara.initialloc.register);
  1621. if assigned(paraloc^.next) then
  1622. internalerror(200410109);
  1623. {$endif sparc}
  1624. end;
  1625. LOC_CMMREGISTER :
  1626. begin
  1627. unget_para(paraloc^);
  1628. gen_alloc_regvar(list,currpara);
  1629. gen_load_reg(paraloc^,currpara.initialloc.register);
  1630. { data could come in two memory locations, for now
  1631. we simply ignore the sanity check (FK)
  1632. if assigned(paraloc^.next) then
  1633. internalerror(200410108);
  1634. }
  1635. end;
  1636. end;
  1637. end;
  1638. { generate copies of call by value parameters, must be done before
  1639. the initialization and body is parsed because the refcounts are
  1640. incremented using the local copies }
  1641. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1642. {$ifdef powerpc}
  1643. { unget the register that contains the stack pointer before the procedure entry, }
  1644. { which is used to access the parameters in their original callee-side location }
  1645. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1646. cg.a_reg_dealloc(list,NR_R12);
  1647. {$endif powerpc}
  1648. {$ifdef powerpc64}
  1649. { unget the register that contains the stack pointer before the procedure entry, }
  1650. { which is used to access the parameters in their original callee-side location }
  1651. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1652. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1653. {$endif powerpc64}
  1654. end;
  1655. procedure gen_initialize_code(list:TAsmList);
  1656. begin
  1657. { initialize local data like ansistrings }
  1658. case current_procinfo.procdef.proctypeoption of
  1659. potype_unitinit:
  1660. begin
  1661. { this is also used for initialization of variables in a
  1662. program which does not have a globalsymtable }
  1663. if assigned(current_module.globalsymtable) then
  1664. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1665. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1666. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1667. end;
  1668. { units have seperate code for initilization and finalization }
  1669. potype_unitfinalize: ;
  1670. { program init/final is generated in separate procedure }
  1671. potype_proginit:
  1672. begin
  1673. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1674. end;
  1675. else
  1676. begin
  1677. if (localvartrashing <> -1) and
  1678. not(po_assembler in current_procinfo.procdef.procoptions) then
  1679. current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  1680. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1681. end;
  1682. end;
  1683. { initialisizes temp. ansi/wide string data }
  1684. inittempvariables(list);
  1685. { initialize ansi/widesstring para's }
  1686. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1687. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1688. {$ifdef OLDREGVARS}
  1689. load_regvars(list,nil);
  1690. {$endif OLDREGVARS}
  1691. end;
  1692. procedure gen_finalize_code(list:TAsmList);
  1693. begin
  1694. {$ifdef OLDREGVARS}
  1695. cleanup_regvars(list);
  1696. {$endif OLDREGVARS}
  1697. { finalize temporary data }
  1698. finalizetempvariables(list);
  1699. { finalize local data like ansistrings}
  1700. case current_procinfo.procdef.proctypeoption of
  1701. potype_unitfinalize:
  1702. begin
  1703. { this is also used for initialization of variables in a
  1704. program which does not have a globalsymtable }
  1705. if assigned(current_module.globalsymtable) then
  1706. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1707. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1708. end;
  1709. { units/progs have separate code for initialization and finalization }
  1710. potype_unitinit: ;
  1711. { program init/final is generated in separate procedure }
  1712. potype_proginit: ;
  1713. else
  1714. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  1715. end;
  1716. { finalize paras data }
  1717. if assigned(current_procinfo.procdef.parast) and
  1718. not(po_assembler in current_procinfo.procdef.procoptions) then
  1719. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  1720. end;
  1721. procedure gen_entry_code(list:TAsmList);
  1722. var
  1723. paraloc1,
  1724. paraloc2 : tcgpara;
  1725. begin
  1726. paraloc1.init;
  1727. paraloc2.init;
  1728. { the actual profile code can clobber some registers,
  1729. therefore if the context must be saved, do it before
  1730. the actual call to the profile code
  1731. }
  1732. if (cs_profile in current_settings.moduleswitches) and
  1733. not(po_assembler in current_procinfo.procdef.procoptions) then
  1734. begin
  1735. { non-win32 can call mcout even in main }
  1736. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1737. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1738. begin
  1739. cg.g_profilecode(list);
  1740. end;
  1741. end;
  1742. { call startup helpers from main program }
  1743. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1744. begin
  1745. { initialize units }
  1746. cg.allocallcpuregisters(list);
  1747. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1748. cg.deallocallcpuregisters(list);
  1749. end;
  1750. list.concat(Tai_force_line.Create);
  1751. {$ifdef OLDREGVARS}
  1752. load_regvars(list,nil);
  1753. {$endif OLDREGVARS}
  1754. paraloc1.done;
  1755. paraloc2.done;
  1756. end;
  1757. procedure gen_exit_code(list:TAsmList);
  1758. begin
  1759. { call __EXIT for main program }
  1760. if (not DLLsource) and
  1761. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1762. cg.a_call_name(list,'FPC_DO_EXIT');
  1763. end;
  1764. {****************************************************************************
  1765. Entry/Exit
  1766. ****************************************************************************}
  1767. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1768. var
  1769. item : TCmdStrListItem;
  1770. begin
  1771. result:=true;
  1772. if pd.mangledname=s then
  1773. exit;
  1774. item := TCmdStrListItem(pd.aliasnames.first);
  1775. while assigned(item) do
  1776. begin
  1777. if item.str=s then
  1778. exit;
  1779. item := TCmdStrListItem(item.next);
  1780. end;
  1781. result:=false;
  1782. end;
  1783. procedure alloc_proc_symbol(pd: tprocdef);
  1784. var
  1785. item : TCmdStrListItem;
  1786. begin
  1787. item := TCmdStrListItem(pd.aliasnames.first);
  1788. while assigned(item) do
  1789. begin
  1790. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1791. item := TCmdStrListItem(item.next);
  1792. end;
  1793. end;
  1794. procedure gen_proc_symbol(list:TAsmList);
  1795. var
  1796. item : TCmdStrListItem;
  1797. begin
  1798. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1799. while assigned(item) do
  1800. begin
  1801. if (cs_profile in current_settings.moduleswitches) or
  1802. (po_global in current_procinfo.procdef.procoptions) then
  1803. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1804. else
  1805. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1806. if tf_use_function_relative_addresses in target_info.flags then
  1807. list.concat(Tai_function_name.create(item.str));
  1808. item := TCmdStrListItem(item.next);
  1809. end;
  1810. current_procinfo.procdef.procstarttai:=tai(list.last);
  1811. end;
  1812. procedure gen_proc_symbol_end(list:TAsmList);
  1813. begin
  1814. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1815. current_procinfo.procdef.procendtai:=tai(list.last);
  1816. { finalisation marker for Mac OS X }
  1817. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) and
  1818. (current_module.islibrary) and
  1819. (((current_module.flags and uf_finalize)<>0) or
  1820. (current_procinfo.procdef.proctypeoption = potype_proginit)) then
  1821. begin
  1822. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1823. list.concat(tai_directive.create(asd_mod_init_func,''))
  1824. else
  1825. list.concat(tai_directive.create(asd_mod_term_func,''));
  1826. list.concat(tai_align.create(4));
  1827. list.concat(Tai_const.Createname(current_procinfo.procdef.mangledname,0));
  1828. end;
  1829. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1830. begin
  1831. if (target_info.system in [system_powerpc_darwin,system_i386_darwin,system_powerpc_macos]) and
  1832. not(current_module.islibrary) then
  1833. begin
  1834. list.concat(tai_section.create(sec_code,'',4));
  1835. list.concat(tai_symbol.createname_global(
  1836. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1837. { keep argc, argv and envp properly on the stack }
  1838. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1839. end;
  1840. end;
  1841. end;
  1842. procedure gen_proc_entry_code(list:TAsmList);
  1843. var
  1844. hitemp,
  1845. lotemp : longint;
  1846. begin
  1847. { generate call frame marker for dwarf call frame info }
  1848. current_asmdata.asmcfi.start_frame(list);
  1849. { All temps are know, write offsets used for information }
  1850. if (cs_asm_source in current_settings.globalswitches) then
  1851. begin
  1852. if tg.direction>0 then
  1853. begin
  1854. lotemp:=current_procinfo.tempstart;
  1855. hitemp:=tg.lasttemp;
  1856. end
  1857. else
  1858. begin
  1859. lotemp:=tg.lasttemp;
  1860. hitemp:=current_procinfo.tempstart;
  1861. end;
  1862. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1863. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1864. end;
  1865. { generate target specific proc entry code }
  1866. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1867. end;
  1868. procedure gen_proc_exit_code(list:TAsmList);
  1869. var
  1870. parasize : longint;
  1871. begin
  1872. { c style clearstack does not need to remove parameters from the stack, only the
  1873. return value when it was pushed by arguments }
  1874. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1875. begin
  1876. parasize:=0;
  1877. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1878. inc(parasize,sizeof(aint));
  1879. end
  1880. else
  1881. parasize:=current_procinfo.para_stack_size;
  1882. { generate target specific proc exit code }
  1883. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1884. { release return registers, needed for optimizer }
  1885. if not is_void(current_procinfo.procdef.returndef) then
  1886. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1887. { end of frame marker for call frame info }
  1888. current_asmdata.asmcfi.end_frame(list);
  1889. end;
  1890. procedure gen_stack_check_size_para(list:TAsmList);
  1891. var
  1892. paraloc1 : tcgpara;
  1893. begin
  1894. paraloc1.init;
  1895. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1896. paramanager.allocparaloc(list,paraloc1);
  1897. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1898. paramanager.freeparaloc(list,paraloc1);
  1899. paraloc1.done;
  1900. end;
  1901. procedure gen_stack_check_call(list:TAsmList);
  1902. var
  1903. paraloc1 : tcgpara;
  1904. begin
  1905. paraloc1.init;
  1906. { Also alloc the register needed for the parameter }
  1907. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1908. paramanager.allocparaloc(list,paraloc1);
  1909. paramanager.freeparaloc(list,paraloc1);
  1910. { Call the helper }
  1911. cg.allocallcpuregisters(list);
  1912. cg.a_call_name(list,'FPC_STACKCHECK');
  1913. cg.deallocallcpuregisters(list);
  1914. paraloc1.done;
  1915. end;
  1916. procedure gen_save_used_regs(list:TAsmList);
  1917. begin
  1918. { Pure assembler routines need to save the registers themselves }
  1919. if (po_assembler in current_procinfo.procdef.procoptions) then
  1920. exit;
  1921. { oldfpccall expects all registers to be destroyed }
  1922. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1923. cg.g_save_standard_registers(list);
  1924. end;
  1925. procedure gen_restore_used_regs(list:TAsmList);
  1926. begin
  1927. { Pure assembler routines need to save the registers themselves }
  1928. if (po_assembler in current_procinfo.procdef.procoptions) then
  1929. exit;
  1930. { oldfpccall expects all registers to be destroyed }
  1931. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1932. cg.g_restore_standard_registers(list);
  1933. end;
  1934. procedure gen_got_load(list : TAsmList);
  1935. begin
  1936. { if loading got is necessary for more cpus, it can be moved
  1937. to the cg }
  1938. {$ifdef i386}
  1939. { allocate PIC register }
  1940. if (cs_create_pic in current_settings.moduleswitches) and
  1941. (tf_pic_uses_got in target_info.flags) and
  1942. (pi_needs_got in current_procinfo.flags) and
  1943. not(po_kylixlocal in current_procinfo.procdef.procoptions) then
  1944. begin
  1945. current_module.requires_ebx_pic_helper:=true;
  1946. cg.a_call_name_static(list,'fpc_geteipasebx');
  1947. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),0,NR_PIC_OFFSET_REG));
  1948. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1949. { ecx could be used in leave procedures }
  1950. current_procinfo.got:=NR_EBX;
  1951. end;
  1952. {$endif i386}
  1953. end;
  1954. {****************************************************************************
  1955. External handling
  1956. ****************************************************************************}
  1957. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1958. var
  1959. ref : treference;
  1960. sym : tasmsymbol;
  1961. begin
  1962. { add the procedure to the al_procedures }
  1963. maybe_new_object_file(list);
  1964. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1965. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1966. if (po_global in pd.procoptions) then
  1967. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1968. else
  1969. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1970. {$ifdef x86}
  1971. { fix this for other CPUs as well }
  1972. sym:=current_asmdata.RefAsmSymbol(externalname);
  1973. reference_reset_symbol(ref,sym,0);
  1974. { create pic'ed? }
  1975. if cs_create_pic in current_settings.moduleswitches then
  1976. begin
  1977. { it could be that we're called from a procedure not having the
  1978. got loaded
  1979. }
  1980. gen_got_load(list);
  1981. ref.refaddr:=addr_pic;
  1982. end
  1983. else
  1984. ref.refaddr:=addr_full;
  1985. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1986. {$else x86}
  1987. cg.a_jmp_name(list,externalname);
  1988. {$endif x86}
  1989. end;
  1990. {****************************************************************************
  1991. Const Data
  1992. ****************************************************************************}
  1993. procedure insertbssdata(sym : tstaticvarsym);
  1994. var
  1995. l : aint;
  1996. varalign : shortint;
  1997. storefilepos : tfileposinfo;
  1998. list : TAsmList;
  1999. sectype : TAsmSectiontype;
  2000. begin
  2001. storefilepos:=current_filepos;
  2002. current_filepos:=sym.fileinfo;
  2003. l:=sym.getsize;
  2004. if tf_section_threadvars in target_info.flags then
  2005. begin
  2006. if (vo_is_thread_var in sym.varoptions) then
  2007. begin
  2008. list:=current_asmdata.asmlists[al_threadvars];
  2009. sectype:=sec_threadvar;
  2010. end
  2011. else
  2012. begin
  2013. list:=current_asmdata.asmlists[al_globals];
  2014. sectype:=sec_bss;
  2015. end;
  2016. end
  2017. else
  2018. begin
  2019. if (vo_is_thread_var in sym.varoptions) then
  2020. inc(l,sizeof(aint));
  2021. list:=current_asmdata.asmlists[al_globals];
  2022. sectype:=sec_bss;
  2023. end;
  2024. varalign:=var_align(size_2_align(l));
  2025. maybe_new_object_file(list);
  2026. new_section(list,sectype,lower(sym.mangledname),varalign);
  2027. if (sym.owner.symtabletype=globalsymtable) or
  2028. maybe_smartlink_symbol or
  2029. DLLSource or
  2030. (assigned(current_procinfo) and
  2031. (po_inline in current_procinfo.procdef.procoptions)) or
  2032. (vo_is_public in sym.varoptions) then
  2033. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  2034. else
  2035. list.concat(Tai_datablock.create(sym.mangledname,l));
  2036. current_filepos:=storefilepos;
  2037. end;
  2038. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  2039. procedure setlocalloc(vs:tabstractnormalvarsym);
  2040. begin
  2041. if cs_asm_source in current_settings.globalswitches then
  2042. begin
  2043. case vs.initialloc.loc of
  2044. LOC_REFERENCE :
  2045. begin
  2046. if not assigned(vs.initialloc.reference.symbol) then
  2047. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  2048. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  2049. end;
  2050. end;
  2051. end;
  2052. vs.localloc:=vs.initialloc;
  2053. end;
  2054. var
  2055. i : longint;
  2056. sym : tsym;
  2057. vs : tabstractnormalvarsym;
  2058. isaddr : boolean;
  2059. begin
  2060. for i:=0 to st.SymList.Count-1 do
  2061. begin
  2062. sym:=tsym(st.SymList[i]);
  2063. case sym.typ of
  2064. staticvarsym :
  2065. begin
  2066. vs:=tabstractnormalvarsym(sym);
  2067. { The code in laodnode.pass_generatecode will create the
  2068. LOC_REFERENCE instead for all none register variables. This is
  2069. required because we can't store an asmsymbol in the localloc because
  2070. the asmsymbol is invalid after an unit is compiled. This gives
  2071. problems when this procedure is inlined in an other unit (PFV) }
  2072. if vs.is_regvar(false) then
  2073. begin
  2074. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2075. vs.initialloc.size:=def_cgsize(vs.vardef);
  2076. gen_alloc_regvar(list,vs);
  2077. setlocalloc(vs);
  2078. end;
  2079. end;
  2080. paravarsym :
  2081. begin
  2082. vs:=tabstractnormalvarsym(sym);
  2083. { Parameters passed to assembler procedures need to be kept
  2084. in the original location }
  2085. if (po_assembler in current_procinfo.procdef.procoptions) then
  2086. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  2087. else
  2088. begin
  2089. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  2090. if isaddr then
  2091. vs.initialloc.size:=OS_ADDR
  2092. else
  2093. vs.initialloc.size:=def_cgsize(vs.vardef);
  2094. if vs.is_regvar(isaddr) then
  2095. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  2096. else
  2097. begin
  2098. vs.initialloc.loc:=LOC_REFERENCE;
  2099. { Reuse the parameter location for values to are at a single location on the stack }
  2100. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  2101. begin
  2102. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  2103. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  2104. end
  2105. else
  2106. begin
  2107. if isaddr then
  2108. tg.GetLocal(list,sizeof(aint),voidpointertype,vs.initialloc.reference)
  2109. else
  2110. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  2111. end;
  2112. end;
  2113. end;
  2114. setlocalloc(vs);
  2115. end;
  2116. localvarsym :
  2117. begin
  2118. vs:=tabstractnormalvarsym(sym);
  2119. vs.initialloc.size:=def_cgsize(vs.vardef);
  2120. if (m_delphi in current_settings.modeswitches) and
  2121. (po_assembler in current_procinfo.procdef.procoptions) and
  2122. (vo_is_funcret in vs.varoptions) and
  2123. (vs.refs=0) then
  2124. begin
  2125. { not referenced, so don't allocate. Use dummy to }
  2126. { avoid ie's later on because of LOC_INVALID }
  2127. vs.initialloc.loc:=LOC_REGISTER;
  2128. vs.initialloc.size:=OS_INT;
  2129. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  2130. end
  2131. else if vs.is_regvar(false) then
  2132. begin
  2133. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2134. gen_alloc_regvar(list,vs);
  2135. end
  2136. else
  2137. begin
  2138. vs.initialloc.loc:=LOC_REFERENCE;
  2139. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  2140. end;
  2141. setlocalloc(vs);
  2142. end;
  2143. end;
  2144. end;
  2145. end;
  2146. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  2147. begin
  2148. case location.loc of
  2149. LOC_CREGISTER:
  2150. {$ifndef cpu64bit}
  2151. if location.size in [OS_64,OS_S64] then
  2152. begin
  2153. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  2154. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  2155. end
  2156. else
  2157. {$endif cpu64bit}
  2158. rv.intregvars.addnodup(getsupreg(location.register));
  2159. LOC_CFPUREGISTER:
  2160. rv.fpuregvars.addnodup(getsupreg(location.register));
  2161. LOC_CMMREGISTER:
  2162. rv.mmregvars.addnodup(getsupreg(location.register));
  2163. end;
  2164. end;
  2165. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  2166. var
  2167. rv: pusedregvars absolute arg;
  2168. begin
  2169. case (n.nodetype) of
  2170. temprefn:
  2171. { We only have to synchronise a tempnode before a loop if it is }
  2172. { not created inside the loop, and only synchronise after the }
  2173. { loop if it's not destroyed inside the loop. If it's created }
  2174. { before the loop and not yet destroyed, then before the loop }
  2175. { is secondpassed tempinfo^.valid will be true, and we get the }
  2176. { correct registers. If it's not destroyed inside the loop, }
  2177. { then after the loop has been secondpassed tempinfo^.valid }
  2178. { be true and we also get the right registers. In other cases, }
  2179. { tempinfo^.valid will be false and so we do not add }
  2180. { unnecessary registers. This way, we don't have to look at }
  2181. { tempcreate and tempdestroy nodes to get this info (JM) }
  2182. if (ttemprefnode(n).tempinfo^.valid) then
  2183. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  2184. loadn:
  2185. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2186. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  2187. vecn:
  2188. { range checks sometimes need the high parameter }
  2189. if (cs_check_range in current_settings.localswitches) and
  2190. (is_open_array(tvecnode(n).left.resultdef) or
  2191. is_array_of_const(tvecnode(n).left.resultdef)) and
  2192. not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2193. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  2194. end;
  2195. result := fen_true;
  2196. end;
  2197. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  2198. begin
  2199. foreachnodestatic(n,@do_get_used_regvars,@rv);
  2200. end;
  2201. (*
  2202. See comments at declaration of pusedregvarscommon
  2203. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  2204. var
  2205. rv: pusedregvarscommon absolute arg;
  2206. begin
  2207. if (n.nodetype = loadn) and
  2208. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2209. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2210. case loc of
  2211. LOC_CREGISTER:
  2212. { if not yet encountered in this node tree }
  2213. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2214. { but nevertheless already encountered somewhere }
  2215. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2216. { then it's a regvar used in two or more node trees }
  2217. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2218. LOC_CFPUREGISTER:
  2219. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2220. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2221. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2222. LOC_CMMREGISTER:
  2223. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2224. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2225. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2226. end;
  2227. result := fen_true;
  2228. end;
  2229. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2230. begin
  2231. rv.myregvars.intregvars.clear;
  2232. rv.myregvars.fpuregvars.clear;
  2233. rv.myregvars.mmregvars.clear;
  2234. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2235. end;
  2236. *)
  2237. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2238. var
  2239. count: longint;
  2240. begin
  2241. for count := 1 to rv.intregvars.length do
  2242. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2243. for count := 1 to rv.fpuregvars.length do
  2244. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2245. for count := 1 to rv.mmregvars.length do
  2246. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2247. end;
  2248. {*****************************************************************************
  2249. SSA support
  2250. *****************************************************************************}
  2251. type
  2252. preplaceregrec = ^treplaceregrec;
  2253. treplaceregrec = record
  2254. old, new: tregister;
  2255. {$ifndef cpu64bit}
  2256. oldhi, newhi: tregister;
  2257. {$endif cpu64bit}
  2258. ressym: tsym;
  2259. end;
  2260. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  2261. var
  2262. rr: preplaceregrec absolute para;
  2263. begin
  2264. result := fen_false;
  2265. case n.nodetype of
  2266. loadn:
  2267. begin
  2268. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  2269. not assigned(tloadnode(n).left) and
  2270. (((tloadnode(n).symtableentry <> rr^.ressym) and
  2271. not(vo_is_funcret in tabstractvarsym(tloadnode(n).symtableentry).varoptions)) or
  2272. not(fc_exit in flowcontrol)) and
  2273. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2274. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  2275. begin
  2276. {$ifndef cpu64bit}
  2277. { it's possible a 64 bit location was shifted and/xor typecasted }
  2278. { in a 32 bit value, so only 1 register was left in the location }
  2279. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  2280. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  2281. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  2282. else
  2283. exit;
  2284. {$endif cpu64bit}
  2285. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  2286. result := fen_norecurse_true;
  2287. end;
  2288. end;
  2289. temprefn:
  2290. begin
  2291. if (ttemprefnode(n).tempinfo^.valid) and
  2292. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2293. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  2294. begin
  2295. {$ifndef cpu64bit}
  2296. { it's possible a 64 bit location was shifted and/xor typecasted }
  2297. { in a 32 bit value, so only 1 register was left in the location }
  2298. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  2299. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  2300. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  2301. else
  2302. exit;
  2303. {$endif cpu64bit}
  2304. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  2305. result := fen_norecurse_true;
  2306. end;
  2307. end;
  2308. { optimize the searching a bit }
  2309. derefn,addrn,
  2310. calln,inlinen,casen,
  2311. addn,subn,muln,
  2312. andn,orn,xorn,
  2313. ltn,lten,gtn,gten,equaln,unequaln,
  2314. slashn,divn,shrn,shln,notn,
  2315. inn,
  2316. asn,isn:
  2317. result := fen_norecurse_false;
  2318. end;
  2319. end;
  2320. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  2321. var
  2322. rr: treplaceregrec;
  2323. begin
  2324. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  2325. ([fc_inflowcontrol,fc_gotolabel] * flowcontrol <> []) then
  2326. exit;
  2327. rr.old := n.location.register;
  2328. rr.ressym := nil;
  2329. {$ifndef cpu64bit}
  2330. rr.oldhi := NR_NO;
  2331. {$endif cpu64bit}
  2332. case n.location.loc of
  2333. LOC_CREGISTER:
  2334. begin
  2335. {$ifndef cpu64bit}
  2336. if (n.location.size in [OS_64,OS_S64]) then
  2337. begin
  2338. rr.oldhi := n.location.register64.reghi;
  2339. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2340. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2341. end
  2342. else
  2343. {$endif cpu64bit}
  2344. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  2345. end;
  2346. LOC_CFPUREGISTER:
  2347. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  2348. {$ifdef SUPPORT_MMX}
  2349. LOC_CMMXREGISTER:
  2350. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  2351. {$endif SUPPORT_MMX}
  2352. LOC_CMMREGISTER:
  2353. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  2354. else
  2355. exit;
  2356. end;
  2357. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  2358. assigned(current_procinfo.procdef.funcretsym) and
  2359. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  2360. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2361. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  2362. else
  2363. rr.ressym:=current_procinfo.procdef.funcretsym;
  2364. if not foreachnodestatic(n,@doreplace,@rr) then
  2365. exit;
  2366. if reload then
  2367. case n.location.loc of
  2368. LOC_CREGISTER:
  2369. begin
  2370. {$ifndef cpu64bit}
  2371. if (n.location.size in [OS_64,OS_S64]) then
  2372. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  2373. else
  2374. {$endif cpu64bit}
  2375. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2376. end;
  2377. LOC_CFPUREGISTER:
  2378. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2379. {$ifdef SUPPORT_MMX}
  2380. LOC_CMMXREGISTER:
  2381. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  2382. {$endif SUPPORT_MMX}
  2383. LOC_CMMREGISTER:
  2384. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  2385. else
  2386. internalerror(2006090920);
  2387. end;
  2388. { now that we've change the loadn/temp, also change the node result location }
  2389. {$ifndef cpu64bit}
  2390. if (n.location.size in [OS_64,OS_S64]) then
  2391. begin
  2392. n.location.register64.reglo := rr.new;
  2393. n.location.register64.reghi := rr.newhi;
  2394. end
  2395. else
  2396. {$endif cpu64bit}
  2397. n.location.register := rr.new;
  2398. end;
  2399. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  2400. var
  2401. i : longint;
  2402. sym : tsym;
  2403. begin
  2404. for i:=0 to st.SymList.Count-1 do
  2405. begin
  2406. sym:=tsym(st.SymList[i]);
  2407. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  2408. begin
  2409. with tabstractnormalvarsym(sym) do
  2410. begin
  2411. { Note: We need to keep the data available in memory
  2412. for the sub procedures that can access local data
  2413. in the parent procedures }
  2414. case localloc.loc of
  2415. LOC_CREGISTER :
  2416. {$ifndef cpu64bit}
  2417. if (pi_has_goto in current_procinfo.flags) then
  2418. if def_cgsize(vardef) in [OS_64,OS_S64] then
  2419. begin
  2420. cg.a_reg_sync(list,localloc.register64.reglo);
  2421. cg.a_reg_sync(list,localloc.register64.reghi);
  2422. end
  2423. else
  2424. {$endif cpu64bit}
  2425. cg.a_reg_sync(list,localloc.register);
  2426. LOC_CFPUREGISTER,
  2427. LOC_CMMREGISTER:
  2428. if (pi_has_goto in current_procinfo.flags) then
  2429. cg.a_reg_sync(list,localloc.register);
  2430. LOC_REFERENCE :
  2431. begin
  2432. if typ in [localvarsym,paravarsym] then
  2433. tg.Ungetlocal(list,localloc.reference);
  2434. end;
  2435. end;
  2436. end;
  2437. end;
  2438. end;
  2439. end;
  2440. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2441. var
  2442. i,j : longint;
  2443. tmps : string;
  2444. pd : TProcdef;
  2445. ImplIntf : TImplementedInterface;
  2446. begin
  2447. for i:=0 to _class.ImplementedInterfaces.count-1 do
  2448. begin
  2449. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  2450. if (ImplIntf=ImplIntf.VtblImplIntf) and
  2451. assigned(ImplIntf.ProcDefs) then
  2452. begin
  2453. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  2454. begin
  2455. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  2456. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2457. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  2458. { create wrapper code }
  2459. new_section(list,sec_code,tmps,0);
  2460. cg.init_register_allocators;
  2461. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  2462. cg.done_register_allocators;
  2463. end;
  2464. end;
  2465. end;
  2466. end;
  2467. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  2468. var
  2469. i : longint;
  2470. def : tdef;
  2471. begin
  2472. for i:=0 to st.DefList.Count-1 do
  2473. begin
  2474. def:=tdef(st.DefList[i]);
  2475. if is_class(def) then
  2476. gen_intf_wrapper(list,tobjectdef(def));
  2477. end;
  2478. end;
  2479. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2480. var
  2481. href : treference;
  2482. begin
  2483. if is_object(objdef) then
  2484. begin
  2485. case selfloc.loc of
  2486. LOC_CREFERENCE,
  2487. LOC_REFERENCE:
  2488. begin
  2489. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2490. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2491. end;
  2492. else
  2493. internalerror(200305056);
  2494. end;
  2495. end
  2496. else
  2497. begin
  2498. case selfloc.loc of
  2499. LOC_REGISTER:
  2500. begin
  2501. {$ifdef cpu_uses_separate_address_registers}
  2502. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2503. begin
  2504. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2505. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2506. end
  2507. else
  2508. {$endif cpu_uses_separate_address_registers}
  2509. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2510. end;
  2511. LOC_CREGISTER,
  2512. LOC_CREFERENCE,
  2513. LOC_REFERENCE:
  2514. begin
  2515. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2516. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2517. end;
  2518. else
  2519. internalerror(200305057);
  2520. end;
  2521. end;
  2522. vmtreg:=cg.getaddressregister(list);
  2523. cg.g_maybe_testself(list,href.base);
  2524. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2525. { test validity of VMT }
  2526. if not(is_interface(objdef)) and
  2527. not(is_cppclass(objdef)) then
  2528. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2529. end;
  2530. function getprocalign : shortint;
  2531. begin
  2532. { gprof uses 16 byte granularity }
  2533. if (cs_profile in current_settings.moduleswitches) then
  2534. result:=16
  2535. else
  2536. result:=current_settings.alignment.procalign;
  2537. end;
  2538. procedure gen_pic_helpers(list : TAsmList);
  2539. var
  2540. href : treference;
  2541. begin
  2542. { if other cpus require such helpers as well, it can be solved more cleaner }
  2543. {$ifdef i386}
  2544. if current_module.requires_ebx_pic_helper then
  2545. begin
  2546. new_section(list,sec_code,'fpc_geteipasebx',0);
  2547. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2548. reference_reset(href);
  2549. href.base:=NR_ESP;
  2550. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2551. list.concat(taicpu.op_none(A_RET,S_NO));
  2552. end;
  2553. if current_module.requires_ecx_pic_helper then
  2554. begin
  2555. new_section(list,sec_code,'fpc_geteipasecx',0);
  2556. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2557. reference_reset(href);
  2558. href.base:=NR_ESP;
  2559. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2560. list.concat(taicpu.op_none(A_RET,S_NO));
  2561. end;
  2562. {$endif i386}
  2563. end;
  2564. end.