cgai386.pas 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054
  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. cobjects,
  23. cpubase,cpuasm,
  24. symconst,symtype,symdef,aasm;
  25. {$define TESTGETTEMP to store const that
  26. are written into temps for later release PM }
  27. function def_opsize(p1:pdef):topsize;
  28. function def2def_opsize(p1,p2:pdef):topsize;
  29. function def_getreg(p1:pdef):tregister;
  30. function makereg8(r:tregister):tregister;
  31. function makereg16(r:tregister):tregister;
  32. function makereg32(r:tregister):tregister;
  33. procedure locflags2reg(var l:tlocation;opsize:topsize);
  34. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: pasmlabel);
  35. procedure emitlab(var l : pasmlabel);
  36. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  37. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  38. procedure emit_none(i : tasmop;s : topsize);
  39. procedure emit_const(i : tasmop;s : topsize;c : longint);
  40. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  41. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  42. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  43. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  44. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  45. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  46. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  47. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  48. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  49. procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
  50. procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
  51. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
  52. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
  53. procedure emitcall(const routine:string);
  54. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  55. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  56. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  57. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  58. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  59. procedure emit_push_loc(const t:tlocation);
  60. procedure emit_push_mem_size(const t: treference; size: longint);
  61. { pushes qword location to the stack }
  62. procedure emit_pushq_loc(const t : tlocation);
  63. procedure release_qword_loc(const t : tlocation);
  64. { remove non regvar registers in loc from regs (in the format }
  65. { pushusedregisters uses) }
  66. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  67. { releases the registers of a location }
  68. procedure release_loc(const t : tlocation);
  69. procedure emit_pushw_loc(const t:tlocation);
  70. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  71. procedure emit_to_mem(var t:tlocation;def:pdef);
  72. procedure emit_to_reg16(var hr:tregister);
  73. procedure emit_to_reg32(var hr:tregister);
  74. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  75. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  76. procedure copyshortstring(const dref,sref : treference;len : byte;
  77. loadref, del_sref: boolean);
  78. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  79. procedure incrstringref(t : pdef;const ref : treference);
  80. procedure decrstringref(t : pdef;const ref : treference);
  81. procedure push_int(l : longint);
  82. procedure emit_push_mem(const ref : treference);
  83. procedure emitpushreferenceaddr(const ref : treference);
  84. procedure incrcomintfref(t: pdef; const ref: treference);
  85. procedure decrcomintfref(t: pdef; const ref: treference);
  86. procedure floatload(t : tfloattype;const ref : treference);
  87. procedure floatstore(t : tfloattype;const ref : treference);
  88. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  89. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  90. procedure maybe_loadesi;
  91. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;destreg:Tregister;delloc:boolean);
  92. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  93. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  94. stackframe:longint;
  95. var parasize:longint;var nostackframe:boolean;
  96. inlined : boolean);
  97. procedure genexitcode(alist : TAAsmoutput;parasize:longint;
  98. nostackframe,inlined:boolean);
  99. { if a unit doesn't have a explicit init/final code, }
  100. { we've to generate one, if the units has ansistrings }
  101. { in the interface or implementation }
  102. procedure genimplicitunitfinal(alist : TAAsmoutput);
  103. procedure genimplicitunitinit(alist : TAAsmoutput);
  104. {$ifdef test_dest_loc}
  105. const
  106. { used to avoid temporary assignments }
  107. dest_loc_known : boolean = false;
  108. in_dest_loc : boolean = false;
  109. dest_loc_tree : ptree = nil;
  110. var
  111. dest_loc : tlocation;
  112. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  113. {$endif test_dest_loc}
  114. implementation
  115. uses
  116. {$ifdef delphi}
  117. sysutils,
  118. {$else}
  119. strings,
  120. {$endif}
  121. cutils,
  122. globtype,systems,globals,verbose,
  123. fmodule,
  124. symbase,symsym,symtable,types,
  125. tgcpu,temp_gen,hcodegen,regvars
  126. {$ifdef GDB}
  127. ,gdb
  128. {$endif}
  129. {$ifndef NOTARGETWIN32}
  130. ,t_win32
  131. {$endif}
  132. ;
  133. {*****************************************************************************
  134. Helpers
  135. *****************************************************************************}
  136. function def_opsize(p1:pdef):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:pdef):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:pdef):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: pasmlabel);
  241. var
  242. hregister : tregister;
  243. hl : pasmlabel;
  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 : pasmlabel);
  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 : pasmlabel);
  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 : pasmsymbol);
  354. begin
  355. exprasmList.concat(Taicpu.Op_sym(i,s,op));
  356. end;
  357. procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;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 : pasmsymbol;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 : pasmsymbol;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 target_os.stackalignment=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 target_os.stackalignment=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. (target_os.stackalignment=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 target_os.stackalignment=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:pdef);
  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(pfloatdef(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(pfloatdef(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: pdef; 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: pdef; 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_loadesi;
  893. end;
  894. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  895. begin
  896. emitpushreferenceaddr(dref);
  897. if loadref then
  898. emit_push_mem(sref)
  899. else
  900. emitpushreferenceaddr(sref);
  901. push_int(len);
  902. saveregvars($ff);
  903. emitcall('FPC_LONGSTR_COPY');
  904. maybe_loadesi;
  905. end;
  906. procedure incrstringref(t : pdef;const ref : treference);
  907. var
  908. pushedregs : tpushed;
  909. begin
  910. pushusedregisters(pushedregs,$ff);
  911. emitpushreferenceaddr(ref);
  912. saveregvars($ff);
  913. if is_ansistring(t) then
  914. begin
  915. emitcall('FPC_ANSISTR_INCR_REF');
  916. end
  917. else if is_widestring(t) then
  918. begin
  919. emitcall('FPC_WIDESTR_INCR_REF');
  920. end
  921. else internalerror(1859);
  922. popusedregisters(pushedregs);
  923. end;
  924. procedure decrstringref(t : pdef;const ref : treference);
  925. var
  926. pushedregs : tpushed;
  927. begin
  928. pushusedregisters(pushedregs,$ff);
  929. emitpushreferenceaddr(ref);
  930. saveregvars($ff);
  931. if is_ansistring(t) then
  932. begin
  933. emitcall('FPC_ANSISTR_DECR_REF');
  934. end
  935. else if is_widestring(t) then
  936. begin
  937. emitcall('FPC_WIDESTR_DECR_REF');
  938. end
  939. else internalerror(1859);
  940. popusedregisters(pushedregs);
  941. end;
  942. {*****************************************************************************
  943. Emit Push Functions
  944. *****************************************************************************}
  945. procedure push_int(l : longint);
  946. begin
  947. if (l = 0) and
  948. not(aktoptprocessor in [Class386, ClassP6]) and
  949. not(cs_littlesize in aktglobalswitches)
  950. Then
  951. begin
  952. getexplicitregister32(R_EDI);
  953. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  954. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  955. ungetregister32(R_EDI);
  956. end
  957. else
  958. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,l));
  959. end;
  960. procedure emit_push_mem(const ref : treference);
  961. begin
  962. if ref.is_immediate then
  963. push_int(ref.offset)
  964. else
  965. begin
  966. if not(aktoptprocessor in [Class386, ClassP6]) and
  967. not(cs_littlesize in aktglobalswitches)
  968. then
  969. begin
  970. getexplicitregister32(R_EDI);
  971. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  972. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  973. ungetregister32(R_EDI);
  974. end
  975. else exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)));
  976. end;
  977. end;
  978. procedure emitpushreferenceaddr(const ref : treference);
  979. var
  980. href : treference;
  981. begin
  982. { this will fail for references to other segments !!! }
  983. if ref.is_immediate then
  984. { is this right ? }
  985. begin
  986. { push_int(ref.offset)}
  987. gettempofsizereference(4,href);
  988. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  989. emitpushreferenceaddr(href);
  990. del_reference(href);
  991. end
  992. else
  993. begin
  994. if ref.segment<>R_NO then
  995. CGMessage(cg_e_cant_use_far_pointer_there);
  996. if (ref.base=R_NO) and (ref.index=R_NO) then
  997. exprasmList.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset))
  998. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  999. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  1000. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.index))
  1001. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  1002. (ref.offset=0) and (ref.symbol=nil) then
  1003. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.base))
  1004. else
  1005. begin
  1006. getexplicitregister32(R_EDI);
  1007. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1008. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1009. ungetregister32(R_EDI);
  1010. end;
  1011. end;
  1012. end;
  1013. {*****************************************************************************
  1014. Emit Float Functions
  1015. *****************************************************************************}
  1016. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1017. begin
  1018. case t of
  1019. s32real : begin
  1020. op:=A_FLD;
  1021. s:=S_FS;
  1022. end;
  1023. s64real : begin
  1024. op:=A_FLD;
  1025. { ???? }
  1026. s:=S_FL;
  1027. end;
  1028. s80real : begin
  1029. op:=A_FLD;
  1030. s:=S_FX;
  1031. end;
  1032. s64comp : begin
  1033. op:=A_FILD;
  1034. s:=S_IQ;
  1035. end;
  1036. else internalerror(17);
  1037. end;
  1038. end;
  1039. procedure floatload(t : tfloattype;const ref : treference);
  1040. var
  1041. op : tasmop;
  1042. s : topsize;
  1043. begin
  1044. floatloadops(t,op,s);
  1045. exprasmList.concat(Taicpu.Op_ref(op,s,
  1046. newreference(ref)));
  1047. inc(fpuvaroffset);
  1048. end;
  1049. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1050. begin
  1051. case t of
  1052. s32real : begin
  1053. op:=A_FSTP;
  1054. s:=S_FS;
  1055. end;
  1056. s64real : begin
  1057. op:=A_FSTP;
  1058. s:=S_FL;
  1059. end;
  1060. s80real : begin
  1061. op:=A_FSTP;
  1062. s:=S_FX;
  1063. end;
  1064. s64comp : begin
  1065. op:=A_FISTP;
  1066. s:=S_IQ;
  1067. end;
  1068. else
  1069. internalerror(17);
  1070. end;
  1071. end;
  1072. procedure floatstore(t : tfloattype;const ref : treference);
  1073. var
  1074. op : tasmop;
  1075. s : topsize;
  1076. begin
  1077. floatstoreops(t,op,s);
  1078. exprasmList.concat(Taicpu.Op_ref(op,s,
  1079. newreference(ref)));
  1080. dec(fpuvaroffset);
  1081. end;
  1082. {*****************************************************************************
  1083. Emit Functions
  1084. *****************************************************************************}
  1085. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1086. const
  1087. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1088. ishr : array[0..3] of byte=(2,0,1,0);
  1089. var
  1090. ecxpushed : boolean;
  1091. helpsize : longint;
  1092. i : byte;
  1093. reg8,reg32 : tregister;
  1094. swap : boolean;
  1095. procedure maybepushecx;
  1096. begin
  1097. if not(R_ECX in unused) then
  1098. begin
  1099. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1100. ecxpushed:=true;
  1101. end
  1102. else getexplicitregister32(R_ECX);
  1103. end;
  1104. begin
  1105. {$IfNDef regallocfix}
  1106. If delsource then
  1107. del_reference(source);
  1108. {$EndIf regallocfix}
  1109. if (not loadref) and
  1110. ((size<=8) or
  1111. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1112. begin
  1113. helpsize:=size shr 2;
  1114. getexplicitregister32(R_EDI);
  1115. for i:=1 to helpsize do
  1116. begin
  1117. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1118. {$ifdef regallocfix}
  1119. If (size = 4) and delsource then
  1120. del_reference(source);
  1121. {$endif regallocfix}
  1122. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest)));
  1123. inc(source.offset,4);
  1124. inc(dest.offset,4);
  1125. dec(size,4);
  1126. end;
  1127. if size>1 then
  1128. begin
  1129. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1130. {$ifdef regallocfix}
  1131. If (size = 2) and delsource then
  1132. del_reference(source);
  1133. {$endif regallocfix}
  1134. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_W,R_DI,newreference(dest)));
  1135. inc(source.offset,2);
  1136. inc(dest.offset,2);
  1137. dec(size,2);
  1138. end;
  1139. ungetregister32(R_EDI);
  1140. if size>0 then
  1141. begin
  1142. { and now look for an 8 bit register }
  1143. swap:=false;
  1144. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1145. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1146. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1147. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1148. else
  1149. begin
  1150. swap:=true;
  1151. { we need only to check 3 registers, because }
  1152. { one is always not index or base }
  1153. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1154. begin
  1155. reg8:=R_AL;
  1156. reg32:=R_EAX;
  1157. end
  1158. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1159. begin
  1160. reg8:=R_BL;
  1161. reg32:=R_EBX;
  1162. end
  1163. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1164. begin
  1165. reg8:=R_CL;
  1166. reg32:=R_ECX;
  1167. end;
  1168. end;
  1169. if swap then
  1170. { was earlier XCHG, of course nonsense }
  1171. begin
  1172. getexplicitregister32(R_EDI);
  1173. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1174. end;
  1175. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1176. {$ifdef regallocfix}
  1177. If delsource then
  1178. del_reference(source);
  1179. {$endif regallocfix}
  1180. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_B,reg8,newreference(dest)));
  1181. if swap then
  1182. begin
  1183. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1184. ungetregister32(R_EDI);
  1185. end
  1186. else
  1187. ungetregister(reg8);
  1188. end;
  1189. end
  1190. else
  1191. begin
  1192. getexplicitregister32(R_EDI);
  1193. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1194. {$ifdef regallocfix}
  1195. {is this ok?? (JM)}
  1196. del_reference(dest);
  1197. {$endif regallocfix}
  1198. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1199. if loadref then
  1200. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1201. else
  1202. begin
  1203. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1204. {$ifdef regallocfix}
  1205. if delsource then
  1206. del_reference(source);
  1207. {$endif regallocfix}
  1208. end;
  1209. exprasmList.concat(Taicpu.Op_none(A_CLD,S_NO));
  1210. ecxpushed:=false;
  1211. if cs_littlesize in aktglobalswitches then
  1212. begin
  1213. maybepushecx;
  1214. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1215. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1216. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1217. end
  1218. else
  1219. begin
  1220. helpsize:=size shr 2;
  1221. size:=size and 3;
  1222. if helpsize>1 then
  1223. begin
  1224. maybepushecx;
  1225. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1226. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1227. end;
  1228. if helpsize>0 then
  1229. exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1230. if size>1 then
  1231. begin
  1232. dec(size,2);
  1233. exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1234. end;
  1235. if size=1 then
  1236. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1237. end;
  1238. ungetregister32(R_EDI);
  1239. exprasmList.concat(Tairegalloc.DeAlloc(R_ESI));
  1240. if ecxpushed then
  1241. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1242. else
  1243. ungetregister32(R_ECX);
  1244. { loading SELF-reference again }
  1245. maybe_loadesi;
  1246. end;
  1247. if delsource then
  1248. ungetiftemp(source);
  1249. end;
  1250. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;
  1251. destreg:Tregister;delloc:boolean);
  1252. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1253. var tai:Taicpu;
  1254. r:Preference;
  1255. begin
  1256. tai := nil;
  1257. case location.loc of
  1258. LOC_REGISTER,LOC_CREGISTER:
  1259. begin
  1260. case orddef^.typ of
  1261. u8bit:
  1262. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_BL,location.register,destreg);
  1263. s8bit:
  1264. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_BL,location.register,destreg);
  1265. u16bit:
  1266. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_WL,location.register,destreg);
  1267. s16bit:
  1268. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_WL,location.register,destreg);
  1269. u32bit,s32bit:
  1270. if location.register <> destreg then
  1271. tai:=Taicpu.Op_reg_reg(A_MOV,S_L,location.register,destreg);
  1272. end;
  1273. if delloc then
  1274. ungetregister(location.register);
  1275. end;
  1276. LOC_MEM,
  1277. LOC_REFERENCE:
  1278. begin
  1279. if location.reference.is_immediate then
  1280. tai:=Taicpu.Op_const_reg(A_MOV,S_L,location.reference.offset,destreg)
  1281. else
  1282. begin
  1283. r:=newreference(location.reference);
  1284. case orddef^.typ of
  1285. u8bit:
  1286. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_BL,r,destreg);
  1287. s8bit:
  1288. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_BL,r,destreg);
  1289. u16bit:
  1290. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_WL,r,destreg);
  1291. s16bit:
  1292. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_WL,r,destreg);
  1293. u32bit:
  1294. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1295. s32bit:
  1296. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1297. end;
  1298. end;
  1299. if delloc then
  1300. del_reference(location.reference);
  1301. end
  1302. else
  1303. internalerror(6);
  1304. end;
  1305. if assigned(tai) then
  1306. exprasmList.concat(tai);
  1307. end;
  1308. { if necessary ESI is reloaded after a call}
  1309. procedure maybe_loadesi;
  1310. var
  1311. hp : preference;
  1312. p : pprocinfo;
  1313. i : longint;
  1314. begin
  1315. if assigned(procinfo^._class) then
  1316. begin
  1317. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1318. if lexlevel>normal_function_level then
  1319. begin
  1320. new(hp);
  1321. reset_reference(hp^);
  1322. hp^.offset:=procinfo^.framepointer_offset;
  1323. hp^.base:=procinfo^.framepointer;
  1324. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1325. p:=procinfo^.parent;
  1326. for i:=3 to lexlevel-1 do
  1327. begin
  1328. new(hp);
  1329. reset_reference(hp^);
  1330. hp^.offset:=p^.framepointer_offset;
  1331. hp^.base:=R_ESI;
  1332. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1333. p:=p^.parent;
  1334. end;
  1335. new(hp);
  1336. reset_reference(hp^);
  1337. hp^.offset:=p^.selfpointer_offset;
  1338. hp^.base:=R_ESI;
  1339. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1340. end
  1341. else
  1342. begin
  1343. new(hp);
  1344. reset_reference(hp^);
  1345. hp^.offset:=procinfo^.selfpointer_offset;
  1346. hp^.base:=procinfo^.framepointer;
  1347. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1348. end;
  1349. end;
  1350. end;
  1351. {*****************************************************************************
  1352. Entry/Exit Code Functions
  1353. *****************************************************************************}
  1354. procedure genprofilecode;
  1355. var
  1356. pl : pasmlabel;
  1357. begin
  1358. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1359. exit;
  1360. case target_info.target of
  1361. target_i386_freebsd,
  1362. target_i386_linux:
  1363. begin
  1364. getaddrlabel(pl);
  1365. emitinsertcall('mcount');
  1366. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1367. exprasmList.insert(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1368. exprasmList.insert(Tai_section.Create(sec_code));
  1369. exprasmList.insert(Tai_const.Create_32bit(0));
  1370. exprasmList.insert(Tai_label.Create(pl));
  1371. exprasmList.insert(Tai_align.Create(4));
  1372. exprasmList.insert(Tai_section.Create(sec_data));
  1373. end;
  1374. target_i386_go32v2:
  1375. begin
  1376. emitinsertcall('MCOUNT');
  1377. end;
  1378. end;
  1379. end;
  1380. procedure generate_interrupt_stackframe_entry;
  1381. begin
  1382. { save the registers of an interrupt procedure }
  1383. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1384. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1385. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1386. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1387. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1388. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1389. { .... also the segment registers }
  1390. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1391. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1392. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1393. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1394. end;
  1395. procedure generate_interrupt_stackframe_exit;
  1396. begin
  1397. { restore the registers of an interrupt procedure }
  1398. { this was all with entrycode instead of exitcode !!}
  1399. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1400. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1401. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1402. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1403. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1404. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1405. { .... also the segment registers }
  1406. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1407. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1408. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1409. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1410. { this restores the flags }
  1411. procinfo^.aktexitcode.concat(Taicpu.Op_none(A_IRET,S_NO));
  1412. end;
  1413. { generates the code for threadvar initialisation }
  1414. procedure initialize_threadvar(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1415. var
  1416. hr : treference;
  1417. begin
  1418. if (psym(p)^.typ=varsym) and
  1419. (vo_is_thread_var in pvarsym(p)^.varoptions) then
  1420. begin
  1421. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,pvarsym(p)^.getsize));
  1422. reset_reference(hr);
  1423. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  1424. emitpushreferenceaddr(hr);
  1425. saveregvars($ff);
  1426. emitcall('FPC_INIT_THREADVAR');
  1427. end;
  1428. end;
  1429. { initilizes data of type t }
  1430. { if is_already_ref is true then the routines assumes }
  1431. { that r points to the data to initialize }
  1432. procedure initialize(t : pdef;const ref : treference;is_already_ref : boolean);
  1433. var
  1434. hr : treference;
  1435. begin
  1436. if is_ansistring(t) or
  1437. is_widestring(t) or
  1438. is_interfacecom(t) then
  1439. begin
  1440. emit_const_ref(A_MOV,S_L,0,
  1441. newreference(ref));
  1442. end
  1443. else
  1444. begin
  1445. reset_reference(hr);
  1446. hr.symbol:=pstoreddef(t)^.get_inittable_label;
  1447. emitpushreferenceaddr(hr);
  1448. if is_already_ref then
  1449. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1450. newreference(ref)))
  1451. else
  1452. emitpushreferenceaddr(ref);
  1453. emitcall('FPC_INITIALIZE');
  1454. end;
  1455. end;
  1456. { finalizes data of type t }
  1457. { if is_already_ref is true then the routines assumes }
  1458. { that r points to the data to finalizes }
  1459. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  1460. var
  1461. r : treference;
  1462. begin
  1463. if is_ansistring(t) or
  1464. is_widestring(t) then
  1465. begin
  1466. decrstringref(t,ref);
  1467. end
  1468. else if is_interfacecom(t) then
  1469. begin
  1470. decrcomintfref(t,ref);
  1471. end
  1472. else
  1473. begin
  1474. reset_reference(r);
  1475. r.symbol:=pstoreddef(t)^.get_inittable_label;
  1476. emitpushreferenceaddr(r);
  1477. if is_already_ref then
  1478. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1479. newreference(ref)))
  1480. else
  1481. emitpushreferenceaddr(ref);
  1482. emitcall('FPC_FINALIZE');
  1483. end;
  1484. end;
  1485. { generates the code for initialisation of local data }
  1486. procedure initialize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1487. var
  1488. hr : treference;
  1489. begin
  1490. if (psym(p)^.typ=varsym) and
  1491. assigned(pvarsym(p)^.vartype.def) and
  1492. not(is_class(pvarsym(p)^.vartype.def)) and
  1493. pvarsym(p)^.vartype.def^.needs_inittable then
  1494. begin
  1495. if assigned(procinfo) then
  1496. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1497. reset_reference(hr);
  1498. if psym(p)^.owner^.symtabletype in [localsymtable,inlinelocalsymtable] then
  1499. begin
  1500. hr.base:=procinfo^.framepointer;
  1501. hr.offset:=-pvarsym(p)^.address+pvarsym(p)^.owner^.address_fixup;
  1502. end
  1503. else
  1504. begin
  1505. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  1506. end;
  1507. initialize(pvarsym(p)^.vartype.def,hr,false);
  1508. end;
  1509. end;
  1510. { generates the code for incrementing the reference count of parameters and
  1511. initialize out parameters }
  1512. procedure init_paras(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1513. var
  1514. hrv : treference;
  1515. hr: treference;
  1516. begin
  1517. if (psym(p)^.typ=varsym) and
  1518. not is_class(pvarsym(p)^.vartype.def) and
  1519. pvarsym(p)^.vartype.def^.needs_inittable then
  1520. begin
  1521. if (pvarsym(p)^.varspez=vs_value) then
  1522. begin
  1523. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1524. reset_reference(hrv);
  1525. hrv.base:=procinfo^.framepointer;
  1526. hrv.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1527. if is_ansistring(pvarsym(p)^.vartype.def) or
  1528. is_widestring(pvarsym(p)^.vartype.def) then
  1529. begin
  1530. incrstringref(pvarsym(p)^.vartype.def,hrv)
  1531. end
  1532. else if is_interfacecom(pvarsym(p)^.vartype.def) then
  1533. begin
  1534. incrcomintfref(pvarsym(p)^.vartype.def,hrv)
  1535. end
  1536. else
  1537. begin
  1538. reset_reference(hr);
  1539. hr.symbol:=pstoreddef(pvarsym(p)^.vartype.def)^.get_inittable_label;
  1540. emitpushreferenceaddr(hr);
  1541. emitpushreferenceaddr(hrv);
  1542. emitcall('FPC_ADDREF');
  1543. end;
  1544. end
  1545. else if (pvarsym(p)^.varspez=vs_out) then
  1546. begin
  1547. reset_reference(hrv);
  1548. hrv.base:=procinfo^.framepointer;
  1549. hrv.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1550. {$ifndef noAllocEdi}
  1551. getexplicitregister32(R_EDI);
  1552. {$endif noAllocEdi}
  1553. exprasmList.concat(Taicpu.Op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI));
  1554. reset_reference(hr);
  1555. hr.base:=R_EDI;
  1556. initialize(pvarsym(p)^.vartype.def,hr,false);
  1557. end;
  1558. end;
  1559. end;
  1560. { generates the code for decrementing the reference count of parameters }
  1561. procedure final_paras(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1562. var
  1563. hrv : treference;
  1564. hr: treference;
  1565. begin
  1566. if (psym(p)^.typ=varsym) and
  1567. not is_class(pvarsym(p)^.vartype.def) and
  1568. pvarsym(p)^.vartype.def^.needs_inittable then
  1569. begin
  1570. if (pvarsym(p)^.varspez=vs_value) then
  1571. begin
  1572. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1573. reset_reference(hrv);
  1574. hrv.base:=procinfo^.framepointer;
  1575. hrv.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1576. if is_ansistring(pvarsym(p)^.vartype.def) or
  1577. is_widestring(pvarsym(p)^.vartype.def) then
  1578. begin
  1579. decrstringref(pvarsym(p)^.vartype.def,hrv)
  1580. end
  1581. else if is_interfacecom(pvarsym(p)^.vartype.def) then
  1582. begin
  1583. decrcomintfref(pvarsym(p)^.vartype.def,hrv)
  1584. end
  1585. else
  1586. begin
  1587. reset_reference(hr);
  1588. hr.symbol:=pstoreddef(pvarsym(p)^.vartype.def)^.get_inittable_label;
  1589. emitpushreferenceaddr(hr);
  1590. emitpushreferenceaddr(hrv);
  1591. emitcall('FPC_DECREF');
  1592. end;
  1593. end;
  1594. end;
  1595. end;
  1596. { generates the code for finalisation of local data }
  1597. procedure finalize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1598. var
  1599. hr : treference;
  1600. begin
  1601. if (psym(p)^.typ=varsym) and
  1602. assigned(pvarsym(p)^.vartype.def) and
  1603. not(is_class(pvarsym(p)^.vartype.def)) and
  1604. pvarsym(p)^.vartype.def^.needs_inittable then
  1605. begin
  1606. if assigned(procinfo) then
  1607. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1608. reset_reference(hr);
  1609. case psym(p)^.owner^.symtabletype of
  1610. localsymtable,inlinelocalsymtable:
  1611. begin
  1612. hr.base:=procinfo^.framepointer;
  1613. hr.offset:=-pvarsym(p)^.address+pvarsym(p)^.owner^.address_fixup;
  1614. end;
  1615. else
  1616. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  1617. end;
  1618. finalize(pvarsym(p)^.vartype.def,hr,false);
  1619. end;
  1620. end;
  1621. { generates the code to make local copies of the value parameters }
  1622. procedure copyvalueparas(p : pnamedindexobject);{$ifndef fpc}far;{$endif}
  1623. var
  1624. href1,href2 : treference;
  1625. r : preference;
  1626. power,len : longint;
  1627. opsize : topsize;
  1628. again,ok : pasmlabel;
  1629. begin
  1630. if (psym(p)^.typ=varsym) and
  1631. (pvarsym(p)^.varspez=vs_value) and
  1632. (push_addr_param(pvarsym(p)^.vartype.def)) then
  1633. begin
  1634. if is_open_array(pvarsym(p)^.vartype.def) or
  1635. is_array_of_const(pvarsym(p)^.vartype.def) then
  1636. begin
  1637. { get stack space }
  1638. new(r);
  1639. reset_reference(r^);
  1640. r^.base:=procinfo^.framepointer;
  1641. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  1642. getexplicitregister32(R_EDI);
  1643. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1644. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1645. if (parraydef(pvarsym(p)^.vartype.def)^.elesize<>1) then
  1646. begin
  1647. if ispowerof2(parraydef(pvarsym(p)^.vartype.def)^.elesize, power) then
  1648. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1649. else
  1650. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1651. parraydef(pvarsym(p)^.vartype.def)^.elesize,R_EDI));
  1652. end;
  1653. {$ifndef NOTARGETWIN32}
  1654. { windows guards only a few pages for stack growing, }
  1655. { so we have to access every page first }
  1656. if target_os.id=os_i386_win32 then
  1657. begin
  1658. getlabel(again);
  1659. getlabel(ok);
  1660. emitlab(again);
  1661. exprasmList.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1662. emitjmp(C_C,ok);
  1663. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1664. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1665. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1666. emitjmp(C_None,again);
  1667. emitlab(ok);
  1668. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1669. ungetregister32(R_EDI);
  1670. { now reload EDI }
  1671. new(r);
  1672. reset_reference(r^);
  1673. r^.base:=procinfo^.framepointer;
  1674. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  1675. getexplicitregister32(R_EDI);
  1676. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1677. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1678. if (parraydef(pvarsym(p)^.vartype.def)^.elesize<>1) then
  1679. begin
  1680. if ispowerof2(parraydef(pvarsym(p)^.vartype.def)^.elesize, power) then
  1681. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1682. else
  1683. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1684. parraydef(pvarsym(p)^.vartype.def)^.elesize,R_EDI));
  1685. end;
  1686. end
  1687. else
  1688. {$endif NOTARGETWIN32}
  1689. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1690. { load destination }
  1691. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1692. { don't destroy the registers! }
  1693. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1694. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1695. { load count }
  1696. new(r);
  1697. reset_reference(r^);
  1698. r^.base:=procinfo^.framepointer;
  1699. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  1700. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ECX));
  1701. { load source }
  1702. new(r);
  1703. reset_reference(r^);
  1704. r^.base:=procinfo^.framepointer;
  1705. r^.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1706. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ESI));
  1707. { scheduled .... }
  1708. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1709. { calculate size }
  1710. len:=parraydef(pvarsym(p)^.vartype.def)^.elesize;
  1711. opsize:=S_B;
  1712. if (len and 3)=0 then
  1713. begin
  1714. opsize:=S_L;
  1715. len:=len shr 2;
  1716. end
  1717. else
  1718. if (len and 1)=0 then
  1719. begin
  1720. opsize:=S_W;
  1721. len:=len shr 1;
  1722. end;
  1723. if ispowerof2(len, power) then
  1724. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1725. else
  1726. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1727. exprasmList.concat(Taicpu.op_none(A_REP,S_NO));
  1728. case opsize of
  1729. S_B : exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1730. S_W : exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1731. S_L : exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1732. end;
  1733. ungetregister32(R_EDI);
  1734. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1735. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1736. { patch the new address }
  1737. new(r);
  1738. reset_reference(r^);
  1739. r^.base:=procinfo^.framepointer;
  1740. r^.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1741. exprasmList.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,r));
  1742. end
  1743. else
  1744. if is_shortstring(pvarsym(p)^.vartype.def) then
  1745. begin
  1746. reset_reference(href1);
  1747. href1.base:=procinfo^.framepointer;
  1748. href1.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1749. reset_reference(href2);
  1750. href2.base:=procinfo^.framepointer;
  1751. href2.offset:=-pvarsym(p)^.localvarsym^.address+pvarsym(p)^.localvarsym^.owner^.address_fixup;
  1752. copyshortstring(href2,href1,pstringdef(pvarsym(p)^.vartype.def)^.len,true,false);
  1753. end
  1754. else
  1755. begin
  1756. reset_reference(href1);
  1757. href1.base:=procinfo^.framepointer;
  1758. href1.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1759. reset_reference(href2);
  1760. href2.base:=procinfo^.framepointer;
  1761. href2.offset:=-pvarsym(p)^.localvarsym^.address+pvarsym(p)^.localvarsym^.owner^.address_fixup;
  1762. concatcopy(href1,href2,pvarsym(p)^.vartype.def^.size,true,true);
  1763. end;
  1764. end;
  1765. end;
  1766. procedure inittempvariables;
  1767. var
  1768. hp : ptemprecord;
  1769. r : preference;
  1770. begin
  1771. hp:=templist;
  1772. while assigned(hp) do
  1773. begin
  1774. if hp^.temptype in [tt_ansistring,tt_freeansistring,tt_interfacecom] then
  1775. begin
  1776. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1777. new(r);
  1778. reset_reference(r^);
  1779. r^.base:=procinfo^.framepointer;
  1780. r^.offset:=hp^.pos;
  1781. emit_const_ref(A_MOV,S_L,0,r);
  1782. end;
  1783. hp:=hp^.next;
  1784. end;
  1785. end;
  1786. procedure finalizetempvariables;
  1787. var
  1788. hp : ptemprecord;
  1789. hr : treference;
  1790. begin
  1791. hp:=templist;
  1792. while assigned(hp) do
  1793. begin
  1794. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1795. begin
  1796. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1797. reset_reference(hr);
  1798. hr.base:=procinfo^.framepointer;
  1799. hr.offset:=hp^.pos;
  1800. emitpushreferenceaddr(hr);
  1801. emitcall('FPC_ANSISTR_DECR_REF');
  1802. end
  1803. else if hp^.temptype=tt_interfacecom 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_INTF_DECR_REF');
  1811. end;
  1812. hp:=hp^.next;
  1813. end;
  1814. end;
  1815. var
  1816. ls : longint;
  1817. procedure largest_size(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1818. begin
  1819. if (psym(p)^.typ=varsym) and
  1820. (pvarsym(p)^.getvaluesize>ls) then
  1821. ls:=pvarsym(p)^.getvaluesize;
  1822. end;
  1823. procedure alignstack(alist : TAAsmoutput);
  1824. begin
  1825. {$ifdef dummy}
  1826. if (cs_optimize in aktglobalswitches) and
  1827. (aktoptprocessor in [classp5,classp6]) then
  1828. begin
  1829. ls:=0;
  1830. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}largest_size);
  1831. if ls>=8 then
  1832. aList.insert(Taicpu.Op_const_reg(A_AND,S_L,-8,R_ESP));
  1833. end;
  1834. {$endif dummy}
  1835. end;
  1836. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  1837. stackframe:longint;
  1838. var parasize:longint;var nostackframe:boolean;
  1839. inlined : boolean);
  1840. {
  1841. Generates the entry code for a procedure
  1842. }
  1843. var
  1844. hs : string;
  1845. {$ifdef GDB}
  1846. stab_function_name : tai_stab_function_name;
  1847. {$endif GDB}
  1848. hr : preference;
  1849. p : psymtable;
  1850. r : treference;
  1851. oldlist,
  1852. oldexprasmlist : TAAsmoutput;
  1853. again : pasmlabel;
  1854. i : longint;
  1855. tempbuf,tempaddr : treference;
  1856. begin
  1857. oldexprasmlist:=exprasmlist;
  1858. exprasmlist:=alist;
  1859. if (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  1860. begin
  1861. emitinsertcall('FPC_INITIALIZEUNITS');
  1862. oldlist:=exprasmlist;
  1863. exprasmlist:=TAAsmoutput.Create;
  1864. p:=symtablestack;
  1865. while assigned(p) do
  1866. begin
  1867. p^.foreach({$ifndef TP}@{$endif}initialize_threadvar);
  1868. p:=p^.next;
  1869. end;
  1870. oldList.insertlist(exprasmlist);
  1871. exprasmlist.free;
  1872. exprasmlist:=oldlist;
  1873. end;
  1874. {$ifdef GDB}
  1875. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1876. exprasmList.insert(Tai_force_line.Create);
  1877. {$endif GDB}
  1878. { a constructor needs a help procedure }
  1879. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1880. begin
  1881. if is_class(procinfo^._class) then
  1882. begin
  1883. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1884. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1885. emitinsertcall('FPC_NEW_CLASS');
  1886. end
  1887. else if is_object(procinfo^._class) then
  1888. begin
  1889. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1890. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1891. getexplicitregister32(R_EDI);
  1892. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI));
  1893. end
  1894. else
  1895. Internalerror(200006161);
  1896. end;
  1897. { don't load ESI, does the caller }
  1898. { we must do it for local function }
  1899. { that can be called from a foreach }
  1900. { of another object than self !! PM }
  1901. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1902. (lexlevel>normal_function_level) then
  1903. maybe_loadesi;
  1904. { When message method contains self as a parameter,
  1905. we must load it into ESI }
  1906. If (po_containsself in aktprocsym^.definition^.procoptions) then
  1907. begin
  1908. new(hr);
  1909. reset_reference(hr^);
  1910. hr^.offset:=procinfo^.selfpointer_offset;
  1911. hr^.base:=procinfo^.framepointer;
  1912. exprasmList.insert(Taicpu.Op_ref_reg(A_MOV,S_L,hr,R_ESI));
  1913. exprasmList.insert(Tairegalloc.Alloc(R_ESI));
  1914. end;
  1915. { should we save edi,esi,ebx like C ? }
  1916. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  1917. begin
  1918. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1919. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1920. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1921. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1922. end;
  1923. { for the save all registers we can simply use a pusha,popa which
  1924. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1925. if (po_saveregisters in aktprocsym^.definition^.procoptions) then
  1926. begin
  1927. exprasmList.insert(Taicpu.Op_none(A_PUSHA,S_L));
  1928. end;
  1929. { omit stack frame ? }
  1930. if (not inlined) then
  1931. if (procinfo^.framepointer=stack_pointer) then
  1932. begin
  1933. CGMessage(cg_d_stackframe_omited);
  1934. nostackframe:=true;
  1935. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1936. parasize:=0
  1937. else
  1938. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-4;
  1939. if stackframe<>0 then
  1940. exprasmList.insert(Taicpu.op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  1941. end
  1942. else
  1943. begin
  1944. alignstack(alist);
  1945. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1946. parasize:=0
  1947. else
  1948. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-8;
  1949. nostackframe:=false;
  1950. if stackframe<>0 then
  1951. begin
  1952. {$ifdef unused}
  1953. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  1954. begin
  1955. if (cs_check_stack in aktlocalswitches) and
  1956. not(target_info.target in [target_386_freebsd,
  1957. target_i386_linux,target_i386_win32]) then
  1958. begin
  1959. emitinsertcall('FPC_STACKCHECK');
  1960. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  1961. end;
  1962. if cs_profile in aktmoduleswitches then
  1963. genprofilecode;
  1964. { %edi is already saved when pocdecl is used
  1965. if ((target_info.target=target_linux) or (target_info.target=target_freebsd)) and
  1966. ((aktprocsym^.definition^.options and poexports)<>0) then
  1967. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI)); }
  1968. { ATTENTION:
  1969. never use ENTER in linux !!! (or freebsd MvdV)
  1970. the stack page fault does not support it PM }
  1971. exprasmList.insert(Taicpu.Op_const_const(A_ENTER,S_NO,stackframe,0)))
  1972. end
  1973. else
  1974. {$endif unused}
  1975. begin
  1976. { windows guards only a few pages for stack growing, }
  1977. { so we have to access every page first }
  1978. if (target_os.id=os_i386_win32) and
  1979. (stackframe>=winstackpagesize) then
  1980. begin
  1981. if stackframe div winstackpagesize<=5 then
  1982. begin
  1983. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe-4,R_ESP));
  1984. for i:=1 to stackframe div winstackpagesize do
  1985. begin
  1986. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  1987. exprasmList.concat(Taicpu.op_const_ref(A_MOV,S_L,0,hr));
  1988. end;
  1989. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1990. end
  1991. else
  1992. begin
  1993. getlabel(again);
  1994. getexplicitregister32(R_EDI);
  1995. exprasmList.concat(Taicpu.op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI));
  1996. emitlab(again);
  1997. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1998. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1999. exprasmList.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  2000. emitjmp(C_NZ,again);
  2001. ungetregister32(R_EDI);
  2002. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP));
  2003. end
  2004. end
  2005. else
  2006. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  2007. if (cs_check_stack in aktlocalswitches) and
  2008. not(target_info.target in [target_i386_freebsd,
  2009. target_i386_linux,target_i386_win32]) then
  2010. begin
  2011. emitinsertcall('FPC_STACKCHECK');
  2012. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  2013. end;
  2014. if cs_profile in aktmoduleswitches then
  2015. genprofilecode;
  2016. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2017. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2018. end;
  2019. end { endif stackframe <> 0 }
  2020. else
  2021. begin
  2022. if cs_profile in aktmoduleswitches then
  2023. genprofilecode;
  2024. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2025. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2026. end;
  2027. end;
  2028. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  2029. generate_interrupt_stackframe_entry;
  2030. { initialize return value }
  2031. if (procinfo^.returntype.def<>pdef(voiddef)) and
  2032. (procinfo^.returntype.def^.needs_inittable) then
  2033. begin
  2034. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2035. reset_reference(r);
  2036. r.offset:=procinfo^.return_offset;
  2037. r.base:=procinfo^.framepointer;
  2038. initialize(procinfo^.returntype.def,r,ret_in_param(procinfo^.returntype.def));
  2039. end;
  2040. { initialisize local data like ansistrings }
  2041. case aktprocsym^.definition^.proctypeoption of
  2042. potype_unitinit:
  2043. begin
  2044. { using current_module.globalsymtable is hopefully }
  2045. { more robust than symtablestack and symtablestack^.next }
  2046. psymtable(current_module.globalsymtable)^.foreach({$ifndef TP}@{$endif}initialize_data);
  2047. psymtable(current_module.localsymtable)^.foreach({$ifndef TP}@{$endif}initialize_data);
  2048. end;
  2049. { units have seperate code for initilization and finalization }
  2050. potype_unitfinalize: ;
  2051. else
  2052. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}initialize_data);
  2053. end;
  2054. { initialisizes temp. ansi/wide string data }
  2055. inittempvariables;
  2056. { generate copies of call by value parameters }
  2057. if not(po_assembler in aktprocsym^.definition^.procoptions) and
  2058. (([pocall_cdecl,pocall_cppdecl]*aktprocsym^.definition^.proccalloptions)=[]) then
  2059. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}copyvalueparas);
  2060. if assigned( aktprocsym^.definition^.parast) then
  2061. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}init_paras);
  2062. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2063. if not inlined and
  2064. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2065. { but it's useless in init/final code of units }
  2066. not(aktprocsym^.definition^.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2067. begin
  2068. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2069. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,36,R_ESP));
  2070. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  2071. reset_reference(tempaddr);
  2072. tempaddr.base:=R_EDI;
  2073. emitpushreferenceaddr(tempaddr);
  2074. reset_reference(tempbuf);
  2075. tempbuf.base:=R_EDI;
  2076. tempbuf.offset:=12;
  2077. emitpushreferenceaddr(tempbuf);
  2078. { Type of stack-frame must be pushed}
  2079. exprasmList.concat(Taicpu.op_const(A_PUSH,S_L,1));
  2080. emitcall('FPC_PUSHEXCEPTADDR');
  2081. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2082. emitcall('FPC_SETJMP');
  2083. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2084. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2085. emitjmp(C_NE,aktexitlabel);
  2086. { probably we've to reload self here }
  2087. maybe_loadesi;
  2088. end;
  2089. if not inlined then
  2090. begin
  2091. if (cs_profile in aktmoduleswitches) or
  2092. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  2093. (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
  2094. make_global:=true;
  2095. hs:=aktprocsym^.definition^.aliasnames.getfirst;
  2096. {$ifdef GDB}
  2097. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  2098. stab_function_name := Tai_stab_function_name.Create(strpnew(hs));
  2099. {$EndIf GDB}
  2100. while hs<>'' do
  2101. begin
  2102. if make_global then
  2103. exprasmList.insert(Tai_symbol.Createname_global(hs,0))
  2104. else
  2105. exprasmList.insert(Tai_symbol.Createname(hs,0));
  2106. {$ifdef GDB}
  2107. if (cs_debuginfo in aktmoduleswitches) and
  2108. target_os.use_function_relative_addresses then
  2109. exprasmList.insert(Tai_stab_function_name.Create(strpnew(hs)));
  2110. {$endif GDB}
  2111. hs:=aktprocsym^.definition^.aliasnames.getfirst;
  2112. end;
  2113. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2114. aktprocsym^.is_global := True;
  2115. {$ifdef GDB}
  2116. if (cs_debuginfo in aktmoduleswitches) then
  2117. begin
  2118. if target_os.use_function_relative_addresses then
  2119. exprasmList.insert(stab_function_name);
  2120. exprasmList.insert(Tai_stabs.Create(aktprocsym^.stabstring));
  2121. aktprocsym^.isstabwritten:=true;
  2122. end;
  2123. {$endif GDB}
  2124. { Align, gprof uses 16 byte granularity }
  2125. if (cs_profile in aktmoduleswitches) then
  2126. exprasmList.insert(Tai_align.Create_op(16,$90))
  2127. else
  2128. if not(cs_littlesize in aktglobalswitches) then
  2129. exprasmList.insert(Tai_align.Create(16));
  2130. end;
  2131. if inlined then
  2132. load_regvars(exprasmlist,nil);
  2133. exprasmlist:=oldexprasmlist;
  2134. end;
  2135. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2136. var
  2137. hr : preference;
  2138. op : Tasmop;
  2139. s : Topsize;
  2140. begin
  2141. uses_eax:=false;
  2142. uses_edx:=false;
  2143. if procinfo^.returntype.def<>pdef(voiddef) then
  2144. begin
  2145. {if ((procinfo^.flags and pi_operator)<>0) and
  2146. assigned(opsym) then
  2147. procinfo^.funcret_is_valid:=
  2148. procinfo^.funcret_is_valid or (opsym^.refs>0);}
  2149. if (procinfo^.funcret_state<>vs_assigned) and not inlined { and
  2150. ((procinfo^.flags and pi_uses_asm)=0)} then
  2151. CGMessage(sym_w_function_result_not_set);
  2152. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2153. if (procinfo^.returntype.def^.deftype in [orddef,enumdef]) then
  2154. begin
  2155. uses_eax:=true;
  2156. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2157. case procinfo^.returntype.def^.size of
  2158. 8:
  2159. begin
  2160. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2161. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2162. exprasmList.concat(Tairegalloc.Alloc(R_EDX));
  2163. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2164. uses_edx:=true;
  2165. end;
  2166. 4:
  2167. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2168. 2:
  2169. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2170. 1:
  2171. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2172. end;
  2173. end
  2174. else
  2175. if ret_in_acc(procinfo^.returntype.def) then
  2176. begin
  2177. uses_eax:=true;
  2178. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2179. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2180. end
  2181. else
  2182. if (procinfo^.returntype.def^.deftype=floatdef) then
  2183. begin
  2184. floatloadops(pfloatdef(procinfo^.returntype.def)^.typ,op,s);
  2185. exprasmList.concat(Taicpu.Op_ref(op,s,hr));
  2186. end
  2187. else
  2188. dispose(hr);
  2189. end
  2190. end;
  2191. procedure genexitcode(alist : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  2192. var
  2193. {$ifdef GDB}
  2194. mangled_length : longint;
  2195. p : pchar;
  2196. st : string[2];
  2197. {$endif GDB}
  2198. nofinal,okexitlabel,noreraiselabel,nodestroycall : pasmlabel;
  2199. hr : treference;
  2200. uses_eax,uses_edx,uses_esi : boolean;
  2201. oldexprasmlist : TAAsmoutput;
  2202. ai : taicpu;
  2203. pd : pprocdef;
  2204. begin
  2205. oldexprasmlist:=exprasmlist;
  2206. exprasmlist:=alist;
  2207. if aktexitlabel^.is_used then
  2208. exprasmList.insert(Tai_label.Create(aktexitlabel));
  2209. { call the destructor help procedure }
  2210. if (aktprocsym^.definition^.proctypeoption=potype_destructor) and
  2211. assigned(procinfo^._class) then
  2212. begin
  2213. if is_class(procinfo^._class) then
  2214. begin
  2215. emitinsertcall('FPC_DISPOSE_CLASS');
  2216. end
  2217. else if is_object(procinfo^._class) then
  2218. begin
  2219. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2220. getexplicitregister32(R_EDI);
  2221. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI));
  2222. { must the object be finalized ? }
  2223. if procinfo^._class^.needs_inittable then
  2224. begin
  2225. getlabel(nofinal);
  2226. exprasmList.insert(Tai_label.Create(nofinal));
  2227. emitinsertcall('FPC_FINALIZE');
  2228. ungetregister32(R_EDI);
  2229. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  2230. exprasmList.insert(Taicpu.Op_sym(A_PUSH,S_L,procinfo^._class^.get_inittable_label));
  2231. ai:=Taicpu.Op_sym(A_Jcc,S_NO,nofinal);
  2232. ai.SetCondition(C_Z);
  2233. exprasmList.insert(ai);
  2234. reset_reference(hr);
  2235. hr.base:=R_EBP;
  2236. hr.offset:=8;
  2237. exprasmList.insert(Taicpu.Op_const_ref(A_CMP,S_L,0,newreference(hr)));
  2238. end;
  2239. end
  2240. else
  2241. begin
  2242. Internalerror(200006161);
  2243. end;
  2244. end;
  2245. { finalize temporary data }
  2246. finalizetempvariables;
  2247. { finalize local data like ansistrings}
  2248. case aktprocsym^.definition^.proctypeoption of
  2249. potype_unitfinalize:
  2250. begin
  2251. { using current_module.globalsymtable is hopefully }
  2252. { more robust than symtablestack and symtablestack^.next }
  2253. psymtable(current_module.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2254. psymtable(current_module.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2255. end;
  2256. { units have seperate code for initialization and finalization }
  2257. potype_unitinit: ;
  2258. else
  2259. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}finalize_data);
  2260. end;
  2261. { finalize paras data }
  2262. if assigned(aktprocsym^.definition^.parast) then
  2263. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}final_paras);
  2264. { do we need to handle exceptions because of ansi/widestrings ? }
  2265. if not inlined and
  2266. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2267. { but it's useless in init/final code of units }
  2268. not(aktprocsym^.definition^.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2269. begin
  2270. { the exception helper routines modify all registers }
  2271. aktprocsym^.definition^.usedregisters:=$ff;
  2272. getlabel(noreraiselabel);
  2273. emitcall('FPC_POPADDRSTACK');
  2274. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2275. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  2276. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2277. ungetregister32(R_EAX);
  2278. emitjmp(C_E,noreraiselabel);
  2279. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  2280. begin
  2281. if assigned(procinfo^._class) then
  2282. begin
  2283. pd:=procinfo^._class^.searchdestructor;
  2284. if assigned(pd) then
  2285. begin
  2286. getlabel(nodestroycall);
  2287. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2288. procinfo^.selfpointer_offset));
  2289. emitjmp(C_E,nodestroycall);
  2290. if is_class(procinfo^._class) then
  2291. begin
  2292. emit_const(A_PUSH,S_L,1);
  2293. emit_reg(A_PUSH,S_L,R_ESI);
  2294. end
  2295. else if is_object(procinfo^._class) then
  2296. begin
  2297. emit_reg(A_PUSH,S_L,R_ESI);
  2298. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class^.vmt_mangledname));
  2299. end
  2300. else
  2301. begin
  2302. Internalerror(200006161);
  2303. end;
  2304. if (po_virtualmethod in pd^.procoptions) then
  2305. begin
  2306. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2307. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class^.vmtmethodoffset(pd^.extnumber)));
  2308. end
  2309. else
  2310. emitcall(pd^.mangledname);
  2311. { not necessary because the result is never assigned in the
  2312. case of an exception (FK)
  2313. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2314. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2315. }
  2316. emitlab(nodestroycall);
  2317. end;
  2318. end
  2319. end
  2320. else
  2321. { must be the return value finalized before reraising the exception? }
  2322. if (procinfo^.returntype.def<>pdef(voiddef)) and
  2323. (procinfo^.returntype.def^.needs_inittable) and
  2324. ((procinfo^.returntype.def^.deftype<>objectdef) or
  2325. not is_class(procinfo^.returntype.def)) then
  2326. begin
  2327. reset_reference(hr);
  2328. hr.offset:=procinfo^.return_offset;
  2329. hr.base:=procinfo^.framepointer;
  2330. finalize(procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  2331. end;
  2332. emitcall('FPC_RERAISE');
  2333. emitlab(noreraiselabel);
  2334. end;
  2335. { call __EXIT for main program }
  2336. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  2337. begin
  2338. emitcall('FPC_DO_EXIT');
  2339. end;
  2340. { handle return value }
  2341. uses_eax:=false;
  2342. uses_edx:=false;
  2343. uses_esi:=false;
  2344. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  2345. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  2346. handle_return_value(inlined,uses_eax,uses_edx)
  2347. else
  2348. begin
  2349. { successful constructor deletes the zero flag }
  2350. { and returns self in eax }
  2351. { eax must be set to zero if the allocation failed !!! }
  2352. getlabel(okexitlabel);
  2353. emitjmp(C_NONE,okexitlabel);
  2354. emitlab(faillabel);
  2355. if is_class(procinfo^._class) then
  2356. begin
  2357. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2358. emitcall('FPC_HELP_FAIL_CLASS');
  2359. end
  2360. else if is_object(procinfo^._class) then
  2361. begin
  2362. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2363. getexplicitregister32(R_EDI);
  2364. emit_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI);
  2365. emitcall('FPC_HELP_FAIL');
  2366. ungetregister32(R_EDI);
  2367. end
  2368. else
  2369. Internalerror(200006161);
  2370. emitlab(okexitlabel);
  2371. { for classes this is done after the call to }
  2372. { AfterConstruction }
  2373. if is_object(procinfo^._class) then
  2374. begin
  2375. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2376. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2377. uses_eax:=true;
  2378. end;
  2379. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2380. uses_esi:=true;
  2381. end;
  2382. { stabs uses the label also ! }
  2383. if aktexit2label^.is_used or
  2384. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2385. emitlab(aktexit2label);
  2386. { gives problems for long mangled names }
  2387. {List.concat(Tai_symbol.Create(aktprocsym^.definition^.mangledname+'_end'));}
  2388. { should we restore edi ? }
  2389. { for all i386 gcc implementations }
  2390. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  2391. begin
  2392. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2393. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  2394. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  2395. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  2396. { here we could reset R_EBX
  2397. but that is risky because it only works
  2398. if genexitcode is called after genentrycode
  2399. so lets skip this for the moment PM
  2400. aktprocsym^.definition^.usedregisters:=
  2401. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  2402. }
  2403. end;
  2404. { for the save all registers we can simply use a pusha,popa which
  2405. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2406. if (po_saveregisters in aktprocsym^.definition^.procoptions) then
  2407. begin
  2408. if uses_esi then
  2409. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4)));
  2410. if uses_edx then
  2411. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20)));
  2412. if uses_eax then
  2413. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28)));
  2414. exprasmList.concat(Taicpu.Op_none(A_POPA,S_L));
  2415. end;
  2416. if not(nostackframe) then
  2417. begin
  2418. if not inlined then
  2419. exprasmList.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  2420. end
  2421. else
  2422. begin
  2423. if (gettempsize<>0) and not inlined then
  2424. exprasmList.insert(Taicpu.op_const_reg(A_ADD,S_L,gettempsize,R_ESP));
  2425. end;
  2426. { parameters are limited to 65535 bytes because }
  2427. { ret allows only imm16 }
  2428. if (parasize>65535) and not(pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  2429. CGMessage(cg_e_parasize_too_big);
  2430. { at last, the return is generated }
  2431. if not inlined then
  2432. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  2433. begin
  2434. if uses_esi then
  2435. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16)));
  2436. if uses_edx then
  2437. begin
  2438. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2439. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12)));
  2440. end;
  2441. if uses_eax then
  2442. begin
  2443. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2444. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0)));
  2445. end;
  2446. generate_interrupt_stackframe_exit;
  2447. end
  2448. else
  2449. begin
  2450. {Routines with the poclearstack flag set use only a ret.}
  2451. { also routines with parasize=0 }
  2452. if (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  2453. begin
  2454. {$ifndef OLD_C_STACK}
  2455. { complex return values are removed from stack in C code PM }
  2456. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  2457. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,4))
  2458. else
  2459. {$endif not OLD_C_STACK}
  2460. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2461. end
  2462. else if (parasize=0) then
  2463. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO))
  2464. else
  2465. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  2466. end;
  2467. if not inlined then
  2468. exprasmList.concat(Tai_symbol_end.Createname(aktprocsym^.definition^.mangledname));
  2469. {$ifdef GDB}
  2470. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2471. begin
  2472. aktprocsym^.concatstabto(exprasmlist);
  2473. if assigned(procinfo^._class) then
  2474. if (not assigned(procinfo^.parent) or
  2475. not assigned(procinfo^.parent^._class)) then
  2476. begin
  2477. if (po_classmethod in aktprocsym^.definition^.procoptions) or
  2478. ((po_virtualmethod in aktprocsym^.definition^.procoptions) and
  2479. (potype_constructor=aktprocsym^.definition^.proctypeoption)) or
  2480. (po_staticmethod in aktprocsym^.definition^.procoptions) then
  2481. begin
  2482. exprasmList.concat(Tai_stabs.Create(strpnew(
  2483. '"pvmt:p'+pvmtdef^.numberstring+'",'+
  2484. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2485. end
  2486. else
  2487. begin
  2488. if not(is_class(procinfo^._class)) then
  2489. st:='v'
  2490. else
  2491. st:='p';
  2492. exprasmList.concat(Tai_stabs.Create(strpnew(
  2493. '"$t:'+st+procinfo^._class^.numberstring+'",'+
  2494. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2495. end;
  2496. end
  2497. else
  2498. begin
  2499. if not is_class(procinfo^._class) then
  2500. st:='*'
  2501. else
  2502. st:='';
  2503. exprasmList.concat(Tai_stabs.Create(strpnew(
  2504. '"$t:r'+st+procinfo^._class^.numberstring+'",'+
  2505. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI]))));
  2506. end;
  2507. { define calling EBP as pseudo local var PM }
  2508. { this enables test if the function is a local one !! }
  2509. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2510. exprasmList.concat(Tai_stabs.Create(strpnew(
  2511. '"parent_ebp:'+voidpointerdef^.numberstring+'",'+
  2512. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  2513. if (pdef(aktprocsym^.definition^.rettype.def) <> pdef(voiddef)) then
  2514. begin
  2515. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  2516. exprasmList.concat(Tai_stabs.Create(strpnew(
  2517. '"'+aktprocsym^.name+':X*'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2518. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset))))
  2519. else
  2520. exprasmList.concat(Tai_stabs.Create(strpnew(
  2521. '"'+aktprocsym^.name+':X'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2522. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset))));
  2523. if (m_result in aktmodeswitches) then
  2524. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  2525. exprasmList.concat(Tai_stabs.Create(strpnew(
  2526. '"RESULT:X*'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2527. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset))))
  2528. else
  2529. exprasmList.concat(Tai_stabs.Create(strpnew(
  2530. '"RESULT:X'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2531. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset))));
  2532. end;
  2533. mangled_length:=length(aktprocsym^.definition^.mangledname);
  2534. getmem(p,2*mangled_length+50);
  2535. strpcopy(p,'192,0,0,');
  2536. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2537. if (target_os.use_function_relative_addresses) then
  2538. begin
  2539. strpcopy(strend(p),'-');
  2540. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2541. end;
  2542. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2543. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  2544. +aktprocsym^.definition^.mangledname))));
  2545. p[0]:='2';p[1]:='2';p[2]:='4';
  2546. strpcopy(strend(p),'_end');}
  2547. strpcopy(p,'224,0,0,'+aktexit2label^.name);
  2548. if (target_os.use_function_relative_addresses) then
  2549. begin
  2550. strpcopy(strend(p),'-');
  2551. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2552. end;
  2553. exprasmList.concatlist(withdebuglist);
  2554. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2555. { strpnew('224,0,0,'
  2556. +aktprocsym^.definition^.mangledname+'_end'))));}
  2557. freemem(p,2*mangled_length+50);
  2558. end;
  2559. {$endif GDB}
  2560. if inlined then
  2561. cleanup_regvars(exprasmlist);
  2562. exprasmlist:=oldexprasmlist;
  2563. end;
  2564. procedure genimplicitunitfinal(alist : TAAsmoutput);
  2565. begin
  2566. { using current_module.globalsymtable is hopefully }
  2567. { more robust than symtablestack and symtablestack^.next }
  2568. psymtable(current_module.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2569. psymtable(current_module.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2570. exprasmList.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  2571. exprasmList.insert(Tai_symbol.Createname_global(target_os.cprefix+current_module.modulename^+'_finalize',0));
  2572. {$ifdef GDB}
  2573. if (cs_debuginfo in aktmoduleswitches) and
  2574. target_os.use_function_relative_addresses then
  2575. exprasmList.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  2576. {$endif GDB}
  2577. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2578. aList.concatlist(exprasmlist);
  2579. end;
  2580. procedure genimplicitunitinit(alist : TAAsmoutput);
  2581. begin
  2582. { using current_module.globalsymtable is hopefully }
  2583. { more robust than symtablestack and symtablestack^.next }
  2584. psymtable(current_module.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2585. psymtable(current_module.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2586. exprasmList.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  2587. exprasmList.insert(Tai_symbol.Createname_global(target_os.cprefix+current_module.modulename^+'_init',0));
  2588. {$ifdef GDB}
  2589. if (cs_debuginfo in aktmoduleswitches) and
  2590. target_os.use_function_relative_addresses then
  2591. exprasmList.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  2592. {$endif GDB}
  2593. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2594. aList.concatlist(exprasmlist);
  2595. end;
  2596. {$ifdef test_dest_loc}
  2597. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2598. begin
  2599. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2600. begin
  2601. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2602. set_location(p^.location,dest_loc);
  2603. in_dest_loc:=true;
  2604. end
  2605. else
  2606. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2607. begin
  2608. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference)));
  2609. set_location(p^.location,dest_loc);
  2610. in_dest_loc:=true;
  2611. end
  2612. else
  2613. internalerror(20080);
  2614. end;
  2615. {$endif test_dest_loc}
  2616. end.
  2617. {
  2618. $Log$
  2619. Revision 1.17 2001-01-05 17:36:58 florian
  2620. * the info about exception frames is stored now on the stack
  2621. instead on the heap
  2622. Revision 1.16 2000/12/25 00:07:31 peter
  2623. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2624. tlinkedlist objects)
  2625. Revision 1.15 2000/12/05 11:44:32 jonas
  2626. + new integer regvar handling, should be much more efficient
  2627. Revision 1.14 2000/11/29 00:30:43 florian
  2628. * unused units removed from uses clause
  2629. * some changes for widestrings
  2630. Revision 1.13 2000/11/28 00:28:07 pierre
  2631. * stabs fixing
  2632. Revision 1.12 2000/11/22 15:12:06 jonas
  2633. * fixed inline-related problems (partially "merges")
  2634. Revision 1.11 2000/11/17 10:30:24 florian
  2635. * passing interfaces as parameters fixed
  2636. Revision 1.10 2000/11/07 23:40:48 florian
  2637. + AfterConstruction and BeforeDestruction impemented
  2638. Revision 1.9 2000/11/06 23:49:20 florian
  2639. * fixed init_paras call
  2640. Revision 1.8 2000/11/06 23:15:01 peter
  2641. * added copyvaluepara call again
  2642. Revision 1.7 2000/11/04 14:25:23 florian
  2643. + merged Attila's changes for interfaces, not tested yet
  2644. Revision 1.6 2000/10/31 22:02:55 peter
  2645. * symtable splitted, no real code changes
  2646. Revision 1.5 2000/10/24 22:23:04 peter
  2647. * emitcall -> emitinsertcall for profiling (merged)
  2648. Revision 1.4 2000/10/24 12:47:45 jonas
  2649. * allocate registers which hold function result
  2650. Revision 1.3 2000/10/24 08:54:25 michael
  2651. + Extra patch from peter
  2652. Revision 1.2 2000/10/24 07:20:03 pierre
  2653. * fix for bug 1193 (merged)
  2654. Revision 1.1 2000/10/15 09:47:42 peter
  2655. * moved to i386/
  2656. Revision 1.19 2000/10/14 10:14:46 peter
  2657. * moehrendorf oct 2000 rewrite
  2658. Revision 1.18 2000/10/10 14:55:28 jonas
  2659. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2660. Revision 1.17 2000/10/01 19:48:23 peter
  2661. * lot of compile updates for cg11
  2662. Revision 1.16 2000/09/30 16:08:45 peter
  2663. * more cg11 updates
  2664. Revision 1.15 2000/09/24 15:06:12 peter
  2665. * use defines.inc
  2666. Revision 1.14 2000/09/16 12:22:52 peter
  2667. * freebsd support merged
  2668. Revision 1.13 2000/08/27 16:11:49 peter
  2669. * moved some util functions from globals,cobjects to cutils
  2670. * splitted files into finput,fmodule
  2671. Revision 1.12 2000/08/24 19:07:54 peter
  2672. * don't initialize if localvarsym is set because that varsym will
  2673. already be initialized
  2674. * first initialize local data before copy of value para's (merged)
  2675. Revision 1.11 2000/08/19 20:09:33 peter
  2676. * check size after checking openarray in push_value_para (merged)
  2677. Revision 1.10 2000/08/16 13:06:06 florian
  2678. + support of 64 bit integer constants
  2679. Revision 1.9 2000/08/10 18:42:03 peter
  2680. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2681. Revision 1.8 2000/08/07 11:29:40 jonas
  2682. + emit_push_mem_size() which pushes a value in memory of a certain size
  2683. * pushsetelement() and pushvaluepara() use this new procedure, because
  2684. otherwise they could sometimes try to push data past the end of the
  2685. heap, causing a crash
  2686. (merged from fixes branch)
  2687. Revision 1.7 2000/08/03 13:17:25 jonas
  2688. + allow regvars to be used inside inlined procs, which required the
  2689. following changes:
  2690. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2691. * moved all regvar related code to new regvars unit
  2692. + added pregvarinfo type to hcodegen
  2693. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2694. * deallocate the regvars of the caller in secondprocinline before
  2695. inlining the called procedure and reallocate them afterwards
  2696. Revision 1.6 2000/08/02 08:05:04 jonas
  2697. * fixed web bug1087
  2698. * allocate R_ECX explicitely if it's used
  2699. (merged from fixes branch)
  2700. Revision 1.5 2000/07/27 09:25:05 jonas
  2701. * moved locflags2reg() procedure from cg386add to cgai386
  2702. + added locjump2reg() procedure to cgai386
  2703. * fixed internalerror(2002) when the result of a case expression has
  2704. LOC_JUMP
  2705. (all merged from fixes branch)
  2706. Revision 1.4 2000/07/21 15:14:02 jonas
  2707. + added is_addr field for labels, if they are only used for getting the address
  2708. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2709. Revision 1.3 2000/07/13 12:08:25 michael
  2710. + patched to 1.1.0 with former 1.09patch from peter
  2711. Revision 1.2 2000/07/13 11:32:37 michael
  2712. + removed logs
  2713. }