2
0

ncgutil.pas 117 KB

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