ncgutil.pas 126 KB

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