2
0

ncgutil.pas 109 KB

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