ncgutil.pas 113 KB

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