cgai386.pas 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgai386;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cpubase,cpuasm,
  23. symconst,symtype,symdef,aasm;
  24. {$define TESTGETTEMP to store const that
  25. are written into temps for later release PM }
  26. function def_opsize(p1:tdef):topsize;
  27. function def2def_opsize(p1,p2:tdef):topsize;
  28. function def_getreg(p1:tdef):tregister;
  29. function makereg8(r:tregister):tregister;
  30. function makereg16(r:tregister):tregister;
  31. function makereg32(r:tregister):tregister;
  32. procedure locflags2reg(var l:tlocation;opsize:topsize);
  33. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  34. procedure emitlab(var l : tasmlabel);
  35. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  36. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  37. procedure emit_none(i : tasmop;s : topsize);
  38. procedure emit_const(i : tasmop;s : topsize;c : longint);
  39. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  40. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  41. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  42. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  43. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  44. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  45. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  46. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  47. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  48. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  49. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  50. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  51. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  52. procedure emitcall(const routine:string);
  53. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  54. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  55. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  56. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  57. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  58. procedure emit_push_loc(const t:tlocation);
  59. procedure emit_push_mem_size(const t: treference; size: longint);
  60. { pushes qword location to the stack }
  61. procedure emit_pushq_loc(const t : tlocation);
  62. procedure release_qword_loc(const t : tlocation);
  63. { remove non regvar registers in loc from regs (in the format }
  64. { pushusedregisters uses) }
  65. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  66. { releases the registers of a location }
  67. procedure release_loc(const t : tlocation);
  68. procedure emit_pushw_loc(const t:tlocation);
  69. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  70. procedure emit_to_mem(var t:tlocation;def:tdef);
  71. procedure emit_to_reg16(var hr:tregister);
  72. procedure emit_to_reg32(var hr:tregister);
  73. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  74. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  75. procedure copyshortstring(const dref,sref : treference;len : byte;
  76. loadref, del_sref: boolean);
  77. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  78. procedure incrstringref(t : tdef;const ref : treference);
  79. procedure decrstringref(t : tdef;const ref : treference);
  80. procedure push_int(l : longint);
  81. procedure emit_push_mem(const ref : treference);
  82. procedure emitpushreferenceaddr(const ref : treference);
  83. procedure incrcomintfref(t: tdef; const ref: treference);
  84. procedure decrcomintfref(t: tdef; const ref: treference);
  85. procedure floatload(t : tfloattype;const ref : treference);
  86. procedure floatstore(t : tfloattype;const ref : treference);
  87. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  88. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  89. procedure maybe_loadself;
  90. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;destreg:Tregister;delloc:boolean);
  91. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  92. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  93. stackframe:longint;
  94. var parasize:longint;var nostackframe:boolean;
  95. inlined : boolean);
  96. procedure genexitcode(alist : TAAsmoutput;parasize:longint;
  97. nostackframe,inlined:boolean);
  98. { if a unit doesn't have a explicit init/final code, }
  99. { we've to generate one, if the units has ansistrings }
  100. { in the interface or implementation }
  101. procedure genimplicitunitfinal(alist : TAAsmoutput);
  102. procedure genimplicitunitinit(alist : TAAsmoutput);
  103. {$ifdef test_dest_loc}
  104. const
  105. { used to avoid temporary assignments }
  106. dest_loc_known : boolean = false;
  107. in_dest_loc : boolean = false;
  108. dest_loc_tree : ptree = nil;
  109. var
  110. dest_loc : tlocation;
  111. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  112. {$endif test_dest_loc}
  113. implementation
  114. uses
  115. {$ifdef delphi}
  116. sysutils,
  117. {$else}
  118. strings,
  119. {$endif}
  120. cutils,cclasses,
  121. globtype,systems,globals,verbose,
  122. fmodule,
  123. symbase,symsym,symtable,types,
  124. tgcpu,temp_gen,hcodegen,regvars
  125. {$ifdef GDB}
  126. ,gdb
  127. {$endif}
  128. ;
  129. {$ifndef NOTARGETWIN32}
  130. const
  131. winstackpagesize = 4096;
  132. {$endif}
  133. {*****************************************************************************
  134. Helpers
  135. *****************************************************************************}
  136. function def_opsize(p1:tdef):topsize;
  137. begin
  138. case p1.size of
  139. 1 : def_opsize:=S_B;
  140. 2 : def_opsize:=S_W;
  141. 4 : def_opsize:=S_L;
  142. else
  143. internalerror(130820001);
  144. end;
  145. end;
  146. function def2def_opsize(p1,p2:tdef):topsize;
  147. var
  148. o1 : topsize;
  149. begin
  150. case p1.size of
  151. 1 : o1:=S_B;
  152. 2 : o1:=S_W;
  153. 4 : o1:=S_L;
  154. { I don't know if we need it (FK) }
  155. 8 : o1:=S_L;
  156. else
  157. internalerror(130820002);
  158. end;
  159. if assigned(p2) then
  160. begin
  161. case p2.size of
  162. 1 : o1:=S_B;
  163. 2 : begin
  164. if o1=S_B then
  165. o1:=S_BW
  166. else
  167. o1:=S_W;
  168. end;
  169. 4,8:
  170. begin
  171. case o1 of
  172. S_B : o1:=S_BL;
  173. S_W : o1:=S_WL;
  174. end;
  175. end;
  176. end;
  177. end;
  178. def2def_opsize:=o1;
  179. end;
  180. function def_getreg(p1:tdef):tregister;
  181. begin
  182. case p1.size of
  183. 1 : def_getreg:=reg32toreg8(getregister32);
  184. 2 : def_getreg:=reg32toreg16(getregister32);
  185. 4 : def_getreg:=getregister32;
  186. else
  187. internalerror(130820003);
  188. end;
  189. end;
  190. function makereg8(r:tregister):tregister;
  191. begin
  192. case r of
  193. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  194. makereg8:=reg32toreg8(r);
  195. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  196. makereg8:=reg16toreg8(r);
  197. R_AL,R_BL,R_CL,R_DL :
  198. makereg8:=r;
  199. end;
  200. end;
  201. function makereg16(r:tregister):tregister;
  202. begin
  203. case r of
  204. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  205. makereg16:=reg32toreg16(r);
  206. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  207. makereg16:=r;
  208. R_AL,R_BL,R_CL,R_DL :
  209. makereg16:=reg8toreg16(r);
  210. end;
  211. end;
  212. function makereg32(r:tregister):tregister;
  213. begin
  214. case r of
  215. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  216. makereg32:=r;
  217. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  218. makereg32:=reg16toreg32(r);
  219. R_AL,R_BL,R_CL,R_DL :
  220. makereg32:=reg8toreg32(r);
  221. end;
  222. end;
  223. procedure locflags2reg(var l:tlocation;opsize:topsize);
  224. var
  225. hregister : tregister;
  226. begin
  227. if (l.loc=LOC_FLAGS) then
  228. begin
  229. hregister:=getregister32;
  230. case opsize of
  231. S_W : hregister:=reg32toreg16(hregister);
  232. S_B : hregister:=reg32toreg8(hregister);
  233. end;
  234. emit_flag2reg(l.resflags,hregister);
  235. l.loc:=LOC_REGISTER;
  236. l.register:=hregister;
  237. end
  238. else internalerror(270720001);
  239. end;
  240. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  241. var
  242. hregister : tregister;
  243. hl : tasmlabel;
  244. begin
  245. if l.loc = LOC_JUMP then
  246. begin
  247. hregister:=getregister32;
  248. case opsize of
  249. S_W : hregister:=reg32toreg16(hregister);
  250. S_B : hregister:=reg32toreg8(hregister);
  251. end;
  252. l.loc:=LOC_REGISTER;
  253. l.register:=hregister;
  254. emitlab(truelabel);
  255. truelabel:=otl;
  256. emit_const_reg(A_MOV,opsize,1,hregister);
  257. getlabel(hl);
  258. emitjmp(C_None,hl);
  259. emitlab(falselabel);
  260. falselabel:=ofl;
  261. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  262. makereg32(hregister));
  263. emitlab(hl);
  264. end
  265. else internalerror(270720002);
  266. end;
  267. {*****************************************************************************
  268. Emit Assembler
  269. *****************************************************************************}
  270. procedure emitlab(var l : tasmlabel);
  271. begin
  272. if not l.is_set then
  273. exprasmList.concat(Tai_label.Create(l))
  274. else
  275. internalerror(7453984);
  276. end;
  277. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  278. var
  279. ai : taicpu;
  280. begin
  281. if c=C_None then
  282. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  283. else
  284. begin
  285. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  286. ai.SetCondition(c);
  287. end;
  288. ai.is_jmp:=true;
  289. exprasmList.concat(ai);
  290. end;
  291. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  292. var
  293. ai : taicpu;
  294. hreg : tregister;
  295. begin
  296. hreg:=makereg8(hregister);
  297. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  298. ai.SetCondition(flag_2_cond[flag]);
  299. exprasmList.concat(ai);
  300. if hreg<>hregister then
  301. begin
  302. if hregister in regset16bit then
  303. emit_to_reg16(hreg)
  304. else
  305. emit_to_reg32(hreg);
  306. end;
  307. end;
  308. procedure emit_none(i : tasmop;s : topsize);
  309. begin
  310. exprasmList.concat(Taicpu.Op_none(i,s));
  311. end;
  312. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  313. begin
  314. exprasmList.concat(Taicpu.Op_reg(i,s,reg));
  315. end;
  316. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  317. begin
  318. exprasmList.concat(Taicpu.Op_ref(i,s,ref));
  319. end;
  320. procedure emit_const(i : tasmop;s : topsize;c : longint);
  321. begin
  322. exprasmList.concat(Taicpu.Op_const(i,s,c));
  323. end;
  324. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  325. begin
  326. exprasmList.concat(Taicpu.Op_const_reg(i,s,c,reg));
  327. end;
  328. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  329. begin
  330. exprasmList.concat(Taicpu.Op_const_ref(i,s,c,ref));
  331. end;
  332. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  333. begin
  334. exprasmList.concat(Taicpu.Op_ref_reg(i,s,ref,reg));
  335. end;
  336. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  337. begin
  338. exprasmList.concat(Taicpu.Op_reg_ref(i,s,reg,ref));
  339. end;
  340. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  341. begin
  342. if (reg1<>reg2) or (i<>A_MOV) then
  343. exprasmList.concat(Taicpu.Op_reg_reg(i,s,reg1,reg2));
  344. end;
  345. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  346. begin
  347. exprasmList.concat(Taicpu.Op_const_reg_reg(i,s,c,reg1,reg2));
  348. end;
  349. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  350. begin
  351. exprasmList.concat(Taicpu.Op_reg_reg_reg(i,s,reg1,reg2,reg3));
  352. end;
  353. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  354. begin
  355. exprasmList.concat(Taicpu.Op_sym(i,s,op));
  356. end;
  357. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  358. begin
  359. exprasmList.concat(Taicpu.Op_sym_ofs(i,s,op,ofs));
  360. end;
  361. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  362. begin
  363. exprasmList.concat(Taicpu.Op_sym_ofs_reg(i,s,op,ofs,reg));
  364. end;
  365. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  366. begin
  367. exprasmList.concat(Taicpu.Op_sym_ofs_ref(i,s,op,ofs,ref));
  368. end;
  369. procedure emitcall(const routine:string);
  370. begin
  371. exprasmList.concat(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  372. end;
  373. { only usefull in startup code }
  374. procedure emitinsertcall(const routine:string);
  375. begin
  376. exprasmList.insert(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  377. end;
  378. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  379. var
  380. hreg : tregister;
  381. pushedeax : boolean;
  382. begin
  383. pushedeax:=false;
  384. case t.loc of
  385. LOC_REGISTER,
  386. LOC_CREGISTER : begin
  387. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  388. t.register,newreference(ref)));
  389. ungetregister32(t.register); { the register is not needed anymore }
  390. end;
  391. LOC_MEM,
  392. LOC_REFERENCE : begin
  393. if t.reference.is_immediate then
  394. emit_const_ref(A_MOV,siz,
  395. t.reference.offset,newreference(ref))
  396. else
  397. begin
  398. case siz of
  399. S_B : begin
  400. { we can't do a getregister in the code generator }
  401. { without problems!!! }
  402. if usablereg32>0 then
  403. hreg:=reg32toreg8(getregister32)
  404. else
  405. begin
  406. emit_reg(A_PUSH,S_L,R_EAX);
  407. pushedeax:=true;
  408. hreg:=R_AL;
  409. end;
  410. end;
  411. S_W : hreg:=R_DI;
  412. S_L : hreg:=R_EDI;
  413. end;
  414. if hreg in [R_DI,R_EDI] then
  415. getexplicitregister32(R_EDI);
  416. emit_ref_reg(A_MOV,siz,
  417. newreference(t.reference),hreg);
  418. del_reference(t.reference);
  419. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  420. hreg,newreference(ref)));
  421. if siz=S_B then
  422. begin
  423. if pushedeax then
  424. emit_reg(A_POP,S_L,R_EAX)
  425. else
  426. ungetregister(hreg);
  427. end;
  428. if hreg in [R_DI,R_EDI] then
  429. ungetregister32(R_EDI);
  430. { we can release the registers }
  431. { but only AFTER the MOV! Important for the optimizer!
  432. (JM)}
  433. del_reference(ref);
  434. end;
  435. if freetemp then
  436. ungetiftemp(t.reference);
  437. end;
  438. else
  439. internalerror(330);
  440. end;
  441. end;
  442. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  443. begin
  444. case t.loc of
  445. LOC_REGISTER,
  446. LOC_CREGISTER : begin
  447. emit_reg_reg(A_MOV,S_L,t.register,reg);
  448. ungetregister32(t.register); { the register is not needed anymore }
  449. end;
  450. LOC_MEM,
  451. LOC_REFERENCE : begin
  452. if t.reference.is_immediate then
  453. emit_const_reg(A_MOV,S_L,
  454. t.reference.offset,reg)
  455. else
  456. begin
  457. emit_ref_reg(A_MOV,S_L,
  458. newreference(t.reference),reg);
  459. end;
  460. end;
  461. else
  462. internalerror(330);
  463. end;
  464. end;
  465. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  466. begin
  467. case t.loc of
  468. LOC_REGISTER,
  469. LOC_CREGISTER : begin
  470. emit_reg_reg(A_MOV,RegSize(Reg),
  471. reg,t.register);
  472. end;
  473. LOC_MEM,
  474. LOC_REFERENCE : begin
  475. if t.reference.is_immediate then
  476. internalerror(334)
  477. else
  478. begin
  479. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,RegSize(Reg),
  480. Reg,newreference(t.reference)));
  481. end;
  482. end;
  483. else
  484. internalerror(330);
  485. end;
  486. end;
  487. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  488. begin
  489. case t.loc of
  490. LOC_MEM,
  491. LOC_REFERENCE : begin
  492. if t.reference.is_immediate then
  493. internalerror(331)
  494. else
  495. begin
  496. emit_ref_reg(A_LEA,S_L,
  497. newreference(t.reference),reg);
  498. end;
  499. if freetemp then
  500. ungetiftemp(t.reference);
  501. end;
  502. else
  503. internalerror(332);
  504. end;
  505. end;
  506. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  507. begin
  508. case t.loc of
  509. LOC_REGISTER,
  510. LOC_CREGISTER : begin
  511. emit_reg_reg(A_MOV,S_L,
  512. reglow,t.registerlow);
  513. emit_reg_reg(A_MOV,S_L,
  514. reghigh,t.registerhigh);
  515. end;
  516. LOC_MEM,
  517. LOC_REFERENCE : begin
  518. if t.reference.is_immediate then
  519. internalerror(334)
  520. else
  521. begin
  522. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  523. Reglow,newreference(t.reference)));
  524. inc(t.reference.offset,4);
  525. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  526. Reghigh,newreference(t.reference)));
  527. end;
  528. end;
  529. else
  530. internalerror(330);
  531. end;
  532. end;
  533. procedure emit_pushq_loc(const t : tlocation);
  534. var
  535. hr : preference;
  536. begin
  537. case t.loc of
  538. LOC_REGISTER,
  539. LOC_CREGISTER:
  540. begin
  541. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  542. t.registerhigh));
  543. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  544. t.registerlow));
  545. end;
  546. LOC_MEM,
  547. LOC_REFERENCE:
  548. begin
  549. hr:=newreference(t.reference);
  550. inc(hr^.offset,4);
  551. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  552. hr));
  553. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  554. newreference(t.reference)));
  555. ungetiftemp(t.reference);
  556. end;
  557. else internalerror(331);
  558. end;
  559. end;
  560. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  561. begin
  562. case t.loc of
  563. LOC_REGISTER:
  564. { can't be a regvar, since it would be LOC_CREGISTER then }
  565. regs := regs and not($80 shr byte(t.register));
  566. LOC_MEM,LOC_REFERENCE:
  567. begin
  568. if not(cs_regalloc in aktglobalswitches) or
  569. (t.reference.base in usableregs) then
  570. regs := regs and
  571. not($80 shr byte(t.reference.base));
  572. if not(cs_regalloc in aktglobalswitches) or
  573. (t.reference.index in usableregs) then
  574. regs := regs and
  575. not($80 shr byte(t.reference.index));
  576. end;
  577. end;
  578. end;
  579. procedure release_loc(const t : tlocation);
  580. begin
  581. case t.loc of
  582. LOC_REGISTER,
  583. LOC_CREGISTER:
  584. begin
  585. ungetregister32(t.register);
  586. end;
  587. LOC_MEM,
  588. LOC_REFERENCE:
  589. del_reference(t.reference);
  590. else internalerror(332);
  591. end;
  592. end;
  593. procedure release_qword_loc(const t : tlocation);
  594. begin
  595. case t.loc of
  596. LOC_REGISTER,
  597. LOC_CREGISTER:
  598. begin
  599. ungetregister32(t.registerhigh);
  600. ungetregister32(t.registerlow);
  601. end;
  602. LOC_MEM,
  603. LOC_REFERENCE:
  604. del_reference(t.reference);
  605. else internalerror(331);
  606. end;
  607. end;
  608. procedure emit_push_loc(const t:tlocation);
  609. begin
  610. case t.loc of
  611. LOC_REGISTER,
  612. LOC_CREGISTER : begin
  613. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)));
  614. ungetregister(t.register); { the register is not needed anymore }
  615. end;
  616. LOC_MEM,
  617. LOC_REFERENCE : begin
  618. if t.reference.is_immediate then
  619. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.reference.offset))
  620. else
  621. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(t.reference)));
  622. del_reference(t.reference);
  623. ungetiftemp(t.reference);
  624. end;
  625. else
  626. internalerror(330);
  627. end;
  628. end;
  629. procedure emit_pushw_loc(const t:tlocation);
  630. var
  631. opsize : topsize;
  632. begin
  633. case t.loc of
  634. LOC_REGISTER,
  635. LOC_CREGISTER : begin
  636. if aktalignment.paraalign=4 then
  637. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)))
  638. else
  639. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,makereg16(t.register)));
  640. ungetregister(t.register); { the register is not needed anymore }
  641. end;
  642. LOC_MEM,
  643. LOC_REFERENCE : begin
  644. if aktalignment.paraalign=4 then
  645. opsize:=S_L
  646. else
  647. opsize:=S_W;
  648. if t.reference.is_immediate then
  649. exprasmList.concat(Taicpu.Op_const(A_PUSH,opsize,t.reference.offset))
  650. else
  651. exprasmList.concat(Taicpu.Op_ref(A_PUSH,opsize,newreference(t.reference)));
  652. del_reference(t.reference);
  653. ungetiftemp(t.reference);
  654. end;
  655. else
  656. internalerror(330);
  657. end;
  658. end;
  659. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  660. begin
  661. case t.loc of
  662. LOC_MEM,
  663. LOC_REFERENCE : begin
  664. if t.reference.is_immediate then
  665. internalerror(331)
  666. else
  667. begin
  668. getexplicitregister32(R_EDI);
  669. emit_ref_reg(A_LEA,S_L,
  670. newreference(t.reference),R_EDI);
  671. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  672. R_EDI,newreference(ref)));
  673. ungetregister32(R_EDI);
  674. end;
  675. { release the registers }
  676. del_reference(t.reference);
  677. if freetemp then
  678. ungetiftemp(t.reference);
  679. end;
  680. else
  681. internalerror(332);
  682. end;
  683. end;
  684. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  685. begin
  686. case t.loc of
  687. LOC_MEM,
  688. LOC_REFERENCE : begin
  689. if t.reference.is_immediate then
  690. internalerror(331)
  691. else
  692. begin
  693. getexplicitregister32(R_EDI);
  694. emit_ref_reg(A_LEA,S_L,
  695. newreference(t.reference),R_EDI);
  696. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  697. ungetregister32(R_EDI);
  698. end;
  699. if freetemp then
  700. ungetiftemp(t.reference);
  701. end;
  702. else
  703. internalerror(332);
  704. end;
  705. end;
  706. procedure emit_push_mem_size(const t: treference; size: longint);
  707. var
  708. s: topsize;
  709. begin
  710. if t.is_immediate then
  711. begin
  712. if (size=4) or
  713. (aktalignment.paraalign=4) then
  714. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.offset))
  715. else
  716. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_W,t.offset));
  717. end
  718. else
  719. if size < 4 then
  720. begin
  721. getexplicitregister32(R_EDI);
  722. case size of
  723. 1: s := S_BL;
  724. 2: s := S_WL;
  725. else internalerror(200008071);
  726. end;
  727. exprasmList.concat(Taicpu.Op_ref_reg(A_MOVZX,s,
  728. newreference(t),R_EDI));
  729. if aktalignment.paraalign=4 then
  730. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI))
  731. else
  732. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,R_DI));
  733. ungetregister32(R_EDI);
  734. end
  735. else
  736. if size = 4 then
  737. emit_push_mem(t)
  738. else
  739. internalerror(200008072);
  740. end;
  741. procedure emit_to_mem(var t:tlocation;def:tdef);
  742. var
  743. r : treference;
  744. begin
  745. case t.loc of
  746. LOC_FPU : begin
  747. reset_reference(t.reference);
  748. gettempofsizereference(10,t.reference);
  749. floatstore(tfloatdef(def).typ,t.reference);
  750. end;
  751. LOC_REGISTER:
  752. begin
  753. if is_64bitint(def) then
  754. begin
  755. gettempofsizereference(8,r);
  756. emit_reg_ref(A_MOV,S_L,t.registerlow,newreference(r));
  757. inc(r.offset,4);
  758. emit_reg_ref(A_MOV,S_L,t.registerhigh,newreference(r));
  759. dec(r.offset,4);
  760. t.reference:=r;
  761. end
  762. else
  763. internalerror(1405001);
  764. end;
  765. LOC_MEM,
  766. LOC_REFERENCE : ;
  767. LOC_CFPUREGISTER : begin
  768. emit_reg(A_FLD,S_NO,correct_fpuregister(t.register,fpuvaroffset));
  769. inc(fpuvaroffset);
  770. reset_reference(t.reference);
  771. gettempofsizereference(10,t.reference);
  772. floatstore(tfloatdef(def).typ,t.reference);
  773. end;
  774. else
  775. internalerror(333);
  776. end;
  777. t.loc:=LOC_MEM;
  778. end;
  779. procedure emit_to_reg16(var hr:tregister);
  780. begin
  781. { ranges are a little bit bug sensitive ! }
  782. case hr of
  783. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  784. begin
  785. hr:=reg32toreg16(hr);
  786. end;
  787. R_AL,R_BL,R_CL,R_DL:
  788. begin
  789. hr:=reg8toreg16(hr);
  790. emit_const_reg(A_AND,S_W,$ff,hr);
  791. end;
  792. R_AH,R_BH,R_CH,R_DH:
  793. begin
  794. hr:=reg8toreg16(hr);
  795. emit_const_reg(A_AND,S_W,$ff00,hr);
  796. end;
  797. end;
  798. end;
  799. procedure emit_to_reg32(var hr:tregister);
  800. begin
  801. { ranges are a little bit bug sensitive ! }
  802. case hr of
  803. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  804. begin
  805. hr:=reg16toreg32(hr);
  806. emit_const_reg(A_AND,S_L,$ffff,hr);
  807. end;
  808. R_AL,R_BL,R_CL,R_DL:
  809. begin
  810. hr:=reg8toreg32(hr);
  811. emit_const_reg(A_AND,S_L,$ff,hr);
  812. end;
  813. R_AH,R_BH,R_CH,R_DH:
  814. begin
  815. hr:=reg8toreg32(hr);
  816. emit_const_reg(A_AND,S_L,$ff00,hr);
  817. end;
  818. end;
  819. end;
  820. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  821. var
  822. hr : preference;
  823. begin
  824. { if we load a 64 bit reference, we must be careful because }
  825. { we could overwrite the registers of the reference by }
  826. { accident }
  827. getexplicitregister32(R_EDI);
  828. if r.base=rl then
  829. begin
  830. emit_reg_reg(A_MOV,S_L,r.base,
  831. R_EDI);
  832. r.base:=R_EDI;
  833. end
  834. else if r.index=rl then
  835. begin
  836. emit_reg_reg(A_MOV,S_L,r.index,
  837. R_EDI);
  838. r.index:=R_EDI;
  839. end;
  840. emit_ref_reg(A_MOV,S_L,
  841. newreference(r),rl);
  842. hr:=newreference(r);
  843. inc(hr^.offset,4);
  844. emit_ref_reg(A_MOV,S_L,
  845. hr,rh);
  846. ungetregister32(R_EDI);
  847. end;
  848. {*****************************************************************************
  849. Emit String Functions
  850. *****************************************************************************}
  851. procedure incrcomintfref(t: tdef; const ref: treference);
  852. var
  853. pushedregs : tpushed;
  854. begin
  855. pushusedregisters(pushedregs,$ff);
  856. emit_ref(A_PUSH,S_L,newreference(ref));
  857. saveregvars($ff);
  858. if is_interfacecom(t) then
  859. emitcall('FPC_INTF_INCR_REF')
  860. else
  861. internalerror(1859);
  862. popusedregisters(pushedregs);
  863. end;
  864. procedure decrcomintfref(t: tdef; const ref: treference);
  865. var
  866. pushedregs : tpushed;
  867. begin
  868. pushusedregisters(pushedregs,$ff);
  869. emitpushreferenceaddr(ref);
  870. saveregvars($ff);
  871. if is_interfacecom(t) then
  872. begin
  873. emitcall('FPC_INTF_DECR_REF');
  874. end
  875. else internalerror(1859);
  876. popusedregisters(pushedregs);
  877. end;
  878. procedure copyshortstring(const dref,sref : treference;len : byte;
  879. loadref, del_sref: boolean);
  880. begin
  881. emitpushreferenceaddr(dref);
  882. { if it's deleted right before it's used, the optimizer can move }
  883. { the reg deallocations to the right places (JM) }
  884. if del_sref then
  885. del_reference(sref);
  886. if loadref then
  887. emit_push_mem(sref)
  888. else
  889. emitpushreferenceaddr(sref);
  890. push_int(len);
  891. emitcall('FPC_SHORTSTR_COPY');
  892. maybe_loadself;
  893. end;
  894. {$ifdef unused}
  895. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  896. begin
  897. emitpushreferenceaddr(dref);
  898. if loadref then
  899. emit_push_mem(sref)
  900. else
  901. emitpushreferenceaddr(sref);
  902. push_int(len);
  903. saveregvars($ff);
  904. emitcall('FPC_LONGSTR_COPY');
  905. maybe_loadself;
  906. end;
  907. {$endif unused}
  908. procedure incrstringref(t : tdef;const ref : treference);
  909. var
  910. pushedregs : tpushed;
  911. begin
  912. pushusedregisters(pushedregs,$ff);
  913. emitpushreferenceaddr(ref);
  914. saveregvars($ff);
  915. if is_ansistring(t) then
  916. begin
  917. emitcall('FPC_ANSISTR_INCR_REF');
  918. end
  919. else if is_widestring(t) then
  920. begin
  921. emitcall('FPC_WIDESTR_INCR_REF');
  922. end
  923. else internalerror(1859);
  924. popusedregisters(pushedregs);
  925. end;
  926. procedure decrstringref(t : tdef;const ref : treference);
  927. var
  928. pushedregs : tpushed;
  929. begin
  930. pushusedregisters(pushedregs,$ff);
  931. emitpushreferenceaddr(ref);
  932. saveregvars($ff);
  933. if is_ansistring(t) then
  934. begin
  935. emitcall('FPC_ANSISTR_DECR_REF');
  936. end
  937. else if is_widestring(t) then
  938. begin
  939. emitcall('FPC_WIDESTR_DECR_REF');
  940. end
  941. else internalerror(1859);
  942. popusedregisters(pushedregs);
  943. end;
  944. {*****************************************************************************
  945. Emit Push Functions
  946. *****************************************************************************}
  947. procedure push_int(l : longint);
  948. begin
  949. if (l = 0) and
  950. not(aktoptprocessor in [Class386, ClassP6]) and
  951. not(cs_littlesize in aktglobalswitches)
  952. Then
  953. begin
  954. getexplicitregister32(R_EDI);
  955. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  956. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  957. ungetregister32(R_EDI);
  958. end
  959. else
  960. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,l));
  961. end;
  962. procedure emit_push_mem(const ref : treference);
  963. begin
  964. if ref.is_immediate then
  965. push_int(ref.offset)
  966. else
  967. begin
  968. if not(aktoptprocessor in [Class386, ClassP6]) and
  969. not(cs_littlesize in aktglobalswitches)
  970. then
  971. begin
  972. getexplicitregister32(R_EDI);
  973. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  974. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  975. ungetregister32(R_EDI);
  976. end
  977. else exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)));
  978. end;
  979. end;
  980. procedure emitpushreferenceaddr(const ref : treference);
  981. var
  982. href : treference;
  983. begin
  984. { this will fail for references to other segments !!! }
  985. if ref.is_immediate then
  986. { is this right ? }
  987. begin
  988. { push_int(ref.offset)}
  989. gettempofsizereference(4,href);
  990. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  991. emitpushreferenceaddr(href);
  992. del_reference(href);
  993. end
  994. else
  995. begin
  996. if ref.segment<>R_NO then
  997. CGMessage(cg_e_cant_use_far_pointer_there);
  998. if (ref.base=R_NO) and (ref.index=R_NO) then
  999. exprasmList.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset))
  1000. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  1001. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  1002. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.index))
  1003. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  1004. (ref.offset=0) and (ref.symbol=nil) then
  1005. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.base))
  1006. else
  1007. begin
  1008. getexplicitregister32(R_EDI);
  1009. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1010. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1011. ungetregister32(R_EDI);
  1012. end;
  1013. end;
  1014. end;
  1015. {*****************************************************************************
  1016. Emit Float Functions
  1017. *****************************************************************************}
  1018. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1019. begin
  1020. case t of
  1021. s32real : begin
  1022. op:=A_FLD;
  1023. s:=S_FS;
  1024. end;
  1025. s64real : begin
  1026. op:=A_FLD;
  1027. { ???? }
  1028. s:=S_FL;
  1029. end;
  1030. s80real : begin
  1031. op:=A_FLD;
  1032. s:=S_FX;
  1033. end;
  1034. s64comp : begin
  1035. op:=A_FILD;
  1036. s:=S_IQ;
  1037. end;
  1038. else internalerror(17);
  1039. end;
  1040. end;
  1041. procedure floatload(t : tfloattype;const ref : treference);
  1042. var
  1043. op : tasmop;
  1044. s : topsize;
  1045. begin
  1046. floatloadops(t,op,s);
  1047. exprasmList.concat(Taicpu.Op_ref(op,s,
  1048. newreference(ref)));
  1049. inc(fpuvaroffset);
  1050. end;
  1051. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1052. begin
  1053. case t of
  1054. s32real : begin
  1055. op:=A_FSTP;
  1056. s:=S_FS;
  1057. end;
  1058. s64real : begin
  1059. op:=A_FSTP;
  1060. s:=S_FL;
  1061. end;
  1062. s80real : begin
  1063. op:=A_FSTP;
  1064. s:=S_FX;
  1065. end;
  1066. s64comp : begin
  1067. op:=A_FISTP;
  1068. s:=S_IQ;
  1069. end;
  1070. else
  1071. internalerror(17);
  1072. end;
  1073. end;
  1074. procedure floatstore(t : tfloattype;const ref : treference);
  1075. var
  1076. op : tasmop;
  1077. s : topsize;
  1078. begin
  1079. floatstoreops(t,op,s);
  1080. exprasmList.concat(Taicpu.Op_ref(op,s,
  1081. newreference(ref)));
  1082. dec(fpuvaroffset);
  1083. end;
  1084. {*****************************************************************************
  1085. Emit Functions
  1086. *****************************************************************************}
  1087. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1088. const
  1089. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1090. ishr : array[0..3] of byte=(2,0,1,0);
  1091. var
  1092. ecxpushed : boolean;
  1093. helpsize : longint;
  1094. i : byte;
  1095. reg8,reg32 : tregister;
  1096. swap : boolean;
  1097. procedure maybepushecx;
  1098. begin
  1099. if not(R_ECX in unused) then
  1100. begin
  1101. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1102. ecxpushed:=true;
  1103. end
  1104. else getexplicitregister32(R_ECX);
  1105. end;
  1106. begin
  1107. {$IfNDef regallocfix}
  1108. If delsource then
  1109. del_reference(source);
  1110. {$EndIf regallocfix}
  1111. if (not loadref) and
  1112. ((size<=8) or
  1113. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1114. begin
  1115. helpsize:=size shr 2;
  1116. getexplicitregister32(R_EDI);
  1117. for i:=1 to helpsize do
  1118. begin
  1119. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1120. {$ifdef regallocfix}
  1121. If (size = 4) and delsource then
  1122. del_reference(source);
  1123. {$endif regallocfix}
  1124. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest)));
  1125. inc(source.offset,4);
  1126. inc(dest.offset,4);
  1127. dec(size,4);
  1128. end;
  1129. if size>1 then
  1130. begin
  1131. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1132. {$ifdef regallocfix}
  1133. If (size = 2) and delsource then
  1134. del_reference(source);
  1135. {$endif regallocfix}
  1136. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_W,R_DI,newreference(dest)));
  1137. inc(source.offset,2);
  1138. inc(dest.offset,2);
  1139. dec(size,2);
  1140. end;
  1141. ungetregister32(R_EDI);
  1142. if size>0 then
  1143. begin
  1144. { and now look for an 8 bit register }
  1145. swap:=false;
  1146. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1147. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1148. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1149. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1150. else
  1151. begin
  1152. swap:=true;
  1153. { we need only to check 3 registers, because }
  1154. { one is always not index or base }
  1155. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1156. begin
  1157. reg8:=R_AL;
  1158. reg32:=R_EAX;
  1159. end
  1160. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1161. begin
  1162. reg8:=R_BL;
  1163. reg32:=R_EBX;
  1164. end
  1165. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1166. begin
  1167. reg8:=R_CL;
  1168. reg32:=R_ECX;
  1169. end;
  1170. end;
  1171. if swap then
  1172. { was earlier XCHG, of course nonsense }
  1173. begin
  1174. getexplicitregister32(R_EDI);
  1175. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1176. end;
  1177. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1178. {$ifdef regallocfix}
  1179. If delsource then
  1180. del_reference(source);
  1181. {$endif regallocfix}
  1182. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_B,reg8,newreference(dest)));
  1183. if swap then
  1184. begin
  1185. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1186. ungetregister32(R_EDI);
  1187. end
  1188. else
  1189. ungetregister(reg8);
  1190. end;
  1191. end
  1192. else
  1193. begin
  1194. getexplicitregister32(R_EDI);
  1195. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1196. {$ifdef regallocfix}
  1197. {is this ok?? (JM)}
  1198. del_reference(dest);
  1199. {$endif regallocfix}
  1200. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1201. if loadref then
  1202. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1203. else
  1204. begin
  1205. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1206. {$ifdef regallocfix}
  1207. if delsource then
  1208. del_reference(source);
  1209. {$endif regallocfix}
  1210. end;
  1211. exprasmList.concat(Taicpu.Op_none(A_CLD,S_NO));
  1212. ecxpushed:=false;
  1213. if cs_littlesize in aktglobalswitches then
  1214. begin
  1215. maybepushecx;
  1216. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1217. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1218. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1219. end
  1220. else
  1221. begin
  1222. helpsize:=size shr 2;
  1223. size:=size and 3;
  1224. if helpsize>1 then
  1225. begin
  1226. maybepushecx;
  1227. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1228. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1229. end;
  1230. if helpsize>0 then
  1231. exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1232. if size>1 then
  1233. begin
  1234. dec(size,2);
  1235. exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1236. end;
  1237. if size=1 then
  1238. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1239. end;
  1240. ungetregister32(R_EDI);
  1241. exprasmList.concat(Tairegalloc.DeAlloc(R_ESI));
  1242. if ecxpushed then
  1243. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1244. else
  1245. ungetregister32(R_ECX);
  1246. { loading SELF-reference again }
  1247. maybe_loadself;
  1248. end;
  1249. if delsource then
  1250. ungetiftemp(source);
  1251. end;
  1252. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;
  1253. destreg:Tregister;delloc:boolean);
  1254. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1255. var tai:Taicpu;
  1256. r:Preference;
  1257. begin
  1258. tai := nil;
  1259. case location.loc of
  1260. LOC_REGISTER,LOC_CREGISTER:
  1261. begin
  1262. case orddef.typ of
  1263. u8bit,uchar,bool8bit:
  1264. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_BL,location.register,destreg);
  1265. s8bit:
  1266. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_BL,location.register,destreg);
  1267. u16bit,uwidechar,bool16bit:
  1268. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_WL,location.register,destreg);
  1269. s16bit:
  1270. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_WL,location.register,destreg);
  1271. u32bit,bool32bit,s32bit:
  1272. if location.register <> destreg then
  1273. tai:=Taicpu.Op_reg_reg(A_MOV,S_L,location.register,destreg);
  1274. else
  1275. internalerror(330);
  1276. end;
  1277. if delloc then
  1278. ungetregister(location.register);
  1279. end;
  1280. LOC_MEM,
  1281. LOC_REFERENCE:
  1282. begin
  1283. if location.reference.is_immediate then
  1284. tai:=Taicpu.Op_const_reg(A_MOV,S_L,location.reference.offset,destreg)
  1285. else
  1286. begin
  1287. r:=newreference(location.reference);
  1288. case orddef.typ of
  1289. u8bit,uchar,bool8bit:
  1290. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_BL,r,destreg);
  1291. s8bit:
  1292. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_BL,r,destreg);
  1293. u16bit,uwidechar,bool16bit:
  1294. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_WL,r,destreg);
  1295. s16bit:
  1296. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_WL,r,destreg);
  1297. u32bit,bool32bit:
  1298. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1299. s32bit:
  1300. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1301. else
  1302. internalerror(330);
  1303. end;
  1304. end;
  1305. if delloc then
  1306. del_reference(location.reference);
  1307. end
  1308. else
  1309. internalerror(6);
  1310. end;
  1311. if assigned(tai) then
  1312. exprasmList.concat(tai);
  1313. end;
  1314. { if necessary ESI is reloaded after a call}
  1315. procedure maybe_loadself;
  1316. var
  1317. hp : preference;
  1318. p : pprocinfo;
  1319. i : longint;
  1320. begin
  1321. if assigned(procinfo^._class) then
  1322. begin
  1323. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1324. if lexlevel>normal_function_level then
  1325. begin
  1326. new(hp);
  1327. reset_reference(hp^);
  1328. hp^.offset:=procinfo^.framepointer_offset;
  1329. hp^.base:=procinfo^.framepointer;
  1330. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1331. p:=procinfo^.parent;
  1332. for i:=3 to lexlevel-1 do
  1333. begin
  1334. new(hp);
  1335. reset_reference(hp^);
  1336. hp^.offset:=p^.framepointer_offset;
  1337. hp^.base:=R_ESI;
  1338. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1339. p:=p^.parent;
  1340. end;
  1341. new(hp);
  1342. reset_reference(hp^);
  1343. hp^.offset:=p^.selfpointer_offset;
  1344. hp^.base:=R_ESI;
  1345. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1346. end
  1347. else
  1348. begin
  1349. new(hp);
  1350. reset_reference(hp^);
  1351. hp^.offset:=procinfo^.selfpointer_offset;
  1352. hp^.base:=procinfo^.framepointer;
  1353. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1354. end;
  1355. end;
  1356. end;
  1357. {*****************************************************************************
  1358. Entry/Exit Code Functions
  1359. *****************************************************************************}
  1360. procedure genprofilecode;
  1361. var
  1362. pl : tasmlabel;
  1363. begin
  1364. if (po_assembler in aktprocsym.definition.procoptions) then
  1365. exit;
  1366. case target_info.target of
  1367. target_i386_win32,
  1368. target_i386_freebsd,
  1369. target_i386_linux:
  1370. begin
  1371. getaddrlabel(pl);
  1372. emitinsertcall(target_info.Cprefix+'mcount');
  1373. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1374. exprasmList.insert(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1375. exprasmList.insert(Tai_section.Create(sec_code));
  1376. exprasmList.insert(Tai_const.Create_32bit(0));
  1377. exprasmList.insert(Tai_label.Create(pl));
  1378. exprasmList.insert(Tai_align.Create(4));
  1379. exprasmList.insert(Tai_section.Create(sec_data));
  1380. end;
  1381. target_i386_go32v2:
  1382. begin
  1383. emitinsertcall('MCOUNT');
  1384. end;
  1385. end;
  1386. end;
  1387. procedure generate_interrupt_stackframe_entry;
  1388. begin
  1389. { save the registers of an interrupt procedure }
  1390. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1391. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1392. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1393. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1394. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1395. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1396. { .... also the segment registers }
  1397. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1398. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1399. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1400. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1401. end;
  1402. procedure generate_interrupt_stackframe_exit;
  1403. begin
  1404. { restore the registers of an interrupt procedure }
  1405. { this was all with entrycode instead of exitcode !!}
  1406. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1407. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1408. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1409. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1410. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1411. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1412. { .... also the segment registers }
  1413. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1414. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1415. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1416. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1417. { this restores the flags }
  1418. procinfo^.aktexitcode.concat(Taicpu.Op_none(A_IRET,S_NO));
  1419. end;
  1420. { generates the code for threadvar initialisation }
  1421. procedure initialize_threadvar(p : tnamedindexitem);
  1422. var
  1423. hr : treference;
  1424. begin
  1425. if (tsym(p).typ=varsym) and
  1426. (vo_is_thread_var in tvarsym(p).varoptions) then
  1427. begin
  1428. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,tvarsym(p).getsize));
  1429. reset_reference(hr);
  1430. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1431. emitpushreferenceaddr(hr);
  1432. saveregvars($ff);
  1433. emitcall('FPC_INIT_THREADVAR');
  1434. end;
  1435. end;
  1436. { initilizes data of type t }
  1437. { if is_already_ref is true then the routines assumes }
  1438. { that r points to the data to initialize }
  1439. procedure initialize(t : tdef;const ref : treference;is_already_ref : boolean);
  1440. var
  1441. hr : treference;
  1442. begin
  1443. if is_ansistring(t) or
  1444. is_widestring(t) or
  1445. is_interfacecom(t) then
  1446. begin
  1447. emit_const_ref(A_MOV,S_L,0,
  1448. newreference(ref));
  1449. end
  1450. else
  1451. begin
  1452. reset_reference(hr);
  1453. hr.symbol:=tstoreddef(t).get_inittable_label;
  1454. emitpushreferenceaddr(hr);
  1455. if is_already_ref then
  1456. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1457. newreference(ref)))
  1458. else
  1459. emitpushreferenceaddr(ref);
  1460. emitcall('FPC_INITIALIZE');
  1461. end;
  1462. end;
  1463. { finalizes data of type t }
  1464. { if is_already_ref is true then the routines assumes }
  1465. { that r points to the data to finalizes }
  1466. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  1467. var
  1468. r : treference;
  1469. begin
  1470. if is_ansistring(t) or
  1471. is_widestring(t) then
  1472. begin
  1473. decrstringref(t,ref);
  1474. end
  1475. else if is_interfacecom(t) then
  1476. begin
  1477. decrcomintfref(t,ref);
  1478. end
  1479. else
  1480. begin
  1481. reset_reference(r);
  1482. r.symbol:=tstoreddef(t).get_inittable_label;
  1483. emitpushreferenceaddr(r);
  1484. if is_already_ref then
  1485. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1486. newreference(ref)))
  1487. else
  1488. emitpushreferenceaddr(ref);
  1489. emitcall('FPC_FINALIZE');
  1490. end;
  1491. end;
  1492. { generates the code for initialisation of local data }
  1493. procedure initialize_data(p : tnamedindexitem);
  1494. var
  1495. hr : treference;
  1496. begin
  1497. if (tsym(p).typ=varsym) and
  1498. assigned(tvarsym(p).vartype.def) and
  1499. not(is_class(tvarsym(p).vartype.def)) and
  1500. tvarsym(p).vartype.def.needs_inittable then
  1501. begin
  1502. if assigned(procinfo) then
  1503. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1504. reset_reference(hr);
  1505. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  1506. begin
  1507. hr.base:=procinfo^.framepointer;
  1508. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1509. end
  1510. else
  1511. begin
  1512. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1513. end;
  1514. initialize(tvarsym(p).vartype.def,hr,false);
  1515. end;
  1516. end;
  1517. { generates the code for incrementing the reference count of parameters and
  1518. initialize out parameters }
  1519. procedure init_paras(p : tnamedindexitem);
  1520. var
  1521. hrv : treference;
  1522. hr: treference;
  1523. begin
  1524. if (tsym(p).typ=varsym) and
  1525. not is_class(tvarsym(p).vartype.def) and
  1526. tvarsym(p).vartype.def.needs_inittable then
  1527. begin
  1528. if (tvarsym(p).varspez=vs_value) then
  1529. begin
  1530. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1531. reset_reference(hrv);
  1532. hrv.base:=procinfo^.framepointer;
  1533. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1534. if is_ansistring(tvarsym(p).vartype.def) or
  1535. is_widestring(tvarsym(p).vartype.def) then
  1536. begin
  1537. incrstringref(tvarsym(p).vartype.def,hrv)
  1538. end
  1539. else if is_interfacecom(tvarsym(p).vartype.def) then
  1540. begin
  1541. incrcomintfref(tvarsym(p).vartype.def,hrv)
  1542. end
  1543. else
  1544. begin
  1545. reset_reference(hr);
  1546. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_inittable_label;
  1547. emitpushreferenceaddr(hr);
  1548. emitpushreferenceaddr(hrv);
  1549. emitcall('FPC_ADDREF');
  1550. end;
  1551. end
  1552. else if (tvarsym(p).varspez=vs_out) then
  1553. begin
  1554. reset_reference(hrv);
  1555. hrv.base:=procinfo^.framepointer;
  1556. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1557. getexplicitregister32(R_EDI);
  1558. exprasmList.concat(Taicpu.Op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI));
  1559. reset_reference(hr);
  1560. hr.base:=R_EDI;
  1561. initialize(tvarsym(p).vartype.def,hr,false);
  1562. end;
  1563. end;
  1564. end;
  1565. { generates the code for decrementing the reference count of parameters }
  1566. procedure final_paras(p : tnamedindexitem);
  1567. var
  1568. hrv : treference;
  1569. hr: treference;
  1570. begin
  1571. if (tsym(p).typ=varsym) and
  1572. not is_class(tvarsym(p).vartype.def) and
  1573. tvarsym(p).vartype.def.needs_inittable then
  1574. begin
  1575. if (tvarsym(p).varspez=vs_value) then
  1576. begin
  1577. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1578. reset_reference(hrv);
  1579. hrv.base:=procinfo^.framepointer;
  1580. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1581. if is_ansistring(tvarsym(p).vartype.def) or
  1582. is_widestring(tvarsym(p).vartype.def) then
  1583. begin
  1584. decrstringref(tvarsym(p).vartype.def,hrv)
  1585. end
  1586. else if is_interfacecom(tvarsym(p).vartype.def) then
  1587. begin
  1588. decrcomintfref(tvarsym(p).vartype.def,hrv)
  1589. end
  1590. else
  1591. begin
  1592. reset_reference(hr);
  1593. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_inittable_label;
  1594. emitpushreferenceaddr(hr);
  1595. emitpushreferenceaddr(hrv);
  1596. emitcall('FPC_DECREF');
  1597. end;
  1598. end;
  1599. end;
  1600. end;
  1601. { generates the code for finalisation of local data }
  1602. procedure finalize_data(p : tnamedindexitem);
  1603. var
  1604. hr : treference;
  1605. begin
  1606. if (tsym(p).typ=varsym) and
  1607. assigned(tvarsym(p).vartype.def) and
  1608. not(is_class(tvarsym(p).vartype.def)) and
  1609. tvarsym(p).vartype.def.needs_inittable then
  1610. begin
  1611. if assigned(procinfo) then
  1612. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1613. reset_reference(hr);
  1614. case tsym(p).owner.symtabletype of
  1615. localsymtable,inlinelocalsymtable:
  1616. begin
  1617. hr.base:=procinfo^.framepointer;
  1618. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1619. end;
  1620. else
  1621. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1622. end;
  1623. finalize(tvarsym(p).vartype.def,hr,false);
  1624. end;
  1625. end;
  1626. { generates the code to make local copies of the value parameters }
  1627. procedure copyvalueparas(p : tnamedindexitem);
  1628. var
  1629. href1,href2 : treference;
  1630. r : preference;
  1631. power,len : longint;
  1632. opsize : topsize;
  1633. {$ifndef NOTARGETWIN32}
  1634. again,ok : tasmlabel;
  1635. {$endif}
  1636. begin
  1637. if (tsym(p).typ=varsym) and
  1638. (tvarsym(p).varspez=vs_value) and
  1639. (push_addr_param(tvarsym(p).vartype.def)) then
  1640. begin
  1641. if is_open_array(tvarsym(p).vartype.def) or
  1642. is_array_of_const(tvarsym(p).vartype.def) then
  1643. begin
  1644. { get stack space }
  1645. new(r);
  1646. reset_reference(r^);
  1647. r^.base:=procinfo^.framepointer;
  1648. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1649. getexplicitregister32(R_EDI);
  1650. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1651. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1652. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1653. begin
  1654. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1655. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1656. else
  1657. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1658. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1659. end;
  1660. {$ifndef NOTARGETWIN32}
  1661. { windows guards only a few pages for stack growing, }
  1662. { so we have to access every page first }
  1663. if target_info.target=target_i386_win32 then
  1664. begin
  1665. getlabel(again);
  1666. getlabel(ok);
  1667. emitlab(again);
  1668. exprasmList.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1669. emitjmp(C_C,ok);
  1670. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1671. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1672. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1673. emitjmp(C_None,again);
  1674. emitlab(ok);
  1675. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1676. ungetregister32(R_EDI);
  1677. { now reload EDI }
  1678. new(r);
  1679. reset_reference(r^);
  1680. r^.base:=procinfo^.framepointer;
  1681. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1682. getexplicitregister32(R_EDI);
  1683. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1684. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1685. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1686. begin
  1687. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1688. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1689. else
  1690. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1691. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1692. end;
  1693. end
  1694. else
  1695. {$endif NOTARGETWIN32}
  1696. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1697. { load destination }
  1698. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1699. { don't destroy the registers! }
  1700. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1701. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1702. { load count }
  1703. new(r);
  1704. reset_reference(r^);
  1705. r^.base:=procinfo^.framepointer;
  1706. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1707. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ECX));
  1708. { load source }
  1709. new(r);
  1710. reset_reference(r^);
  1711. r^.base:=procinfo^.framepointer;
  1712. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1713. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ESI));
  1714. { scheduled .... }
  1715. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1716. { calculate size }
  1717. len:=tarraydef(tvarsym(p).vartype.def).elesize;
  1718. opsize:=S_B;
  1719. if (len and 3)=0 then
  1720. begin
  1721. opsize:=S_L;
  1722. len:=len shr 2;
  1723. end
  1724. else
  1725. if (len and 1)=0 then
  1726. begin
  1727. opsize:=S_W;
  1728. len:=len shr 1;
  1729. end;
  1730. if ispowerof2(len, power) then
  1731. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1732. else
  1733. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1734. exprasmList.concat(Taicpu.op_none(A_REP,S_NO));
  1735. case opsize of
  1736. S_B : exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1737. S_W : exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1738. S_L : exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1739. end;
  1740. ungetregister32(R_EDI);
  1741. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1742. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1743. { patch the new address }
  1744. new(r);
  1745. reset_reference(r^);
  1746. r^.base:=procinfo^.framepointer;
  1747. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1748. exprasmList.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,r));
  1749. end
  1750. else
  1751. if is_shortstring(tvarsym(p).vartype.def) then
  1752. begin
  1753. reset_reference(href1);
  1754. href1.base:=procinfo^.framepointer;
  1755. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1756. reset_reference(href2);
  1757. href2.base:=procinfo^.framepointer;
  1758. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1759. copyshortstring(href2,href1,tstringdef(tvarsym(p).vartype.def).len,true,false);
  1760. end
  1761. else
  1762. begin
  1763. reset_reference(href1);
  1764. href1.base:=procinfo^.framepointer;
  1765. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1766. reset_reference(href2);
  1767. href2.base:=procinfo^.framepointer;
  1768. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1769. concatcopy(href1,href2,tvarsym(p).vartype.def.size,true,true);
  1770. end;
  1771. end;
  1772. end;
  1773. procedure inittempvariables;
  1774. var
  1775. hp : ptemprecord;
  1776. r : preference;
  1777. begin
  1778. hp:=templist;
  1779. while assigned(hp) do
  1780. begin
  1781. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  1782. tt_widestring,tt_freewidestring,
  1783. tt_interfacecom] then
  1784. begin
  1785. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1786. new(r);
  1787. reset_reference(r^);
  1788. r^.base:=procinfo^.framepointer;
  1789. r^.offset:=hp^.pos;
  1790. emit_const_ref(A_MOV,S_L,0,r);
  1791. end;
  1792. hp:=hp^.next;
  1793. end;
  1794. end;
  1795. procedure finalizetempvariables;
  1796. var
  1797. hp : ptemprecord;
  1798. hr : treference;
  1799. begin
  1800. hp:=templist;
  1801. while assigned(hp) do
  1802. begin
  1803. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1804. begin
  1805. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1806. reset_reference(hr);
  1807. hr.base:=procinfo^.framepointer;
  1808. hr.offset:=hp^.pos;
  1809. emitpushreferenceaddr(hr);
  1810. emitcall('FPC_ANSISTR_DECR_REF');
  1811. end
  1812. else if hp^.temptype in [tt_widestring,tt_freewidestring] then
  1813. begin
  1814. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1815. reset_reference(hr);
  1816. hr.base:=procinfo^.framepointer;
  1817. hr.offset:=hp^.pos;
  1818. emitpushreferenceaddr(hr);
  1819. emitcall('FPC_WIDESTR_DECR_REF');
  1820. end
  1821. else if hp^.temptype=tt_interfacecom then
  1822. begin
  1823. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1824. reset_reference(hr);
  1825. hr.base:=procinfo^.framepointer;
  1826. hr.offset:=hp^.pos;
  1827. emitpushreferenceaddr(hr);
  1828. emitcall('FPC_INTF_DECR_REF');
  1829. end;
  1830. hp:=hp^.next;
  1831. end;
  1832. end;
  1833. {$ifdef dummy}
  1834. var
  1835. ls : longint;
  1836. procedure largest_size(p : tnamedindexitem);
  1837. begin
  1838. if (tsym(p).typ=varsym) and
  1839. (tvarsym(p).getvaluesize>ls) then
  1840. ls:=tvarsym(p).getvaluesize;
  1841. end;
  1842. {$endif dummy}
  1843. procedure alignstack(alist : TAAsmoutput);
  1844. begin
  1845. {$ifdef dummy}
  1846. if (cs_optimize in aktglobalswitches) and
  1847. (aktoptprocessor in [classp5,classp6]) then
  1848. begin
  1849. ls:=0;
  1850. aktprocsym.definition.localst.foreach({$ifndef TP}@{$endif}largest_size);
  1851. if ls>=8 then
  1852. aList.insert(Taicpu.Op_const_reg(A_AND,S_L,-8,R_ESP));
  1853. end;
  1854. {$endif dummy}
  1855. end;
  1856. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  1857. stackframe:longint;
  1858. var parasize:longint;var nostackframe:boolean;
  1859. inlined : boolean);
  1860. {
  1861. Generates the entry code for a procedure
  1862. }
  1863. var
  1864. hs : string;
  1865. {$ifdef GDB}
  1866. stab_function_name : tai_stab_function_name;
  1867. {$endif GDB}
  1868. hr : preference;
  1869. p : tsymtable;
  1870. r : treference;
  1871. oldlist,
  1872. oldexprasmlist : TAAsmoutput;
  1873. again : tasmlabel;
  1874. i : longint;
  1875. tempbuf,tempaddr : treference;
  1876. begin
  1877. oldexprasmlist:=exprasmlist;
  1878. exprasmlist:=alist;
  1879. if (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  1880. begin
  1881. emitinsertcall('FPC_INITIALIZEUNITS');
  1882. { initialize profiling for win32 }
  1883. if (target_info.target=target_I386_WIN32) and
  1884. (cs_profile in aktmoduleswitches) then
  1885. emitinsertcall('__monstartup');
  1886. { add threadvars }
  1887. oldlist:=exprasmlist;
  1888. exprasmlist:=TAAsmoutput.Create;
  1889. p:=symtablestack;
  1890. while assigned(p) do
  1891. begin
  1892. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar);
  1893. p:=p.next;
  1894. end;
  1895. oldList.insertlist(exprasmlist);
  1896. exprasmlist.free;
  1897. exprasmlist:=oldlist;
  1898. end;
  1899. {$ifdef GDB}
  1900. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1901. exprasmList.insert(Tai_force_line.Create);
  1902. {$endif GDB}
  1903. { a constructor needs a help procedure }
  1904. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  1905. begin
  1906. if is_class(procinfo^._class) then
  1907. begin
  1908. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1909. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1910. emitinsertcall('FPC_NEW_CLASS');
  1911. end
  1912. else if is_object(procinfo^._class) then
  1913. begin
  1914. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1915. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1916. getexplicitregister32(R_EDI);
  1917. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  1918. end
  1919. else
  1920. Internalerror(200006161);
  1921. end;
  1922. { don't load ESI, does the caller }
  1923. { we must do it for local function }
  1924. { that can be called from a foreach_static }
  1925. { of another object than self !! PM }
  1926. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1927. (lexlevel>normal_function_level) then
  1928. maybe_loadself;
  1929. { When message method contains self as a parameter,
  1930. we must load it into ESI }
  1931. If (po_containsself in aktprocsym.definition.procoptions) then
  1932. begin
  1933. new(hr);
  1934. reset_reference(hr^);
  1935. hr^.offset:=procinfo^.selfpointer_offset;
  1936. hr^.base:=procinfo^.framepointer;
  1937. exprasmList.insert(Taicpu.Op_ref_reg(A_MOV,S_L,hr,R_ESI));
  1938. exprasmList.insert(Tairegalloc.Alloc(R_ESI));
  1939. end;
  1940. { should we save edi,esi,ebx like C ? }
  1941. if (po_savestdregs in aktprocsym.definition.procoptions) then
  1942. begin
  1943. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1944. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1945. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1946. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1947. end;
  1948. { for the save all registers we can simply use a pusha,popa which
  1949. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1950. if (po_saveregisters in aktprocsym.definition.procoptions) then
  1951. begin
  1952. exprasmList.insert(Taicpu.Op_none(A_PUSHA,S_L));
  1953. end;
  1954. { omit stack frame ? }
  1955. if (not inlined) then
  1956. if (procinfo^.framepointer=stack_pointer) then
  1957. begin
  1958. CGMessage(cg_d_stackframe_omited);
  1959. nostackframe:=true;
  1960. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1961. parasize:=0
  1962. else
  1963. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-4;
  1964. if stackframe<>0 then
  1965. exprasmList.insert(Taicpu.op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  1966. end
  1967. else
  1968. begin
  1969. alignstack(alist);
  1970. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1971. parasize:=0
  1972. else
  1973. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-8;
  1974. nostackframe:=false;
  1975. if stackframe<>0 then
  1976. begin
  1977. {$ifndef NOTARGETWIN32}
  1978. { windows guards only a few pages for stack growing, }
  1979. { so we have to access every page first }
  1980. if (target_info.target=target_i386_win32) and
  1981. (stackframe>=winstackpagesize) then
  1982. begin
  1983. if stackframe div winstackpagesize<=5 then
  1984. begin
  1985. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe-4,R_ESP));
  1986. for i:=1 to stackframe div winstackpagesize do
  1987. begin
  1988. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  1989. exprasmList.concat(Taicpu.op_const_ref(A_MOV,S_L,0,hr));
  1990. end;
  1991. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1992. end
  1993. else
  1994. begin
  1995. getlabel(again);
  1996. getexplicitregister32(R_EDI);
  1997. exprasmList.concat(Taicpu.op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI));
  1998. emitlab(again);
  1999. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  2000. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2001. exprasmList.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  2002. emitjmp(C_NZ,again);
  2003. ungetregister32(R_EDI);
  2004. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP));
  2005. end
  2006. end
  2007. else
  2008. {$endif NOTARGETWIN32}
  2009. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  2010. if (cs_check_stack in aktlocalswitches) and
  2011. not(target_info.target in [target_i386_freebsd,target_i386_netbsd,
  2012. target_i386_linux,target_i386_win32]) then
  2013. begin
  2014. emitinsertcall('FPC_STACKCHECK');
  2015. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  2016. end;
  2017. if cs_profile in aktmoduleswitches then
  2018. genprofilecode;
  2019. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2020. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2021. end { endif stackframe <> 0 }
  2022. else
  2023. begin
  2024. if cs_profile in aktmoduleswitches then
  2025. genprofilecode;
  2026. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2027. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2028. end;
  2029. end;
  2030. if (po_interrupt in aktprocsym.definition.procoptions) then
  2031. generate_interrupt_stackframe_entry;
  2032. { initialize return value }
  2033. if (not is_void(aktprocsym.definition.rettype.def)) and
  2034. (aktprocsym.definition.rettype.def.needs_inittable) then
  2035. begin
  2036. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2037. reset_reference(r);
  2038. r.offset:=procinfo^.return_offset;
  2039. r.base:=procinfo^.framepointer;
  2040. initialize(aktprocsym.definition.rettype.def,r,ret_in_param(aktprocsym.definition.rettype.def));
  2041. end;
  2042. { initialisize local data like ansistrings }
  2043. case aktprocsym.definition.proctypeoption of
  2044. potype_unitinit:
  2045. begin
  2046. { using current_module.globalsymtable is hopefully }
  2047. { more robust than symtablestack and symtablestack.next }
  2048. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2049. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2050. end;
  2051. { units have seperate code for initilization and finalization }
  2052. potype_unitfinalize: ;
  2053. else
  2054. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}initialize_data);
  2055. end;
  2056. { initialisizes temp. ansi/wide string data }
  2057. inittempvariables;
  2058. { generate copies of call by value parameters }
  2059. if not(po_assembler in aktprocsym.definition.procoptions) and
  2060. (([pocall_cdecl,pocall_cppdecl]*aktprocsym.definition.proccalloptions)=[]) then
  2061. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas);
  2062. if assigned( aktprocsym.definition.parast) then
  2063. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}init_paras);
  2064. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2065. if not inlined and
  2066. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2067. { but it's useless in init/final code of units }
  2068. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2069. begin
  2070. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2071. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,36,R_ESP));
  2072. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  2073. reset_reference(tempaddr);
  2074. tempaddr.base:=R_EDI;
  2075. emitpushreferenceaddr(tempaddr);
  2076. reset_reference(tempbuf);
  2077. tempbuf.base:=R_EDI;
  2078. tempbuf.offset:=12;
  2079. emitpushreferenceaddr(tempbuf);
  2080. { Type of stack-frame must be pushed}
  2081. exprasmList.concat(Taicpu.op_const(A_PUSH,S_L,1));
  2082. emitcall('FPC_PUSHEXCEPTADDR');
  2083. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2084. emitcall('FPC_SETJMP');
  2085. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2086. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2087. emitjmp(C_NE,aktexitlabel);
  2088. { probably we've to reload self here }
  2089. maybe_loadself;
  2090. end;
  2091. if not inlined then
  2092. begin
  2093. if (cs_profile in aktmoduleswitches) or
  2094. (aktprocsym.definition.owner.symtabletype=globalsymtable) or
  2095. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  2096. make_global:=true;
  2097. hs:=aktprocsym.definition.aliasnames.getfirst;
  2098. {$ifdef GDB}
  2099. if (cs_debuginfo in aktmoduleswitches) and target_info.use_function_relative_addresses then
  2100. stab_function_name := Tai_stab_function_name.Create(strpnew(hs));
  2101. {$EndIf GDB}
  2102. while hs<>'' do
  2103. begin
  2104. if make_global then
  2105. exprasmList.insert(Tai_symbol.Createname_global(hs,0))
  2106. else
  2107. exprasmList.insert(Tai_symbol.Createname(hs,0));
  2108. {$ifdef GDB}
  2109. if (cs_debuginfo in aktmoduleswitches) and
  2110. target_info.use_function_relative_addresses then
  2111. exprasmList.insert(Tai_stab_function_name.Create(strpnew(hs)));
  2112. {$endif GDB}
  2113. hs:=aktprocsym.definition.aliasnames.getfirst;
  2114. end;
  2115. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2116. aktprocsym.is_global := True;
  2117. {$ifdef GDB}
  2118. if (cs_debuginfo in aktmoduleswitches) then
  2119. begin
  2120. if target_info.use_function_relative_addresses then
  2121. exprasmList.insert(stab_function_name);
  2122. exprasmList.insert(Tai_stabs.Create(aktprocsym.stabstring));
  2123. aktprocsym.isstabwritten:=true;
  2124. end;
  2125. {$endif GDB}
  2126. { Align, gprof uses 16 byte granularity }
  2127. if (cs_profile in aktmoduleswitches) then
  2128. exprasmList.insert(Tai_align.Create_op(16,$90))
  2129. else
  2130. exprasmList.insert(Tai_align.Create(aktalignment.procalign));
  2131. end;
  2132. if inlined then
  2133. load_regvars(exprasmlist,nil);
  2134. exprasmlist:=oldexprasmlist;
  2135. end;
  2136. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2137. var
  2138. hr : preference;
  2139. op : Tasmop;
  2140. s : Topsize;
  2141. begin
  2142. if not is_void(aktprocsym.definition.rettype.def) then
  2143. begin
  2144. {if ((procinfo^.flags and pi_operator)<>0) and
  2145. assigned(otsym) then
  2146. procinfo^.funcret_is_valid:=
  2147. procinfo^.funcret_is_valid or (otsym.refs>0);}
  2148. if (tfuncretsym(aktprocsym.definition.funcretsym).funcretstate<>vs_assigned) and not inlined { and
  2149. ((procinfo^.flags and pi_uses_asm)=0)} then
  2150. CGMessage(sym_w_function_result_not_set);
  2151. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2152. if (aktprocsym.definition.rettype.def.deftype in [orddef,enumdef]) then
  2153. begin
  2154. uses_eax:=true;
  2155. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2156. case aktprocsym.definition.rettype.def.size of
  2157. 8:
  2158. begin
  2159. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2160. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2161. exprasmList.concat(Tairegalloc.Alloc(R_EDX));
  2162. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2163. uses_edx:=true;
  2164. end;
  2165. 4:
  2166. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2167. 2:
  2168. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2169. 1:
  2170. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2171. end;
  2172. end
  2173. else
  2174. if ret_in_acc(aktprocsym.definition.rettype.def) then
  2175. begin
  2176. uses_eax:=true;
  2177. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2178. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2179. end
  2180. else
  2181. if (aktprocsym.definition.rettype.def.deftype=floatdef) then
  2182. begin
  2183. floatloadops(tfloatdef(aktprocsym.definition.rettype.def).typ,op,s);
  2184. exprasmList.concat(Taicpu.Op_ref(op,s,hr));
  2185. end
  2186. else
  2187. dispose(hr);
  2188. end
  2189. end;
  2190. procedure handle_fast_exit_return_value;
  2191. var
  2192. hr : preference;
  2193. op : Tasmop;
  2194. s : Topsize;
  2195. begin
  2196. if not is_void(aktprocsym.definition.rettype.def) then
  2197. begin
  2198. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2199. if (aktprocsym.definition.rettype.def.deftype in [orddef,enumdef]) then
  2200. begin
  2201. case aktprocsym.definition.rettype.def.size of
  2202. 8:
  2203. begin
  2204. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2205. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2206. emit_reg_ref(A_MOV,S_L,R_EDX,hr);
  2207. end;
  2208. 4:
  2209. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2210. 2:
  2211. emit_reg_ref(A_MOV,S_W,R_AX,hr);
  2212. 1:
  2213. emit_reg_ref(A_MOV,S_B,R_AL,hr);
  2214. end;
  2215. end
  2216. else
  2217. if ret_in_acc(aktprocsym.definition.rettype.def) then
  2218. begin
  2219. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2220. end
  2221. else
  2222. if (aktprocsym.definition.rettype.def.deftype=floatdef) then
  2223. begin
  2224. floatstoreops(tfloatdef(aktprocsym.definition.rettype.def).typ,op,s);
  2225. exprasmlist.concat(taicpu.op_ref(op,s,hr));
  2226. end
  2227. else
  2228. dispose(hr);
  2229. end
  2230. end;
  2231. procedure genexitcode(alist : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  2232. var
  2233. {$ifdef GDB}
  2234. mangled_length : longint;
  2235. p : pchar;
  2236. st : string[2];
  2237. {$endif GDB}
  2238. stabsendlabel,nofinal,okexitlabel,
  2239. noreraiselabel,nodestroycall : tasmlabel;
  2240. hr : treference;
  2241. uses_eax,uses_edx,uses_esi : boolean;
  2242. oldexprasmlist : TAAsmoutput;
  2243. ai : taicpu;
  2244. pd : tprocdef;
  2245. begin
  2246. oldexprasmlist:=exprasmlist;
  2247. exprasmlist:=alist;
  2248. if aktexit2label.is_used and
  2249. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  2250. begin
  2251. exprasmlist.concat(taicpu.op_sym(A_JMP,S_NO,aktexitlabel));
  2252. exprasmlist.concat(tai_label.create(aktexit2label));
  2253. handle_fast_exit_return_value;
  2254. end;
  2255. if aktexitlabel.is_used then
  2256. exprasmList.concat(Tai_label.Create(aktexitlabel));
  2257. { call the destructor help procedure }
  2258. if (aktprocsym.definition.proctypeoption=potype_destructor) and
  2259. assigned(procinfo^._class) then
  2260. begin
  2261. if is_class(procinfo^._class) then
  2262. begin
  2263. emitinsertcall('FPC_DISPOSE_CLASS');
  2264. end
  2265. else if is_object(procinfo^._class) then
  2266. begin
  2267. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2268. getexplicitregister32(R_EDI);
  2269. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  2270. { must the object be finalized ? }
  2271. if procinfo^._class.needs_inittable then
  2272. begin
  2273. getlabel(nofinal);
  2274. exprasmList.insert(Tai_label.Create(nofinal));
  2275. emitinsertcall('FPC_FINALIZE');
  2276. ungetregister32(R_EDI);
  2277. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  2278. exprasmList.insert(Taicpu.Op_sym(A_PUSH,S_L,procinfo^._class.get_inittable_label));
  2279. ai:=Taicpu.Op_sym(A_Jcc,S_NO,nofinal);
  2280. ai.SetCondition(C_Z);
  2281. exprasmList.insert(ai);
  2282. reset_reference(hr);
  2283. hr.base:=R_EBP;
  2284. hr.offset:=8;
  2285. exprasmList.insert(Taicpu.Op_const_ref(A_CMP,S_L,0,newreference(hr)));
  2286. end;
  2287. end
  2288. else
  2289. begin
  2290. Internalerror(200006161);
  2291. end;
  2292. end;
  2293. { finalize temporary data }
  2294. finalizetempvariables;
  2295. { finalize local data like ansistrings}
  2296. case aktprocsym.definition.proctypeoption of
  2297. potype_unitfinalize:
  2298. begin
  2299. { using current_module.globalsymtable is hopefully }
  2300. { more robust than symtablestack and symtablestack.next }
  2301. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2302. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2303. end;
  2304. { units have seperate code for initialization and finalization }
  2305. potype_unitinit: ;
  2306. else
  2307. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}finalize_data);
  2308. end;
  2309. { finalize paras data }
  2310. if assigned(aktprocsym.definition.parast) then
  2311. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}final_paras);
  2312. { do we need to handle exceptions because of ansi/widestrings ? }
  2313. if not inlined and
  2314. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2315. { but it's useless in init/final code of units }
  2316. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2317. begin
  2318. { the exception helper routines modify all registers }
  2319. aktprocsym.definition.usedregisters:=$ff;
  2320. getlabel(noreraiselabel);
  2321. emitcall('FPC_POPADDRSTACK');
  2322. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2323. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  2324. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2325. ungetregister32(R_EAX);
  2326. emitjmp(C_E,noreraiselabel);
  2327. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  2328. begin
  2329. if assigned(procinfo^._class) then
  2330. begin
  2331. pd:=procinfo^._class.searchdestructor;
  2332. if assigned(pd) then
  2333. begin
  2334. getlabel(nodestroycall);
  2335. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2336. procinfo^.selfpointer_offset));
  2337. emitjmp(C_E,nodestroycall);
  2338. if is_class(procinfo^._class) then
  2339. begin
  2340. emit_const(A_PUSH,S_L,1);
  2341. emit_reg(A_PUSH,S_L,R_ESI);
  2342. end
  2343. else if is_object(procinfo^._class) then
  2344. begin
  2345. emit_reg(A_PUSH,S_L,R_ESI);
  2346. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class.vmt_mangledname));
  2347. end
  2348. else
  2349. begin
  2350. Internalerror(200006161);
  2351. end;
  2352. if (po_virtualmethod in pd.procoptions) then
  2353. begin
  2354. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2355. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class.vmtmethodoffset(pd.extnumber)));
  2356. end
  2357. else
  2358. emitcall(pd.mangledname);
  2359. { not necessary because the result is never assigned in the
  2360. case of an exception (FK)
  2361. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2362. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2363. }
  2364. emitlab(nodestroycall);
  2365. end;
  2366. end
  2367. end
  2368. else
  2369. { must be the return value finalized before reraising the exception? }
  2370. if (not is_void(aktprocsym.definition.rettype.def)) and
  2371. (aktprocsym.definition.rettype.def.needs_inittable) and
  2372. ((aktprocsym.definition.rettype.def.deftype<>objectdef) or
  2373. not is_class(aktprocsym.definition.rettype.def)) then
  2374. begin
  2375. reset_reference(hr);
  2376. hr.offset:=procinfo^.return_offset;
  2377. hr.base:=procinfo^.framepointer;
  2378. finalize(aktprocsym.definition.rettype.def,hr,ret_in_param(aktprocsym.definition.rettype.def));
  2379. end;
  2380. emitcall('FPC_RERAISE');
  2381. emitlab(noreraiselabel);
  2382. end;
  2383. { call __EXIT for main program }
  2384. if (not DLLsource) and (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  2385. begin
  2386. emitcall('FPC_DO_EXIT');
  2387. end;
  2388. { handle return value }
  2389. uses_eax:=false;
  2390. uses_edx:=false;
  2391. uses_esi:=false;
  2392. if not(po_assembler in aktprocsym.definition.procoptions) then
  2393. if (aktprocsym.definition.proctypeoption<>potype_constructor) then
  2394. handle_return_value(inlined,uses_eax,uses_edx)
  2395. else
  2396. begin
  2397. { successful constructor deletes the zero flag }
  2398. { and returns self in eax }
  2399. { eax must be set to zero if the allocation failed !!! }
  2400. getlabel(okexitlabel);
  2401. emitjmp(C_NONE,okexitlabel);
  2402. emitlab(faillabel);
  2403. if is_class(procinfo^._class) then
  2404. begin
  2405. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2406. emitcall('FPC_HELP_FAIL_CLASS');
  2407. end
  2408. else if is_object(procinfo^._class) then
  2409. begin
  2410. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2411. getexplicitregister32(R_EDI);
  2412. emit_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI);
  2413. emitcall('FPC_HELP_FAIL');
  2414. ungetregister32(R_EDI);
  2415. end
  2416. else
  2417. Internalerror(200006161);
  2418. emitlab(okexitlabel);
  2419. { for classes this is done after the call to }
  2420. { AfterConstruction }
  2421. if is_object(procinfo^._class) then
  2422. begin
  2423. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2424. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2425. uses_eax:=true;
  2426. end;
  2427. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2428. uses_esi:=true;
  2429. end;
  2430. if aktexit2label.is_used and not aktexit2label.is_set then
  2431. emitlab(aktexit2label);
  2432. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2433. begin
  2434. getlabel(stabsendlabel);
  2435. emitlab(stabsendlabel);
  2436. end;
  2437. { gives problems for long mangled names }
  2438. {List.concat(Tai_symbol.Create(aktprocsym.definition.mangledname+'_end'));}
  2439. { should we restore edi ? }
  2440. { for all i386 gcc implementations }
  2441. if (po_savestdregs in aktprocsym.definition.procoptions) then
  2442. begin
  2443. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2444. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  2445. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  2446. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  2447. { here we could reset R_EBX
  2448. but that is risky because it only works
  2449. if genexitcode is called after genentrycode
  2450. so lets skip this for the moment PM
  2451. aktprocsym.definition.usedregisters:=
  2452. aktprocsym.definition.usedregisters or not ($80 shr byte(R_EBX));
  2453. }
  2454. end;
  2455. { for the save all registers we can simply use a pusha,popa which
  2456. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2457. if (po_saveregisters in aktprocsym.definition.procoptions) then
  2458. begin
  2459. if uses_esi then
  2460. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4)));
  2461. if uses_edx then
  2462. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20)));
  2463. if uses_eax then
  2464. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28)));
  2465. exprasmList.concat(Taicpu.Op_none(A_POPA,S_L));
  2466. { We add a NOP because of the 386DX CPU bugs with POPAD }
  2467. exprasmlist.concat(taicpu.op_none(A_NOP,S_L));
  2468. end;
  2469. if not(nostackframe) then
  2470. begin
  2471. if not inlined then
  2472. exprasmList.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  2473. end
  2474. else
  2475. begin
  2476. if (gettempsize<>0) and not inlined then
  2477. exprasmList.insert(Taicpu.op_const_reg(A_ADD,S_L,gettempsize,R_ESP));
  2478. end;
  2479. { parameters are limited to 65535 bytes because }
  2480. { ret allows only imm16 }
  2481. if (parasize>65535) and not(pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2482. CGMessage(cg_e_parasize_too_big);
  2483. { at last, the return is generated }
  2484. if not inlined then
  2485. if (po_interrupt in aktprocsym.definition.procoptions) then
  2486. begin
  2487. if uses_esi then
  2488. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16)));
  2489. if uses_edx then
  2490. begin
  2491. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2492. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12)));
  2493. end;
  2494. if uses_eax then
  2495. begin
  2496. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2497. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0)));
  2498. end;
  2499. generate_interrupt_stackframe_exit;
  2500. end
  2501. else
  2502. begin
  2503. {Routines with the poclearstack flag set use only a ret.}
  2504. { also routines with parasize=0 }
  2505. if (pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2506. begin
  2507. {$ifndef OLD_C_STACK}
  2508. { complex return values are removed from stack in C code PM }
  2509. if ret_in_param(aktprocsym.definition.rettype.def) then
  2510. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,4))
  2511. else
  2512. {$endif not OLD_C_STACK}
  2513. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2514. end
  2515. else if (parasize=0) then
  2516. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO))
  2517. else
  2518. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  2519. end;
  2520. if not inlined then
  2521. exprasmList.concat(Tai_symbol_end.Createname(aktprocsym.definition.mangledname));
  2522. {$ifdef GDB}
  2523. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2524. begin
  2525. aktprocsym.concatstabto(exprasmlist);
  2526. if assigned(procinfo^._class) then
  2527. if (not assigned(procinfo^.parent) or
  2528. not assigned(procinfo^.parent^._class)) then
  2529. begin
  2530. if (po_classmethod in aktprocsym.definition.procoptions) or
  2531. ((po_virtualmethod in aktprocsym.definition.procoptions) and
  2532. (potype_constructor=aktprocsym.definition.proctypeoption)) or
  2533. (po_staticmethod in aktprocsym.definition.procoptions) then
  2534. begin
  2535. exprasmList.concat(Tai_stabs.Create(strpnew(
  2536. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  2537. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2538. end
  2539. else
  2540. begin
  2541. if not(is_class(procinfo^._class)) then
  2542. st:='v'
  2543. else
  2544. st:='p';
  2545. exprasmList.concat(Tai_stabs.Create(strpnew(
  2546. '"$t:'+st+procinfo^._class.numberstring+'",'+
  2547. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2548. end;
  2549. end
  2550. else
  2551. begin
  2552. if not is_class(procinfo^._class) then
  2553. st:='*'
  2554. else
  2555. st:='';
  2556. exprasmList.concat(Tai_stabs.Create(strpnew(
  2557. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  2558. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI]))));
  2559. end;
  2560. { define calling EBP as pseudo local var PM }
  2561. { this enables test if the function is a local one !! }
  2562. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2563. exprasmList.concat(Tai_stabs.Create(strpnew(
  2564. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  2565. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  2566. if (not is_void(aktprocsym.definition.rettype.def)) then
  2567. begin
  2568. if ret_in_param(aktprocsym.definition.rettype.def) then
  2569. exprasmList.concat(Tai_stabs.Create(strpnew(
  2570. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2571. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2572. else
  2573. exprasmList.concat(Tai_stabs.Create(strpnew(
  2574. '"'+aktprocsym.name+':X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2575. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2576. if (m_result in aktmodeswitches) then
  2577. if ret_in_param(aktprocsym.definition.rettype.def) then
  2578. exprasmList.concat(Tai_stabs.Create(strpnew(
  2579. '"RESULT:X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2580. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2581. else
  2582. exprasmList.concat(Tai_stabs.Create(strpnew(
  2583. '"RESULT:X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2584. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2585. end;
  2586. mangled_length:=length(aktprocsym.definition.mangledname);
  2587. getmem(p,2*mangled_length+50);
  2588. strpcopy(p,'192,0,0,');
  2589. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2590. if (target_info.use_function_relative_addresses) then
  2591. begin
  2592. strpcopy(strend(p),'-');
  2593. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2594. end;
  2595. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2596. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  2597. +aktprocsym.definition.mangledname))));
  2598. p[0]:='2';p[1]:='2';p[2]:='4';
  2599. strpcopy(strend(p),'_end');}
  2600. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  2601. if (target_info.use_function_relative_addresses) then
  2602. begin
  2603. strpcopy(strend(p),'-');
  2604. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2605. end;
  2606. exprasmList.concatlist(withdebuglist);
  2607. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2608. { strpnew('224,0,0,'
  2609. +aktprocsym.definition.mangledname+'_end'))));}
  2610. freemem(p,2*mangled_length+50);
  2611. end;
  2612. {$endif GDB}
  2613. if inlined then
  2614. cleanup_regvars(exprasmlist);
  2615. exprasmlist:=oldexprasmlist;
  2616. end;
  2617. procedure genimplicitunitfinal(alist : TAAsmoutput);
  2618. begin
  2619. { using current_module.globalsymtable is hopefully }
  2620. { more robust than symtablestack and symtablestack.next }
  2621. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2622. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2623. exprasmList.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  2624. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  2625. {$ifdef GDB}
  2626. if (cs_debuginfo in aktmoduleswitches) and
  2627. target_info.use_function_relative_addresses then
  2628. exprasmList.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  2629. {$endif GDB}
  2630. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2631. aList.concatlist(exprasmlist);
  2632. end;
  2633. procedure genimplicitunitinit(alist : TAAsmoutput);
  2634. begin
  2635. { using current_module.globalsymtable is hopefully }
  2636. { more robust than symtablestack and symtablestack.next }
  2637. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2638. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2639. exprasmList.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  2640. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  2641. {$ifdef GDB}
  2642. if (cs_debuginfo in aktmoduleswitches) and
  2643. target_info.use_function_relative_addresses then
  2644. exprasmList.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  2645. {$endif GDB}
  2646. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2647. aList.concatlist(exprasmlist);
  2648. end;
  2649. {$ifdef test_dest_loc}
  2650. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2651. begin
  2652. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2653. begin
  2654. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2655. set_location(p^.location,dest_loc);
  2656. in_dest_loc:=true;
  2657. end
  2658. else
  2659. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2660. begin
  2661. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference)));
  2662. set_location(p^.location,dest_loc);
  2663. in_dest_loc:=true;
  2664. end
  2665. else
  2666. internalerror(20080);
  2667. end;
  2668. {$endif test_dest_loc}
  2669. end.
  2670. {
  2671. $Log$
  2672. Revision 1.29 2001-08-12 20:23:02 peter
  2673. * netbsd doesn't use stackchecking
  2674. Revision 1.28 2001/08/07 18:47:13 peter
  2675. * merged netbsd start
  2676. * profile for win32
  2677. Revision 1.27 2001/08/06 21:40:49 peter
  2678. * funcret moved from tprocinfo to tprocdef
  2679. Revision 1.26 2001/07/30 20:59:28 peter
  2680. * m68k updates from v10 merged
  2681. Revision 1.25 2001/07/01 20:16:18 peter
  2682. * alignmentinfo record added
  2683. * -Oa argument supports more alignment settings that can be specified
  2684. per type: PROC,LOOP,VARMIN,VARMAX,CONSTMIN,CONSTMAX,RECORDMIN
  2685. RECORDMAX,LOCALMIN,LOCALMAX. It is possible to set the mimimum
  2686. required alignment and the maximum usefull alignment. The final
  2687. alignment will be choosen per variable size dependent on these
  2688. settings
  2689. Revision 1.24 2001/05/27 14:30:55 florian
  2690. + some widestring stuff added
  2691. Revision 1.23 2001/04/21 13:33:16 peter
  2692. * move winstackpagesize const to cgai386 to remove uses t_win32
  2693. Revision 1.22 2001/04/21 12:05:32 peter
  2694. * add nop after popa (merged)
  2695. Revision 1.21 2001/04/18 22:02:00 peter
  2696. * registration of targets and assemblers
  2697. Revision 1.20 2001/04/13 01:22:17 peter
  2698. * symtable change to classes
  2699. * range check generation and errors fixed, make cycle DEBUG=1 works
  2700. * memory leaks fixed
  2701. Revision 1.19 2001/04/05 21:33:07 peter
  2702. * fast exit fix merged
  2703. Revision 1.18 2001/04/02 21:20:35 peter
  2704. * resulttype rewrite
  2705. Revision 1.17 2001/01/05 17:36:58 florian
  2706. * the info about exception frames is stored now on the stack
  2707. instead on the heap
  2708. Revision 1.16 2000/12/25 00:07:31 peter
  2709. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2710. tlinkedlist objects)
  2711. Revision 1.15 2000/12/05 11:44:32 jonas
  2712. + new integer regvar handling, should be much more efficient
  2713. Revision 1.14 2000/11/29 00:30:43 florian
  2714. * unused units removed from uses clause
  2715. * some changes for widestrings
  2716. Revision 1.13 2000/11/28 00:28:07 pierre
  2717. * stabs fixing
  2718. Revision 1.12 2000/11/22 15:12:06 jonas
  2719. * fixed inline-related problems (partially "merges")
  2720. Revision 1.11 2000/11/17 10:30:24 florian
  2721. * passing interfaces as parameters fixed
  2722. Revision 1.10 2000/11/07 23:40:48 florian
  2723. + AfterConstruction and BeforeDestruction impemented
  2724. Revision 1.9 2000/11/06 23:49:20 florian
  2725. * fixed init_paras call
  2726. Revision 1.8 2000/11/06 23:15:01 peter
  2727. * added copyvaluepara call again
  2728. Revision 1.7 2000/11/04 14:25:23 florian
  2729. + merged Attila's changes for interfaces, not tested yet
  2730. Revision 1.6 2000/10/31 22:02:55 peter
  2731. * symtable splitted, no real code changes
  2732. Revision 1.5 2000/10/24 22:23:04 peter
  2733. * emitcall -> emitinsertcall for profiling (merged)
  2734. Revision 1.4 2000/10/24 12:47:45 jonas
  2735. * allocate registers which hold function result
  2736. Revision 1.3 2000/10/24 08:54:25 michael
  2737. + Extra patch from peter
  2738. Revision 1.2 2000/10/24 07:20:03 pierre
  2739. * fix for bug 1193 (merged)
  2740. Revision 1.1 2000/10/15 09:47:42 peter
  2741. * moved to i386/
  2742. Revision 1.19 2000/10/14 10:14:46 peter
  2743. * moehrendorf oct 2000 rewrite
  2744. Revision 1.18 2000/10/10 14:55:28 jonas
  2745. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2746. Revision 1.17 2000/10/01 19:48:23 peter
  2747. * lot of compile updates for cg11
  2748. Revision 1.16 2000/09/30 16:08:45 peter
  2749. * more cg11 updates
  2750. Revision 1.15 2000/09/24 15:06:12 peter
  2751. * use defines.inc
  2752. Revision 1.14 2000/09/16 12:22:52 peter
  2753. * freebsd support merged
  2754. Revision 1.13 2000/08/27 16:11:49 peter
  2755. * moved some util functions from globals,cobjects to cutils
  2756. * splitted files into finput,fmodule
  2757. Revision 1.12 2000/08/24 19:07:54 peter
  2758. * don't initialize if localvarsym is set because that varsym will
  2759. already be initialized
  2760. * first initialize local data before copy of value para's (merged)
  2761. Revision 1.11 2000/08/19 20:09:33 peter
  2762. * check size after checking openarray in push_value_para (merged)
  2763. Revision 1.10 2000/08/16 13:06:06 florian
  2764. + support of 64 bit integer constants
  2765. Revision 1.9 2000/08/10 18:42:03 peter
  2766. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2767. Revision 1.8 2000/08/07 11:29:40 jonas
  2768. + emit_push_mem_size() which pushes a value in memory of a certain size
  2769. * pushsetelement() and pushvaluepara() use this new procedure, because
  2770. otherwise they could sometimes try to push data past the end of the
  2771. heap, causing a crash
  2772. (merged from fixes branch)
  2773. Revision 1.7 2000/08/03 13:17:25 jonas
  2774. + allow regvars to be used inside inlined procs, which required the
  2775. following changes:
  2776. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2777. * moved all regvar related code to new regvars unit
  2778. + added pregvarinfo type to hcodegen
  2779. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2780. * deallocate the regvars of the caller in secondprocinline before
  2781. inlining the called procedure and reallocate them afterwards
  2782. Revision 1.6 2000/08/02 08:05:04 jonas
  2783. * fixed web bug1087
  2784. * allocate R_ECX explicitely if it's used
  2785. (merged from fixes branch)
  2786. Revision 1.5 2000/07/27 09:25:05 jonas
  2787. * moved locflags2reg() procedure from cg386add to cgai386
  2788. + added locjump2reg() procedure to cgai386
  2789. * fixed internalerror(2002) when the result of a case expression has
  2790. LOC_JUMP
  2791. (all merged from fixes branch)
  2792. Revision 1.4 2000/07/21 15:14:02 jonas
  2793. + added is_addr field for labels, if they are only used for getting the address
  2794. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2795. Revision 1.3 2000/07/13 12:08:25 michael
  2796. + patched to 1.1.0 with former 1.09patch from peter
  2797. Revision 1.2 2000/07/13 11:32:37 michael
  2798. + removed logs
  2799. }