ncgutil.pas 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  50. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  52. procedure location_allocate_register(list:TAsmList;out l: tlocation;def: tdef;constant: boolean);
  53. { loads a cgpara into a tlocation; assumes that loc.loc is already
  54. initialised }
  55. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  56. { allocate registers for a tlocation; assumes that loc.loc is already
  57. set to LOC_CREGISTER/LOC_CFPUREGISTER/... }
  58. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  59. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  60. function has_alias_name(pd:tprocdef;const s:string):boolean;
  61. procedure alloc_proc_symbol(pd: tprocdef);
  62. procedure gen_proc_symbol(list:TAsmList);
  63. procedure gen_proc_symbol_end(list:TAsmList);
  64. procedure gen_proc_entry_code(list:TAsmList);
  65. procedure gen_proc_exit_code(list:TAsmList);
  66. procedure gen_stack_check_size_para(list:TAsmList);
  67. procedure gen_stack_check_call(list:TAsmList);
  68. procedure gen_save_used_regs(list:TAsmList);
  69. procedure gen_restore_used_regs(list:TAsmList);
  70. procedure gen_load_para_value(list:TAsmList);
  71. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  72. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  73. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  74. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  75. { adds the regvars used in n and its children to rv.allregvars,
  76. those which were already in rv.allregvars to rv.commonregvars and
  77. uses rv.myregvars as scratch (so that two uses of the same regvar
  78. in a single tree to make it appear in commonregvars). Useful to
  79. find out which regvars are used in two different node trees
  80. (e.g. in the "else" and "then" path, or in various case blocks }
  81. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  82. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  83. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  84. { loadn and change its location to a new register (= SSA). In case reload }
  85. { is true, transfer the old to the new register }
  86. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  87. {#
  88. Allocate the buffers for exception management and setjmp environment.
  89. Return a pointer to these buffers, send them to the utility routine
  90. so they are registered, and then call setjmp.
  91. Then compare the result of setjmp with 0, and if not equal
  92. to zero, then jump to exceptlabel.
  93. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  94. It is to note that this routine may be called *after* the stackframe of a
  95. routine has been called, therefore on machines where the stack cannot
  96. be modified, all temps should be allocated on the heap instead of the
  97. stack.
  98. }
  99. const
  100. EXCEPT_BUF_SIZE = 3*sizeof(pint);
  101. type
  102. texceptiontemps=record
  103. jmpbuf,
  104. envbuf,
  105. reasonbuf : treference;
  106. end;
  107. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  108. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  109. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  110. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  111. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  112. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  113. procedure location_free(list: TAsmList; const location : TLocation);
  114. function getprocalign : shortint;
  115. procedure gen_fpc_dummy(list : TAsmList);
  116. procedure InsertInterruptTable;
  117. implementation
  118. uses
  119. version,
  120. cutils,cclasses,
  121. globals,systems,verbose,export,
  122. ppu,defutil,
  123. procinfo,paramgr,fmodule,
  124. regvars,dbgbase,
  125. pass_1,pass_2,
  126. nbas,ncon,nld,nmem,nutils,ngenutil,
  127. tgobj,cgobj,cgcpu,hlcgobj,hlcgcpu
  128. {$ifdef powerpc}
  129. , cpupi
  130. {$endif}
  131. {$ifdef powerpc64}
  132. , cpupi
  133. {$endif}
  134. {$ifdef SUPPORT_MMX}
  135. , cgx86
  136. {$endif SUPPORT_MMX}
  137. ;
  138. {*****************************************************************************
  139. Misc Helpers
  140. *****************************************************************************}
  141. {$if first_mm_imreg = 0}
  142. {$WARN 4044 OFF} { Comparison might be always false ... }
  143. {$endif}
  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 cpu64bitalu}
  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 cpu64bitalu}
  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 paramanager.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. hlcg.a_load_loc_reg(list,p.resultdef,osuinttype,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. {$ifndef cpu64bitalu}
  278. if opsize in [OS_64,OS_S64] then
  279. begin
  280. hlcg.location_force_reg(list,p.location,p.resultdef,hlcg.tcgsize2orddef(opsize),true);
  281. tmpreg:=cg.getintregister(list,OS_32);
  282. cg.a_op_reg_reg_reg(list,OP_OR,OS_32,p.location.register64.reglo,p.location.register64.reghi,tmpreg);
  283. location_reset(p.location,LOC_REGISTER,OS_32);
  284. p.location.register:=tmpreg;
  285. opsize:=OS_32;
  286. end;
  287. {$endif not cpu64bitalu}
  288. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  289. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  290. end;
  291. LOC_JUMP:
  292. ;
  293. {$ifdef cpuflags}
  294. LOC_FLAGS :
  295. begin
  296. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  297. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  298. end;
  299. {$endif cpuflags}
  300. else
  301. begin
  302. printnode(output,p);
  303. internalerror(200308241);
  304. end;
  305. end;
  306. end;
  307. end
  308. else
  309. internalerror(200112305);
  310. current_filepos:=storepos;
  311. end;
  312. (*
  313. This code needs fixing. It is not safe to use rgint; on the m68000 it
  314. would be rgaddr.
  315. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  316. begin
  317. case t.loc of
  318. LOC_REGISTER:
  319. begin
  320. { can't be a regvar, since it would be LOC_CREGISTER then }
  321. exclude(regs,getsupreg(t.register));
  322. if t.register64.reghi<>NR_NO then
  323. exclude(regs,getsupreg(t.register64.reghi));
  324. end;
  325. LOC_CREFERENCE,LOC_REFERENCE:
  326. begin
  327. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  328. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  329. exclude(regs,getsupreg(t.reference.base));
  330. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  331. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  332. exclude(regs,getsupreg(t.reference.index));
  333. end;
  334. end;
  335. end;
  336. *)
  337. {*****************************************************************************
  338. EXCEPTION MANAGEMENT
  339. *****************************************************************************}
  340. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  341. var
  342. srsym : ttypesym;
  343. begin
  344. if jmp_buf_size=-1 then
  345. begin
  346. srsym:=search_system_type('JMP_BUF');
  347. jmp_buf_size:=srsym.typedef.size;
  348. jmp_buf_align:=srsym.typedef.alignment;
  349. end;
  350. tg.GetTemp(list,EXCEPT_BUF_SIZE,sizeof(pint),tt_persistent,t.envbuf);
  351. tg.GetTemp(list,jmp_buf_size,jmp_buf_align,tt_persistent,t.jmpbuf);
  352. tg.GetTemp(list,sizeof(pint),sizeof(pint),tt_persistent,t.reasonbuf);
  353. end;
  354. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  355. begin
  356. tg.Ungettemp(list,t.jmpbuf);
  357. tg.ungettemp(list,t.envbuf);
  358. tg.ungettemp(list,t.reasonbuf);
  359. end;
  360. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  361. var
  362. paraloc1,paraloc2,paraloc3 : tcgpara;
  363. begin
  364. paraloc1.init;
  365. paraloc2.init;
  366. paraloc3.init;
  367. paramanager.getintparaloc(pocall_default,1,paraloc1);
  368. paramanager.getintparaloc(pocall_default,2,paraloc2);
  369. paramanager.getintparaloc(pocall_default,3,paraloc3);
  370. cg.a_loadaddr_ref_cgpara(list,t.envbuf,paraloc3);
  371. cg.a_loadaddr_ref_cgpara(list,t.jmpbuf,paraloc2);
  372. { push type of exceptionframe }
  373. cg.a_load_const_cgpara(list,OS_S32,1,paraloc1);
  374. paramanager.freecgpara(list,paraloc3);
  375. paramanager.freecgpara(list,paraloc2);
  376. paramanager.freecgpara(list,paraloc1);
  377. cg.allocallcpuregisters(list);
  378. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR',false);
  379. cg.deallocallcpuregisters(list);
  380. paramanager.getintparaloc(pocall_default,1,paraloc1);
  381. cg.a_load_reg_cgpara(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  382. paramanager.freecgpara(list,paraloc1);
  383. cg.allocallcpuregisters(list);
  384. cg.a_call_name(list,'FPC_SETJMP',false);
  385. cg.deallocallcpuregisters(list);
  386. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  387. cg.g_exception_reason_save(list, t.reasonbuf);
  388. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  389. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  390. paraloc1.done;
  391. paraloc2.done;
  392. paraloc3.done;
  393. end;
  394. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  395. begin
  396. cg.allocallcpuregisters(list);
  397. cg.a_call_name(list,'FPC_POPADDRSTACK',false);
  398. cg.deallocallcpuregisters(list);
  399. if not onlyfree then
  400. begin
  401. { g_exception_reason_load already allocates NR_FUNCTION_RESULT_REG }
  402. cg.g_exception_reason_load(list, t.reasonbuf);
  403. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  404. cg.a_reg_dealloc(list,NR_FUNCTION_RESULT_REG);
  405. end;
  406. end;
  407. {*****************************************************************************
  408. TLocation
  409. *****************************************************************************}
  410. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  411. var
  412. reg : tregister;
  413. href : treference;
  414. begin
  415. if (l.loc<>LOC_FPUREGISTER) and
  416. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  417. begin
  418. { if it's in an mm register, store to memory first }
  419. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  420. begin
  421. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  422. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  423. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  424. l.reference:=href;
  425. end;
  426. reg:=cg.getfpuregister(list,l.size);
  427. cg.a_loadfpu_loc_reg(list,l.size,l,reg);
  428. location_freetemp(list,l);
  429. location_reset(l,LOC_FPUREGISTER,l.size);
  430. l.register:=reg;
  431. end;
  432. end;
  433. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  434. var
  435. reg : tregister;
  436. href : treference;
  437. newsize : tcgsize;
  438. begin
  439. if (l.loc<>LOC_MMREGISTER) and
  440. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  441. begin
  442. { if it's in an fpu register, store to memory first }
  443. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  444. begin
  445. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  446. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,href);
  447. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  448. l.reference:=href;
  449. end;
  450. {$ifndef cpu64bitalu}
  451. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  452. (l.size in [OS_64,OS_S64]) then
  453. begin
  454. reg:=cg.getmmregister(list,OS_F64);
  455. cg64.a_loadmm_intreg64_reg(list,OS_F64,l.register64,reg);
  456. l.size:=OS_F64
  457. end
  458. else
  459. {$endif not cpu64bitalu}
  460. begin
  461. { on ARM, CFP values may be located in integer registers,
  462. and its second_int_to_real() also uses this routine to
  463. force integer (memory) values in an mmregister }
  464. if (l.size in [OS_32,OS_S32]) then
  465. newsize:=OS_F32
  466. else if (l.size in [OS_64,OS_S64]) then
  467. newsize:=OS_F64
  468. else
  469. newsize:=l.size;
  470. reg:=cg.getmmregister(list,newsize);
  471. hlcg.a_loadmm_loc_reg(list,l.size,newsize,l,reg,mms_movescalar);
  472. l.size:=newsize;
  473. end;
  474. location_freetemp(list,l);
  475. location_reset(l,LOC_MMREGISTER,l.size);
  476. l.register:=reg;
  477. end;
  478. end;
  479. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  480. var
  481. tmpreg: tregister;
  482. begin
  483. if (setbase<>0) then
  484. begin
  485. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  486. internalerror(2007091502);
  487. { subtract the setbase }
  488. case l.loc of
  489. LOC_CREGISTER:
  490. begin
  491. tmpreg := cg.getintregister(list,l.size);
  492. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  493. l.loc:=LOC_REGISTER;
  494. l.register:=tmpreg;
  495. end;
  496. LOC_REGISTER:
  497. begin
  498. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  499. end;
  500. end;
  501. end;
  502. end;
  503. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  504. var
  505. reg : tregister;
  506. begin
  507. if (l.loc<>LOC_MMREGISTER) and
  508. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  509. begin
  510. reg:=cg.getmmregister(list,OS_VECTOR);
  511. hlcg.a_loadmm_loc_reg(list,l.size,OS_VECTOR,l,reg,nil);
  512. location_freetemp(list,l);
  513. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  514. l.register:=reg;
  515. end;
  516. end;
  517. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  518. begin
  519. l.size:=def_cgsize(def);
  520. if (def.typ=floatdef) and
  521. not(cs_fp_emulation in current_settings.moduleswitches) then
  522. begin
  523. if use_vectorfpu(def) then
  524. begin
  525. if constant then
  526. location_reset(l,LOC_CMMREGISTER,l.size)
  527. else
  528. location_reset(l,LOC_MMREGISTER,l.size);
  529. l.register:=cg.getmmregister(list,l.size);
  530. end
  531. else
  532. begin
  533. if constant then
  534. location_reset(l,LOC_CFPUREGISTER,l.size)
  535. else
  536. location_reset(l,LOC_FPUREGISTER,l.size);
  537. l.register:=cg.getfpuregister(list,l.size);
  538. end;
  539. end
  540. else
  541. begin
  542. if constant then
  543. location_reset(l,LOC_CREGISTER,l.size)
  544. else
  545. location_reset(l,LOC_REGISTER,l.size);
  546. {$ifndef cpu64bitalu}
  547. if l.size in [OS_64,OS_S64,OS_F64] then
  548. begin
  549. l.register64.reglo:=cg.getintregister(list,OS_32);
  550. l.register64.reghi:=cg.getintregister(list,OS_32);
  551. end
  552. else
  553. {$endif not cpu64bitalu}
  554. l.register:=cg.getintregister(list,l.size);
  555. end;
  556. end;
  557. {****************************************************************************
  558. Init/Finalize Code
  559. ****************************************************************************}
  560. procedure copyvalueparas(p:TObject;arg:pointer);
  561. var
  562. href : treference;
  563. hreg : tregister;
  564. list : TAsmList;
  565. hsym : tparavarsym;
  566. l : longint;
  567. localcopyloc : tlocation;
  568. sizedef : tdef;
  569. begin
  570. list:=TAsmList(arg);
  571. if (tsym(p).typ=paravarsym) and
  572. (tparavarsym(p).varspez=vs_value) and
  573. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  574. begin
  575. { we have no idea about the alignment at the caller side }
  576. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  577. if is_open_array(tparavarsym(p).vardef) or
  578. is_array_of_const(tparavarsym(p).vardef) then
  579. begin
  580. { cdecl functions don't have a high pointer so it is not possible to generate
  581. a local copy }
  582. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  583. begin
  584. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  585. if not assigned(hsym) then
  586. internalerror(200306061);
  587. hreg:=cg.getaddressregister(list);
  588. if not is_packed_array(tparavarsym(p).vardef) then
  589. cg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef).elesize,hreg)
  590. else
  591. internalerror(2006080401);
  592. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  593. sizedef:=getpointerdef(tparavarsym(p).vardef);
  594. hlcg.a_load_reg_loc(list,sizedef,sizedef,hreg,tparavarsym(p).initialloc);
  595. end;
  596. end
  597. else
  598. begin
  599. { Allocate space for the local copy }
  600. l:=tparavarsym(p).getsize;
  601. localcopyloc.loc:=LOC_REFERENCE;
  602. localcopyloc.size:=int_cgsize(l);
  603. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  604. { Copy data }
  605. if is_shortstring(tparavarsym(p).vardef) then
  606. begin
  607. { this code is only executed before the code for the body and the entry/exit code is generated
  608. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  609. }
  610. include(current_procinfo.flags,pi_do_call);
  611. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef).len)
  612. end
  613. else if tparavarsym(p).vardef.typ = variantdef then
  614. begin
  615. { this code is only executed before the code for the body and the entry/exit code is generated
  616. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  617. }
  618. include(current_procinfo.flags,pi_do_call);
  619. cg.g_copyvariant(list,href,localcopyloc.reference)
  620. end
  621. else
  622. begin
  623. { pass proper alignment info }
  624. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  625. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  626. end;
  627. { update localloc of varsym }
  628. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  629. tparavarsym(p).localloc:=localcopyloc;
  630. tparavarsym(p).initialloc:=localcopyloc;
  631. end;
  632. end;
  633. end;
  634. const
  635. {$ifdef cpu64bitalu}
  636. trashintvalues: array[0..nroftrashvalues-1] of aint = ($5555555555555555,aint($AAAAAAAAAAAAAAAA),aint($EFEFEFEFEFEFEFEF),0);
  637. {$endif cpu64bitalu}
  638. {$ifdef cpu32bitalu}
  639. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55555555,aint($AAAAAAAA),aint($EFEFEFEF),0);
  640. {$endif cpu32bitalu}
  641. {$ifdef cpu8bitalu}
  642. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55,aint($AA),aint($EF),0);
  643. {$endif cpu8bitalu}
  644. procedure trash_reference(list: TAsmList; const ref: treference; size: aint);
  645. var
  646. countreg, valuereg: tregister;
  647. hl: tasmlabel;
  648. trashintval: aint;
  649. tmpref: treference;
  650. begin
  651. trashintval := trashintvalues[localvartrashing];
  652. case size of
  653. 0: ; { empty record }
  654. 1: cg.a_load_const_ref(list,OS_8,byte(trashintval),ref);
  655. 2: cg.a_load_const_ref(list,OS_16,word(trashintval),ref);
  656. 4: cg.a_load_const_ref(list,OS_32,longint(trashintval),ref);
  657. {$ifdef cpu64bitalu}
  658. 8: cg.a_load_const_ref(list,OS_64,int64(trashintval),ref);
  659. {$endif cpu64bitalu}
  660. else
  661. begin
  662. countreg := cg.getintregister(list,OS_ADDR);
  663. valuereg := cg.getintregister(list,OS_8);
  664. cg.a_load_const_reg(list,OS_INT,size,countreg);
  665. cg.a_load_const_reg(list,OS_8,byte(trashintval),valuereg);
  666. current_asmdata.getjumplabel(hl);
  667. tmpref := ref;
  668. if (tmpref.index <> NR_NO) then
  669. internalerror(200607201);
  670. tmpref.index := countreg;
  671. dec(tmpref.offset);
  672. cg.a_label(list,hl);
  673. cg.a_load_reg_ref(list,OS_8,OS_8,valuereg,tmpref);
  674. cg.a_op_const_reg(list,OP_SUB,OS_INT,1,countreg);
  675. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,countreg,hl);
  676. cg.a_reg_sync(list,tmpref.base);
  677. cg.a_reg_sync(list,valuereg);
  678. end;
  679. end;
  680. end;
  681. { trash contents of local variables or parameters (function result) }
  682. procedure trash_variable(p:TObject;arg:pointer);
  683. var
  684. trashintval: aint;
  685. list: TAsmList absolute arg;
  686. begin
  687. if ((tsym(p).typ=localvarsym) or
  688. ((tsym(p).typ=paravarsym) and
  689. (vo_is_funcret in tparavarsym(p).varoptions))) and
  690. not(is_managed_type(tabstractnormalvarsym(p).vardef)) and
  691. not(assigned(tabstractnormalvarsym(p).defaultconstsym)) then
  692. begin
  693. trashintval := trashintvalues[localvartrashing];
  694. case tabstractnormalvarsym(p).initialloc.loc of
  695. LOC_CREGISTER :
  696. {$push}
  697. {$q-}
  698. begin
  699. { avoid problems with broken x86 shifts }
  700. case tcgsize2size[tabstractnormalvarsym(p).initialloc.size] of
  701. 1: cg.a_load_const_reg(list,OS_8,byte(trashintval),tabstractnormalvarsym(p).initialloc.register);
  702. 2: cg.a_load_const_reg(list,OS_16,word(trashintval),tabstractnormalvarsym(p).initialloc.register);
  703. 4: cg.a_load_const_reg(list,OS_32,longint(trashintval),tabstractnormalvarsym(p).initialloc.register);
  704. 8:
  705. begin
  706. {$ifdef cpu64bitalu}
  707. cg.a_load_const_reg(list,OS_64,aint(trashintval),tabstractnormalvarsym(p).initialloc.register);
  708. {$else}
  709. cg64.a_load64_const_reg(list,int64(trashintval) shl 32 or int64(trashintval),tabstractnormalvarsym(p).initialloc.register64);
  710. {$endif}
  711. end;
  712. else
  713. internalerror(2010060801);
  714. end;
  715. end;
  716. {$pop}
  717. LOC_REFERENCE :
  718. begin
  719. if ((tsym(p).typ=localvarsym) and
  720. not(vo_is_funcret in tabstractvarsym(p).varoptions)) or
  721. not is_shortstring(tabstractnormalvarsym(p).vardef) then
  722. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  723. tlocalvarsym(p).getsize)
  724. else
  725. { may be an open string, even if is_open_string() returns }
  726. { false (for some helpers in the system unit) }
  727. { an open string has at least size 2 }
  728. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  729. 2);
  730. end;
  731. LOC_CMMREGISTER :
  732. ;
  733. LOC_CFPUREGISTER :
  734. ;
  735. else
  736. internalerror(200410124);
  737. end;
  738. end;
  739. end;
  740. { generates the code for incrementing the reference count of parameters and
  741. initialize out parameters }
  742. procedure init_paras(p:TObject;arg:pointer);
  743. var
  744. href : treference;
  745. hsym : tparavarsym;
  746. eldef : tdef;
  747. list : TAsmList;
  748. needs_inittable,
  749. do_trashing : boolean;
  750. begin
  751. list:=TAsmList(arg);
  752. if (tsym(p).typ=paravarsym) then
  753. begin
  754. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  755. do_trashing:=
  756. (localvartrashing <> -1) and
  757. (not assigned(tparavarsym(p).defaultconstsym)) and
  758. not needs_inittable;
  759. case tparavarsym(p).varspez of
  760. vs_value :
  761. if needs_inittable then
  762. begin
  763. { variants are already handled by the call to fpc_variant_copy_overwrite if
  764. they are passed by reference }
  765. if not((tparavarsym(p).vardef.typ=variantdef) and
  766. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  767. begin
  768. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  769. if is_open_array(tparavarsym(p).vardef) then
  770. begin
  771. { open arrays do not contain correct element count in their rtti,
  772. the actual count must be passed separately. }
  773. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  774. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  775. if not assigned(hsym) then
  776. internalerror(201003031);
  777. cg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'FPC_ADDREF_ARRAY');
  778. end
  779. else
  780. cg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  781. end;
  782. end;
  783. vs_out :
  784. begin
  785. if needs_inittable or
  786. do_trashing then
  787. begin
  788. { we have no idea about the alignment at the callee side,
  789. and the user also cannot specify "unaligned" here, so
  790. assume worst case }
  791. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  792. if do_trashing and
  793. { needs separate implementation to trash open arrays }
  794. { since their size is only known at run time }
  795. not is_special_array(tparavarsym(p).vardef) then
  796. { may be an open string, even if is_open_string() returns }
  797. { false (for some helpers in the system unit) }
  798. if not is_shortstring(tparavarsym(p).vardef) then
  799. trash_reference(list,href,tparavarsym(p).vardef.size)
  800. else
  801. trash_reference(list,href,2);
  802. if needs_inittable then
  803. begin
  804. if is_open_array(tparavarsym(p).vardef) then
  805. begin
  806. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  807. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  808. if not assigned(hsym) then
  809. internalerror(201103033);
  810. cg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'FPC_INITIALIZE_ARRAY');
  811. end
  812. else
  813. cg.g_initialize(list,tparavarsym(p).vardef,href);
  814. end;
  815. end;
  816. end;
  817. else if do_trashing and
  818. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  819. begin
  820. { should always have standard alignment. If a function is assigned
  821. to a non-aligned variable, the optimisation to pass this variable
  822. directly as hidden function result must/cannot be performed
  823. (see tcallnode.funcret_can_be_reused)
  824. }
  825. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  826. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  827. { may be an open string, even if is_open_string() returns }
  828. { false (for some helpers in the system unit) }
  829. if not is_shortstring(tparavarsym(p).vardef) then
  830. trash_reference(list,href,tparavarsym(p).vardef.size)
  831. else
  832. { an open string has at least size 2 }
  833. trash_reference(list,href,2);
  834. end
  835. end;
  836. end;
  837. end;
  838. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  839. begin
  840. case loc.loc of
  841. LOC_CREGISTER:
  842. begin
  843. {$ifndef cpu64bitalu}
  844. if loc.size in [OS_64,OS_S64] then
  845. begin
  846. loc.register64.reglo:=cg.getintregister(list,OS_32);
  847. loc.register64.reghi:=cg.getintregister(list,OS_32);
  848. end
  849. else
  850. {$endif cpu64bitalu}
  851. loc.register:=cg.getintregister(list,loc.size);
  852. end;
  853. LOC_CFPUREGISTER:
  854. begin
  855. loc.register:=cg.getfpuregister(list,loc.size);
  856. end;
  857. LOC_CMMREGISTER:
  858. begin
  859. loc.register:=cg.getmmregister(list,loc.size);
  860. end;
  861. end;
  862. end;
  863. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  864. begin
  865. if allocreg then
  866. gen_alloc_regloc(list,sym.initialloc);
  867. if (pi_has_label in current_procinfo.flags) then
  868. begin
  869. { Allocate register already, to prevent first allocation to be
  870. inside a loop }
  871. {$ifndef cpu64bitalu}
  872. if sym.initialloc.size in [OS_64,OS_S64] then
  873. begin
  874. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  875. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  876. end
  877. else
  878. {$endif not cpu64bitalu}
  879. cg.a_reg_sync(list,sym.initialloc.register);
  880. end;
  881. sym.localloc:=sym.initialloc;
  882. end;
  883. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  884. procedure unget_para(const paraloc:TCGParaLocation);
  885. begin
  886. case paraloc.loc of
  887. LOC_REGISTER :
  888. begin
  889. if getsupreg(paraloc.register)<first_int_imreg then
  890. cg.ungetcpuregister(list,paraloc.register);
  891. end;
  892. LOC_MMREGISTER :
  893. begin
  894. if getsupreg(paraloc.register)<first_mm_imreg then
  895. cg.ungetcpuregister(list,paraloc.register);
  896. end;
  897. LOC_FPUREGISTER :
  898. begin
  899. if getsupreg(paraloc.register)<first_fpu_imreg then
  900. cg.ungetcpuregister(list,paraloc.register);
  901. end;
  902. end;
  903. end;
  904. var
  905. paraloc : pcgparalocation;
  906. href : treference;
  907. sizeleft : aint;
  908. {$if defined(sparc) or defined(arm)}
  909. tempref : treference;
  910. {$endif sparc}
  911. {$ifndef cpu64bitalu}
  912. tempreg : tregister;
  913. reg64 : tregister64;
  914. {$endif not cpu64bitalu}
  915. begin
  916. paraloc:=para.location;
  917. if not assigned(paraloc) then
  918. internalerror(200408203);
  919. { skip e.g. empty records }
  920. if (paraloc^.loc = LOC_VOID) then
  921. exit;
  922. case destloc.loc of
  923. LOC_REFERENCE :
  924. begin
  925. { If the parameter location is reused we don't need to copy
  926. anything }
  927. if not reusepara then
  928. begin
  929. href:=destloc.reference;
  930. sizeleft:=para.intsize;
  931. while assigned(paraloc) do
  932. begin
  933. if (paraloc^.size=OS_NO) then
  934. begin
  935. { Can only be a reference that contains the rest
  936. of the parameter }
  937. if (paraloc^.loc<>LOC_REFERENCE) or
  938. assigned(paraloc^.next) then
  939. internalerror(2005013010);
  940. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  941. inc(href.offset,sizeleft);
  942. sizeleft:=0;
  943. end
  944. else
  945. begin
  946. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  947. inc(href.offset,TCGSize2Size[paraloc^.size]);
  948. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  949. end;
  950. unget_para(paraloc^);
  951. paraloc:=paraloc^.next;
  952. end;
  953. end;
  954. end;
  955. LOC_REGISTER,
  956. LOC_CREGISTER :
  957. begin
  958. {$ifndef cpu64bitalu}
  959. if (para.size in [OS_64,OS_S64,OS_F64]) and
  960. (is_64bit(vardef) or
  961. { in case of fpu emulation, or abi's that pass fpu values
  962. via integer registers }
  963. (vardef.typ=floatdef)) then
  964. begin
  965. case paraloc^.loc of
  966. LOC_REGISTER:
  967. begin
  968. if not assigned(paraloc^.next) then
  969. internalerror(200410104);
  970. if (target_info.endian=ENDIAN_BIG) then
  971. begin
  972. { paraloc^ -> high
  973. paraloc^.next -> low }
  974. unget_para(paraloc^);
  975. gen_alloc_regloc(list,destloc);
  976. { reg->reg, alignment is irrelevant }
  977. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  978. unget_para(paraloc^.next^);
  979. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  980. end
  981. else
  982. begin
  983. { paraloc^ -> low
  984. paraloc^.next -> high }
  985. unget_para(paraloc^);
  986. gen_alloc_regloc(list,destloc);
  987. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  988. unget_para(paraloc^.next^);
  989. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  990. end;
  991. end;
  992. LOC_REFERENCE:
  993. begin
  994. gen_alloc_regloc(list,destloc);
  995. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  996. cg64.a_load64_ref_reg(list,href,destloc.register64);
  997. unget_para(paraloc^);
  998. end;
  999. else
  1000. internalerror(2005101501);
  1001. end
  1002. end
  1003. else
  1004. {$endif not cpu64bitalu}
  1005. begin
  1006. if assigned(paraloc^.next) then
  1007. internalerror(200410105);
  1008. unget_para(paraloc^);
  1009. gen_alloc_regloc(list,destloc);
  1010. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  1011. end;
  1012. end;
  1013. LOC_FPUREGISTER,
  1014. LOC_CFPUREGISTER :
  1015. begin
  1016. {$if defined(sparc) or defined(arm)}
  1017. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1018. we need a temp }
  1019. sizeleft := TCGSize2Size[destloc.size];
  1020. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1021. href:=tempref;
  1022. while assigned(paraloc) do
  1023. begin
  1024. unget_para(paraloc^);
  1025. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1026. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1027. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1028. paraloc:=paraloc^.next;
  1029. end;
  1030. gen_alloc_regloc(list,destloc);
  1031. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1032. tg.UnGetTemp(list,tempref);
  1033. {$else sparc}
  1034. unget_para(paraloc^);
  1035. gen_alloc_regloc(list,destloc);
  1036. { from register to register -> alignment is irrelevant }
  1037. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1038. if assigned(paraloc^.next) then
  1039. internalerror(200410109);
  1040. {$endif sparc}
  1041. end;
  1042. LOC_MMREGISTER,
  1043. LOC_CMMREGISTER :
  1044. begin
  1045. {$ifndef cpu64bitalu}
  1046. { ARM vfp floats are passed in integer registers }
  1047. if (para.size=OS_F64) and
  1048. (paraloc^.size in [OS_32,OS_S32]) and
  1049. use_vectorfpu(vardef) then
  1050. begin
  1051. { we need 2x32bit reg }
  1052. if not assigned(paraloc^.next) or
  1053. assigned(paraloc^.next^.next) then
  1054. internalerror(2009112421);
  1055. unget_para(paraloc^.next^);
  1056. case paraloc^.next^.loc of
  1057. LOC_REGISTER:
  1058. tempreg:=paraloc^.next^.register;
  1059. LOC_REFERENCE:
  1060. begin
  1061. tempreg:=cg.getintregister(list,OS_32);
  1062. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1063. end;
  1064. else
  1065. internalerror(2012051301);
  1066. end;
  1067. { don't free before the above, because then the getintregister
  1068. could reallocate this register and overwrite it }
  1069. unget_para(paraloc^);
  1070. gen_alloc_regloc(list,destloc);
  1071. if (target_info.endian=endian_big) then
  1072. { paraloc^ -> high
  1073. paraloc^.next -> low }
  1074. reg64:=joinreg64(tempreg,paraloc^.register)
  1075. else
  1076. reg64:=joinreg64(paraloc^.register,tempreg);
  1077. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1078. end
  1079. else
  1080. {$endif not cpu64bitalu}
  1081. begin
  1082. unget_para(paraloc^);
  1083. gen_alloc_regloc(list,destloc);
  1084. { from register to register -> alignment is irrelevant }
  1085. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1086. { data could come in two memory locations, for now
  1087. we simply ignore the sanity check (FK)
  1088. if assigned(paraloc^.next) then
  1089. internalerror(200410108);
  1090. }
  1091. end;
  1092. end;
  1093. else
  1094. internalerror(2010052903);
  1095. end;
  1096. end;
  1097. procedure gen_load_para_value(list:TAsmList);
  1098. procedure get_para(const paraloc:TCGParaLocation);
  1099. begin
  1100. case paraloc.loc of
  1101. LOC_REGISTER :
  1102. begin
  1103. if getsupreg(paraloc.register)<first_int_imreg then
  1104. cg.getcpuregister(list,paraloc.register);
  1105. end;
  1106. LOC_MMREGISTER :
  1107. begin
  1108. if getsupreg(paraloc.register)<first_mm_imreg then
  1109. cg.getcpuregister(list,paraloc.register);
  1110. end;
  1111. LOC_FPUREGISTER :
  1112. begin
  1113. if getsupreg(paraloc.register)<first_fpu_imreg then
  1114. cg.getcpuregister(list,paraloc.register);
  1115. end;
  1116. end;
  1117. end;
  1118. var
  1119. i : longint;
  1120. currpara : tparavarsym;
  1121. paraloc : pcgparalocation;
  1122. begin
  1123. if (po_assembler in current_procinfo.procdef.procoptions) or
  1124. { exceptfilters have a single hidden 'parentfp' parameter, which
  1125. is handled by tcg.g_proc_entry. }
  1126. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1127. exit;
  1128. { Allocate registers used by parameters }
  1129. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1130. begin
  1131. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1132. paraloc:=currpara.paraloc[calleeside].location;
  1133. while assigned(paraloc) do
  1134. begin
  1135. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1136. get_para(paraloc^);
  1137. paraloc:=paraloc^.next;
  1138. end;
  1139. end;
  1140. { Copy parameters to local references/registers }
  1141. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1142. begin
  1143. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1144. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1145. { gen_load_cgpara_loc() already allocated the initialloc
  1146. -> don't allocate again }
  1147. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1148. gen_alloc_regvar(list,currpara,false);
  1149. end;
  1150. { generate copies of call by value parameters, must be done before
  1151. the initialization and body is parsed because the refcounts are
  1152. incremented using the local copies }
  1153. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1154. {$ifdef powerpc}
  1155. { unget the register that contains the stack pointer before the procedure entry, }
  1156. { which is used to access the parameters in their original callee-side location }
  1157. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1158. cg.a_reg_dealloc(list,NR_R12);
  1159. {$endif powerpc}
  1160. {$ifdef powerpc64}
  1161. { unget the register that contains the stack pointer before the procedure entry, }
  1162. { which is used to access the parameters in their original callee-side location }
  1163. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1164. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1165. {$endif powerpc64}
  1166. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1167. begin
  1168. { has to be done here rather than in gen_initialize_code, because
  1169. the initialisation code is generated a) later and b) with
  1170. rad_backwards, so the register allocator would generate
  1171. information as if this code comes before loading the parameters
  1172. from their original registers to their local location }
  1173. if (localvartrashing <> -1) then
  1174. current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  1175. { initialize refcounted paras, and trash others. Needed here
  1176. instead of in gen_initialize_code, because when a reference is
  1177. intialised or trashed while the pointer to that reference is kept
  1178. in a regvar, we add a register move and that one again has to
  1179. come after the parameter loading code as far as the register
  1180. allocator is concerned }
  1181. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1182. end;
  1183. end;
  1184. {****************************************************************************
  1185. Entry/Exit
  1186. ****************************************************************************}
  1187. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1188. var
  1189. item : TCmdStrListItem;
  1190. begin
  1191. result:=true;
  1192. if pd.mangledname=s then
  1193. exit;
  1194. item := TCmdStrListItem(pd.aliasnames.first);
  1195. while assigned(item) do
  1196. begin
  1197. if item.str=s then
  1198. exit;
  1199. item := TCmdStrListItem(item.next);
  1200. end;
  1201. result:=false;
  1202. end;
  1203. procedure alloc_proc_symbol(pd: tprocdef);
  1204. var
  1205. item : TCmdStrListItem;
  1206. begin
  1207. item := TCmdStrListItem(pd.aliasnames.first);
  1208. while assigned(item) do
  1209. begin
  1210. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1211. item := TCmdStrListItem(item.next);
  1212. end;
  1213. end;
  1214. procedure gen_proc_symbol(list:TAsmList);
  1215. var
  1216. item,
  1217. previtem : TCmdStrListItem;
  1218. begin
  1219. previtem:=nil;
  1220. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1221. while assigned(item) do
  1222. begin
  1223. {$ifdef arm}
  1224. if current_settings.cputype in cpu_thumb2 then
  1225. list.concat(tai_thumb_func.create);
  1226. {$endif arm}
  1227. { "double link" all procedure entry symbols via .reference }
  1228. { directives on darwin, because otherwise the linker }
  1229. { sometimes strips the procedure if only on of the symbols }
  1230. { is referenced }
  1231. if assigned(previtem) and
  1232. (target_info.system in systems_darwin) then
  1233. list.concat(tai_directive.create(asd_reference,item.str));
  1234. if (cs_profile in current_settings.moduleswitches) or
  1235. (po_global in current_procinfo.procdef.procoptions) then
  1236. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1237. else
  1238. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1239. if assigned(previtem) and
  1240. (target_info.system in systems_darwin) then
  1241. list.concat(tai_directive.create(asd_reference,previtem.str));
  1242. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1243. list.concat(Tai_function_name.create(item.str));
  1244. previtem:=item;
  1245. item := TCmdStrListItem(item.next);
  1246. end;
  1247. current_procinfo.procdef.procstarttai:=tai(list.last);
  1248. end;
  1249. procedure gen_proc_symbol_end(list:TAsmList);
  1250. begin
  1251. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1252. current_procinfo.procdef.procendtai:=tai(list.last);
  1253. if (current_module.islibrary) then
  1254. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1255. { setinitname may generate a new section -> don't add to the
  1256. current list, because we assume this remains a text section }
  1257. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  1258. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1259. begin
  1260. if (target_info.system in (systems_darwin+[system_powerpc_macos]+systems_aix)) and
  1261. not(current_module.islibrary) then
  1262. begin
  1263. new_section(list,sec_code,'',4);
  1264. list.concat(tai_symbol.createname_global(
  1265. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1266. { keep argc, argv and envp properly on the stack }
  1267. if not(target_info.system in systems_aix) then
  1268. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN')
  1269. else
  1270. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN',false)
  1271. end;
  1272. end;
  1273. end;
  1274. procedure gen_proc_entry_code(list:TAsmList);
  1275. var
  1276. hitemp,
  1277. lotemp : longint;
  1278. begin
  1279. { generate call frame marker for dwarf call frame info }
  1280. current_asmdata.asmcfi.start_frame(list);
  1281. { All temps are know, write offsets used for information }
  1282. if (cs_asm_source in current_settings.globalswitches) then
  1283. begin
  1284. if tg.direction>0 then
  1285. begin
  1286. lotemp:=current_procinfo.tempstart;
  1287. hitemp:=tg.lasttemp;
  1288. end
  1289. else
  1290. begin
  1291. lotemp:=tg.lasttemp;
  1292. hitemp:=current_procinfo.tempstart;
  1293. end;
  1294. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1295. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1296. end;
  1297. { generate target specific proc entry code }
  1298. hlcg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1299. end;
  1300. procedure gen_proc_exit_code(list:TAsmList);
  1301. var
  1302. parasize : longint;
  1303. begin
  1304. { c style clearstack does not need to remove parameters from the stack, only the
  1305. return value when it was pushed by arguments }
  1306. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1307. begin
  1308. parasize:=0;
  1309. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1310. inc(parasize,sizeof(pint));
  1311. end
  1312. else
  1313. begin
  1314. parasize:=current_procinfo.para_stack_size;
  1315. { the parent frame pointer para has to be removed by the caller in
  1316. case of Delphi-style parent frame pointer passing }
  1317. if not paramanager.use_fixed_stack and
  1318. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1319. dec(parasize,sizeof(pint));
  1320. end;
  1321. { generate target specific proc exit code }
  1322. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1323. { release return registers, needed for optimizer }
  1324. if not is_void(current_procinfo.procdef.returndef) then
  1325. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1326. { end of frame marker for call frame info }
  1327. current_asmdata.asmcfi.end_frame(list);
  1328. end;
  1329. procedure gen_stack_check_size_para(list:TAsmList);
  1330. var
  1331. paraloc1 : tcgpara;
  1332. begin
  1333. paraloc1.init;
  1334. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1335. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1336. paramanager.freecgpara(list,paraloc1);
  1337. paraloc1.done;
  1338. end;
  1339. procedure gen_stack_check_call(list:TAsmList);
  1340. var
  1341. paraloc1 : tcgpara;
  1342. begin
  1343. paraloc1.init;
  1344. { Also alloc the register needed for the parameter }
  1345. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1346. paramanager.freecgpara(list,paraloc1);
  1347. { Call the helper }
  1348. cg.allocallcpuregisters(list);
  1349. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1350. cg.deallocallcpuregisters(list);
  1351. paraloc1.done;
  1352. end;
  1353. procedure gen_save_used_regs(list:TAsmList);
  1354. begin
  1355. { Pure assembler routines need to save the registers themselves }
  1356. if (po_assembler in current_procinfo.procdef.procoptions) then
  1357. exit;
  1358. { oldfpccall expects all registers to be destroyed }
  1359. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1360. cg.g_save_registers(list);
  1361. end;
  1362. procedure gen_restore_used_regs(list:TAsmList);
  1363. begin
  1364. { Pure assembler routines need to save the registers themselves }
  1365. if (po_assembler in current_procinfo.procdef.procoptions) then
  1366. exit;
  1367. { oldfpccall expects all registers to be destroyed }
  1368. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1369. cg.g_restore_registers(list);
  1370. end;
  1371. {****************************************************************************
  1372. External handling
  1373. ****************************************************************************}
  1374. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1375. begin
  1376. create_hlcodegen;
  1377. { add the procedure to the al_procedures }
  1378. maybe_new_object_file(list);
  1379. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1380. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1381. if (po_global in pd.procoptions) then
  1382. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1383. else
  1384. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1385. cg.g_external_wrapper(list,pd,externalname);
  1386. destroy_hlcodegen;
  1387. end;
  1388. {****************************************************************************
  1389. Const Data
  1390. ****************************************************************************}
  1391. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  1392. procedure setlocalloc(vs:tabstractnormalvarsym);
  1393. begin
  1394. if cs_asm_source in current_settings.globalswitches then
  1395. begin
  1396. case vs.initialloc.loc of
  1397. LOC_REFERENCE :
  1398. begin
  1399. if not assigned(vs.initialloc.reference.symbol) then
  1400. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  1401. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  1402. end;
  1403. end;
  1404. end;
  1405. vs.localloc:=vs.initialloc;
  1406. end;
  1407. var
  1408. i : longint;
  1409. sym : tsym;
  1410. vs : tabstractnormalvarsym;
  1411. isaddr : boolean;
  1412. begin
  1413. for i:=0 to st.SymList.Count-1 do
  1414. begin
  1415. sym:=tsym(st.SymList[i]);
  1416. case sym.typ of
  1417. staticvarsym :
  1418. begin
  1419. vs:=tabstractnormalvarsym(sym);
  1420. { The code in loadnode.pass_generatecode will create the
  1421. LOC_REFERENCE instead for all none register variables. This is
  1422. required because we can't store an asmsymbol in the localloc because
  1423. the asmsymbol is invalid after an unit is compiled. This gives
  1424. problems when this procedure is inlined in another unit (PFV) }
  1425. if vs.is_regvar(false) then
  1426. begin
  1427. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1428. vs.initialloc.size:=def_cgsize(vs.vardef);
  1429. gen_alloc_regvar(list,vs,true);
  1430. setlocalloc(vs);
  1431. end;
  1432. end;
  1433. paravarsym :
  1434. begin
  1435. vs:=tabstractnormalvarsym(sym);
  1436. { Parameters passed to assembler procedures need to be kept
  1437. in the original location }
  1438. if (po_assembler in current_procinfo.procdef.procoptions) then
  1439. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1440. { exception filters receive their frame pointer as a parameter }
  1441. else if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) and
  1442. (vo_is_parentfp in vs.varoptions) then
  1443. begin
  1444. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1445. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1446. end
  1447. else
  1448. begin
  1449. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  1450. if isaddr then
  1451. vs.initialloc.size:=OS_ADDR
  1452. else
  1453. vs.initialloc.size:=def_cgsize(vs.vardef);
  1454. if vs.is_regvar(isaddr) then
  1455. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1456. else
  1457. begin
  1458. vs.initialloc.loc:=LOC_REFERENCE;
  1459. { Reuse the parameter location for values to are at a single location on the stack }
  1460. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1461. begin
  1462. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1463. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  1464. end
  1465. else
  1466. begin
  1467. if isaddr then
  1468. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  1469. else
  1470. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1471. end;
  1472. end;
  1473. end;
  1474. setlocalloc(vs);
  1475. end;
  1476. localvarsym :
  1477. begin
  1478. vs:=tabstractnormalvarsym(sym);
  1479. vs.initialloc.size:=def_cgsize(vs.vardef);
  1480. if (m_delphi in current_settings.modeswitches) and
  1481. (po_assembler in current_procinfo.procdef.procoptions) and
  1482. (vo_is_funcret in vs.varoptions) and
  1483. (vs.refs=0) then
  1484. begin
  1485. { not referenced, so don't allocate. Use dummy to }
  1486. { avoid ie's later on because of LOC_INVALID }
  1487. vs.initialloc.loc:=LOC_REGISTER;
  1488. vs.initialloc.size:=OS_INT;
  1489. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1490. end
  1491. else if vs.is_regvar(false) then
  1492. begin
  1493. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1494. gen_alloc_regvar(list,vs,true);
  1495. end
  1496. else
  1497. begin
  1498. vs.initialloc.loc:=LOC_REFERENCE;
  1499. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1500. end;
  1501. setlocalloc(vs);
  1502. end;
  1503. end;
  1504. end;
  1505. end;
  1506. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1507. begin
  1508. case location.loc of
  1509. LOC_CREGISTER:
  1510. {$ifndef cpu64bitalu}
  1511. if location.size in [OS_64,OS_S64] then
  1512. begin
  1513. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1514. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1515. end
  1516. else
  1517. {$endif not cpu64bitalu}
  1518. rv.intregvars.addnodup(getsupreg(location.register));
  1519. LOC_CFPUREGISTER:
  1520. rv.fpuregvars.addnodup(getsupreg(location.register));
  1521. LOC_CMMREGISTER:
  1522. rv.mmregvars.addnodup(getsupreg(location.register));
  1523. end;
  1524. end;
  1525. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1526. var
  1527. rv: pusedregvars absolute arg;
  1528. begin
  1529. case (n.nodetype) of
  1530. temprefn:
  1531. { We only have to synchronise a tempnode before a loop if it is }
  1532. { not created inside the loop, and only synchronise after the }
  1533. { loop if it's not destroyed inside the loop. If it's created }
  1534. { before the loop and not yet destroyed, then before the loop }
  1535. { is secondpassed tempinfo^.valid will be true, and we get the }
  1536. { correct registers. If it's not destroyed inside the loop, }
  1537. { then after the loop has been secondpassed tempinfo^.valid }
  1538. { be true and we also get the right registers. In other cases, }
  1539. { tempinfo^.valid will be false and so we do not add }
  1540. { unnecessary registers. This way, we don't have to look at }
  1541. { tempcreate and tempdestroy nodes to get this info (JM) }
  1542. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1543. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1544. loadn:
  1545. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1546. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1547. vecn:
  1548. { range checks sometimes need the high parameter }
  1549. if (cs_check_range in current_settings.localswitches) and
  1550. (is_open_array(tvecnode(n).left.resultdef) or
  1551. is_array_of_const(tvecnode(n).left.resultdef)) and
  1552. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1553. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1554. end;
  1555. result := fen_true;
  1556. end;
  1557. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1558. begin
  1559. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1560. end;
  1561. (*
  1562. See comments at declaration of pusedregvarscommon
  1563. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1564. var
  1565. rv: pusedregvarscommon absolute arg;
  1566. begin
  1567. if (n.nodetype = loadn) and
  1568. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1569. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1570. case loc of
  1571. LOC_CREGISTER:
  1572. { if not yet encountered in this node tree }
  1573. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1574. { but nevertheless already encountered somewhere }
  1575. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1576. { then it's a regvar used in two or more node trees }
  1577. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1578. LOC_CFPUREGISTER:
  1579. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1580. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1581. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1582. LOC_CMMREGISTER:
  1583. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1584. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1585. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1586. end;
  1587. result := fen_true;
  1588. end;
  1589. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1590. begin
  1591. rv.myregvars.intregvars.clear;
  1592. rv.myregvars.fpuregvars.clear;
  1593. rv.myregvars.mmregvars.clear;
  1594. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1595. end;
  1596. *)
  1597. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1598. var
  1599. count: longint;
  1600. begin
  1601. for count := 1 to rv.intregvars.length do
  1602. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1603. for count := 1 to rv.fpuregvars.length do
  1604. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1605. for count := 1 to rv.mmregvars.length do
  1606. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1607. end;
  1608. {*****************************************************************************
  1609. SSA support
  1610. *****************************************************************************}
  1611. type
  1612. preplaceregrec = ^treplaceregrec;
  1613. treplaceregrec = record
  1614. old, new: tregister;
  1615. {$ifndef cpu64bitalu}
  1616. oldhi, newhi: tregister;
  1617. {$endif not cpu64bitalu}
  1618. ressym: tsym;
  1619. { moved sym }
  1620. sym : tsym;
  1621. end;
  1622. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  1623. var
  1624. rr: preplaceregrec absolute para;
  1625. begin
  1626. result := fen_false;
  1627. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  1628. exit;
  1629. case n.nodetype of
  1630. loadn:
  1631. begin
  1632. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  1633. not assigned(tloadnode(n).left) and
  1634. ((tloadnode(n).symtableentry <> rr^.ressym) or
  1635. not(fc_exit in flowcontrol)
  1636. ) and
  1637. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1638. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  1639. begin
  1640. {$ifndef cpu64bitalu}
  1641. { it's possible a 64 bit location was shifted and/xor typecasted }
  1642. { in a 32 bit value, so only 1 register was left in the location }
  1643. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  1644. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  1645. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  1646. else
  1647. exit;
  1648. {$endif not cpu64bitalu}
  1649. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  1650. rr^.sym := tabstractnormalvarsym(tloadnode(n).symtableentry);
  1651. result := fen_norecurse_true;
  1652. end;
  1653. end;
  1654. temprefn:
  1655. begin
  1656. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  1657. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1658. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  1659. begin
  1660. {$ifndef cpu64bitalu}
  1661. { it's possible a 64 bit location was shifted and/xor typecasted }
  1662. { in a 32 bit value, so only 1 register was left in the location }
  1663. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  1664. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  1665. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  1666. else
  1667. exit;
  1668. {$endif not cpu64bitalu}
  1669. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  1670. result := fen_norecurse_true;
  1671. end;
  1672. end;
  1673. { optimize the searching a bit }
  1674. derefn,addrn,
  1675. calln,inlinen,casen,
  1676. addn,subn,muln,
  1677. andn,orn,xorn,
  1678. ltn,lten,gtn,gten,equaln,unequaln,
  1679. slashn,divn,shrn,shln,notn,
  1680. inn,
  1681. asn,isn:
  1682. result := fen_norecurse_false;
  1683. end;
  1684. end;
  1685. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  1686. var
  1687. rr: treplaceregrec;
  1688. begin
  1689. {$ifdef jvm}
  1690. exit;
  1691. {$endif}
  1692. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  1693. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  1694. exit;
  1695. rr.old := n.location.register;
  1696. rr.ressym := nil;
  1697. rr.sym := nil;
  1698. {$ifndef cpu64bitalu}
  1699. rr.oldhi := NR_NO;
  1700. {$endif not cpu64bitalu}
  1701. case n.location.loc of
  1702. LOC_CREGISTER:
  1703. begin
  1704. {$ifndef cpu64bitalu}
  1705. if (n.location.size in [OS_64,OS_S64]) then
  1706. begin
  1707. rr.oldhi := n.location.register64.reghi;
  1708. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1709. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1710. end
  1711. else
  1712. {$endif not cpu64bitalu}
  1713. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  1714. end;
  1715. LOC_CFPUREGISTER:
  1716. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  1717. {$ifdef SUPPORT_MMX}
  1718. LOC_CMMXREGISTER:
  1719. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  1720. {$endif SUPPORT_MMX}
  1721. LOC_CMMREGISTER:
  1722. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  1723. else
  1724. exit;
  1725. end;
  1726. if not is_void(current_procinfo.procdef.returndef) and
  1727. assigned(current_procinfo.procdef.funcretsym) and
  1728. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  1729. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1730. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  1731. else
  1732. rr.ressym:=current_procinfo.procdef.funcretsym;
  1733. if not foreachnodestatic(n,@doreplace,@rr) then
  1734. exit;
  1735. if reload then
  1736. case n.location.loc of
  1737. LOC_CREGISTER:
  1738. begin
  1739. {$ifndef cpu64bitalu}
  1740. if (n.location.size in [OS_64,OS_S64]) then
  1741. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  1742. else
  1743. {$endif not cpu64bitalu}
  1744. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1745. end;
  1746. LOC_CFPUREGISTER:
  1747. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1748. {$ifdef SUPPORT_MMX}
  1749. LOC_CMMXREGISTER:
  1750. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  1751. {$endif SUPPORT_MMX}
  1752. LOC_CMMREGISTER:
  1753. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  1754. else
  1755. internalerror(2006090920);
  1756. end;
  1757. { now that we've change the loadn/temp, also change the node result location }
  1758. {$ifndef cpu64bitalu}
  1759. if (n.location.size in [OS_64,OS_S64]) then
  1760. begin
  1761. n.location.register64.reglo := rr.new;
  1762. n.location.register64.reghi := rr.newhi;
  1763. if assigned(rr.sym) then
  1764. list.concat(tai_varloc.create64(rr.sym,rr.new,rr.newhi));
  1765. end
  1766. else
  1767. {$endif not cpu64bitalu}
  1768. begin
  1769. n.location.register := rr.new;
  1770. if assigned(rr.sym) then
  1771. list.concat(tai_varloc.create(rr.sym,rr.new));
  1772. end;
  1773. end;
  1774. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1775. var
  1776. i : longint;
  1777. sym : tsym;
  1778. begin
  1779. for i:=0 to st.SymList.Count-1 do
  1780. begin
  1781. sym:=tsym(st.SymList[i]);
  1782. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1783. begin
  1784. with tabstractnormalvarsym(sym) do
  1785. begin
  1786. { Note: We need to keep the data available in memory
  1787. for the sub procedures that can access local data
  1788. in the parent procedures }
  1789. case localloc.loc of
  1790. LOC_CREGISTER :
  1791. if (pi_has_label in current_procinfo.flags) then
  1792. {$ifndef cpu64bitalu}
  1793. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1794. begin
  1795. cg.a_reg_sync(list,localloc.register64.reglo);
  1796. cg.a_reg_sync(list,localloc.register64.reghi);
  1797. end
  1798. else
  1799. {$endif not cpu64bitalu}
  1800. cg.a_reg_sync(list,localloc.register);
  1801. LOC_CFPUREGISTER,
  1802. LOC_CMMREGISTER:
  1803. if (pi_has_label in current_procinfo.flags) then
  1804. cg.a_reg_sync(list,localloc.register);
  1805. LOC_REFERENCE :
  1806. begin
  1807. if typ in [localvarsym,paravarsym] then
  1808. tg.Ungetlocal(list,localloc.reference);
  1809. end;
  1810. end;
  1811. end;
  1812. end;
  1813. end;
  1814. end;
  1815. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  1816. var
  1817. i,j : longint;
  1818. tmps : string;
  1819. pd : TProcdef;
  1820. ImplIntf : TImplementedInterface;
  1821. begin
  1822. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1823. begin
  1824. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1825. if (ImplIntf=ImplIntf.VtblImplIntf) and
  1826. assigned(ImplIntf.ProcDefs) then
  1827. begin
  1828. maybe_new_object_file(list);
  1829. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  1830. begin
  1831. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  1832. { we don't track method calls via interfaces yet ->
  1833. assume that every method called via an interface call
  1834. is reachable for now }
  1835. if (po_virtualmethod in pd.procoptions) and
  1836. not is_objectpascal_helper(tprocdef(pd).struct) then
  1837. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  1838. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1839. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  1840. { create wrapper code }
  1841. new_section(list,sec_code,tmps,0);
  1842. hlcg.init_register_allocators;
  1843. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  1844. hlcg.done_register_allocators;
  1845. end;
  1846. end;
  1847. end;
  1848. end;
  1849. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  1850. var
  1851. i : longint;
  1852. def : tdef;
  1853. begin
  1854. if not nested then
  1855. create_hlcodegen;
  1856. for i:=0 to st.DefList.Count-1 do
  1857. begin
  1858. def:=tdef(st.DefList[i]);
  1859. { if def can contain nested types then handle it symtable }
  1860. if def.typ in [objectdef,recorddef] then
  1861. gen_intf_wrappers(list,tabstractrecorddef(def).symtable,true);
  1862. if is_class(def) then
  1863. gen_intf_wrapper(list,tobjectdef(def));
  1864. end;
  1865. if not nested then
  1866. destroy_hlcodegen;
  1867. end;
  1868. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1869. var
  1870. href : treference;
  1871. begin
  1872. if is_object(objdef) then
  1873. begin
  1874. case selfloc.loc of
  1875. LOC_CREFERENCE,
  1876. LOC_REFERENCE:
  1877. begin
  1878. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1879. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1880. end;
  1881. else
  1882. internalerror(200305056);
  1883. end;
  1884. end
  1885. else
  1886. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  1887. and the first "field" of an Objective-C class instance is a pointer
  1888. to its "meta-class". }
  1889. begin
  1890. case selfloc.loc of
  1891. LOC_REGISTER:
  1892. begin
  1893. {$ifdef cpu_uses_separate_address_registers}
  1894. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  1895. begin
  1896. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1897. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  1898. end
  1899. else
  1900. {$endif cpu_uses_separate_address_registers}
  1901. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  1902. end;
  1903. LOC_CREGISTER,
  1904. LOC_CREFERENCE,
  1905. LOC_REFERENCE:
  1906. begin
  1907. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1908. { todo: pass actual vmt pointer type to hlcg }
  1909. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  1910. end;
  1911. else
  1912. internalerror(200305057);
  1913. end;
  1914. end;
  1915. vmtreg:=cg.getaddressregister(list);
  1916. cg.g_maybe_testself(list,href.base);
  1917. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  1918. { test validity of VMT }
  1919. if not(is_interface(objdef)) and
  1920. not(is_cppclass(objdef)) and
  1921. not(is_objc_class_or_protocol(objdef)) then
  1922. cg.g_maybe_testvmt(list,vmtreg,objdef);
  1923. end;
  1924. function getprocalign : shortint;
  1925. begin
  1926. { gprof uses 16 byte granularity }
  1927. if (cs_profile in current_settings.moduleswitches) then
  1928. result:=16
  1929. else
  1930. result:=current_settings.alignment.procalign;
  1931. end;
  1932. procedure gen_fpc_dummy(list : TAsmList);
  1933. begin
  1934. {$ifdef i386}
  1935. { fix me! }
  1936. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1937. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  1938. {$endif i386}
  1939. end;
  1940. procedure InsertInterruptTable;
  1941. procedure WriteVector(const name: string);
  1942. {$IFDEF arm}
  1943. var
  1944. ai: taicpu;
  1945. {$ENDIF arm}
  1946. begin
  1947. {$IFDEF arm}
  1948. if current_settings.cputype in [cpu_armv7m] then
  1949. current_asmdata.asmlists[al_globals].concat(tai_const.Createname(name,0))
  1950. else
  1951. begin
  1952. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(name));
  1953. ai.is_jmp:=true;
  1954. current_asmdata.asmlists[al_globals].concat(ai);
  1955. end;
  1956. {$ENDIF arm}
  1957. end;
  1958. function GetInterruptTableLength: longint;
  1959. begin
  1960. {$if defined(ARM)}
  1961. result:=embedded_controllers[current_settings.controllertype].interruptvectors;
  1962. {$else}
  1963. result:=0;
  1964. {$endif}
  1965. end;
  1966. var
  1967. hp: tused_unit;
  1968. sym: tsym;
  1969. i, i2: longint;
  1970. interruptTable: array of tprocdef;
  1971. pd: tprocdef;
  1972. begin
  1973. SetLength(interruptTable, GetInterruptTableLength);
  1974. FillChar(interruptTable[0], length(interruptTable)*sizeof(pointer), 0);
  1975. hp:=tused_unit(usedunits.first);
  1976. while assigned(hp) do
  1977. begin
  1978. for i := 0 to hp.u.symlist.Count-1 do
  1979. begin
  1980. sym:=tsym(hp.u.symlist[i]);
  1981. if not assigned(sym) then
  1982. continue;
  1983. if sym.typ = procsym then
  1984. begin
  1985. for i2 := 0 to tprocsym(sym).ProcdefList.Count-1 do
  1986. begin
  1987. pd:=tprocdef(tprocsym(sym).ProcdefList[i2]);
  1988. if pd.interruptvector >= 0 then
  1989. begin
  1990. if pd.interruptvector > high(interruptTable) then
  1991. Internalerror(2011030602);
  1992. if interruptTable[pd.interruptvector] <> nil then
  1993. internalerror(2011030601);
  1994. interruptTable[pd.interruptvector]:=pd;
  1995. break;
  1996. end;
  1997. end;
  1998. end;
  1999. end;
  2000. hp:=tused_unit(hp.next);
  2001. end;
  2002. new_section(current_asmdata.asmlists[al_globals],sec_init,'VECTORS',sizeof(pint));
  2003. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('VECTORS',AT_DATA,0));
  2004. {$IFDEF arm}
  2005. if current_settings.cputype in [cpu_armv7m] then
  2006. current_asmdata.asmlists[al_globals].concat(tai_const.Createname('_stack_top',0)); { ARMv7-M processors have the initial stack value at address 0 }
  2007. {$ENDIF arm}
  2008. for i:=0 to high(interruptTable) do
  2009. begin
  2010. if interruptTable[i]<>nil then
  2011. writeVector(interruptTable[i].mangledname)
  2012. else
  2013. writeVector('DefaultHandler'); { Default handler name }
  2014. end;
  2015. end;
  2016. end.