cgobj.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Member of the Free Pascal development team
  5. This unit implements the basic code generator object
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit cgobj;
  20. {$i defines.inc}
  21. interface
  22. uses
  23. cclasses,aasm,symtable,
  24. cpuasm,cpubase,cpuinfo,
  25. cginfo,
  26. symconst,symbase,symtype,node;
  27. type
  28. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  29. tcg = class
  30. scratch_register_array_pointer : aword;
  31. unusedscratchregisters : tregisterset;
  32. alignment : talignment;
  33. {************************************************}
  34. { basic routines }
  35. constructor create;
  36. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  37. { allocates register r by inserting a pai_realloc record }
  38. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  39. { deallocates register r by inserting a pa_regdealloc record}
  40. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  41. { returns a register for use as scratch register }
  42. function get_scratch_reg(list : taasmoutput) : tregister;
  43. { releases a scratch register }
  44. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  45. {************************************************}
  46. { code generation for subroutine entry/exit code }
  47. { initilizes data of type t }
  48. { if is_already_ref is true then the routines assumes }
  49. { that r points to the data to initialize }
  50. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  51. { finalizes data of type t }
  52. { if is_already_ref is true then the routines assumes }
  53. { that r points to the data to finalizes }
  54. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  55. { helper routines }
  56. procedure g_initialize_data(list : taasmoutput;p : tsym);
  57. procedure g_incr_data(list : taasmoutput;p : tsym);
  58. procedure g_finalize_data(list : taasmoutput;p : tnamedindexitem);
  59. procedure g_copyvalueparas(list : taasmoutput;p : tnamedindexitem);
  60. procedure g_finalizetempansistrings(list : taasmoutput);
  61. procedure g_entrycode(alist : TAAsmoutput;make_global:boolean;
  62. stackframe:longint;
  63. var parasize:longint;var nostackframe:boolean;
  64. inlined : boolean);
  65. procedure g_exitcode(list : taasmoutput;parasize : longint;
  66. nostackframe,inlined : boolean);
  67. { string helper routines }
  68. procedure g_decrstrref(list : taasmoutput;const ref : treference;t : tdef);
  69. procedure g_removetemps(list : taasmoutput;p : tlinkedlist);
  70. { passing parameters, per default the parameter is pushed }
  71. { nr gives the number of the parameter (enumerated from }
  72. { left to right), this allows to move the parameter to }
  73. { register, if the cpu supports register calling }
  74. { conventions }
  75. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);virtual; abstract;
  76. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);virtual;
  77. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);virtual;
  78. procedure a_param_loc(list : taasmoutput;const l : tlocation;nr : longint);
  79. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);virtual;
  80. {**********************************}
  81. { these methods must be overriden: }
  82. { Remarks:
  83. * If a method specifies a size you have only to take care
  84. of that number of bits, i.e. load_const_reg with OP_8 must
  85. only load the lower 8 bit of the specified register
  86. the rest of the register can be undefined
  87. if necessary the compiler will call a method
  88. to zero or sign extend the register
  89. * The a_load_XX_XX with OP_64 needn't to be
  90. implemented for 32 bit
  91. processors, the code generator takes care of that
  92. * the addr size is for work with the natural pointer
  93. size
  94. * the procedures without fpu/mm are only for integer usage
  95. * normally the first location is the source and the
  96. second the destination
  97. }
  98. procedure a_call_name(list : taasmoutput;const s : string;
  99. offset : longint);virtual; abstract;
  100. { move instructions }
  101. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  102. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  103. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  104. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  105. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  106. procedure a_load_reg_loc(list : taasmoutput;reg : tregister;const loc: tlocation);
  107. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  108. procedure a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  109. procedure a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  110. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);virtual; abstract;
  111. { fpu move instructions }
  112. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  113. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  114. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  115. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  116. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  117. { vector register move instructions }
  118. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  119. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  120. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  121. { basic arithmetic operations }
  122. { note: for operators which require only one argument (not, neg), use }
  123. { the op_reg_reg, op_reg_reg or op_reg_loc methods and keep in mind }
  124. { that in this case the *second* operand is used as both source and }
  125. { destination (JM) }
  126. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  127. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  128. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  129. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  130. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  131. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  132. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  133. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  134. { trinary operations for processors that support them, 'emulated' }
  135. { on others. None with "ref" arguments since I don't think there }
  136. { are any processors that support it (JM) }
  137. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  138. size: tcgsize; a: aword; src, dst: tregister); virtual;
  139. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  140. size: tcgsize; src1, src2, dst: tregister); virtual;
  141. { comparison operations }
  142. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  143. l : tasmlabel);virtual; abstract;
  144. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  145. l : tasmlabel); virtual;
  146. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  147. l : tasmlabel);
  148. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  149. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  150. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  151. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  152. l : tasmlabel);
  153. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel); virtual; abstract;
  154. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  155. procedure g_flags2reg(list: taasmoutput; const f: tresflags; reg: TRegister); virtual; abstract;
  156. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  157. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  158. { restores the frame pointer at procedure exit, for the }
  159. { i386 it generates a simple leave }
  160. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  161. { some processors like the PPC doesn't allow to change the stack in }
  162. { a procedure, so we need to maintain an extra stack for the }
  163. { result values of setjmp in exception code }
  164. { this two procedures are for pushing an exception value, }
  165. { they can use the scratch registers }
  166. procedure g_push_exception_value_reg(list : taasmoutput;reg : tregister);virtual; abstract;
  167. procedure g_push_exception_value_const(list : taasmoutput;reg : tregister);virtual; abstract;
  168. { that procedure pops a exception value }
  169. procedure g_pop_exception_value_reg(list : taasmoutput;reg : tregister);virtual; abstract;
  170. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  171. {********************************************************}
  172. { these methods can be overriden for extra functionality }
  173. { the following methods do nothing: }
  174. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  175. procedure g_interrupt_stackframe_exit(list : taasmoutput);virtual;
  176. procedure g_profilecode(list : taasmoutput);virtual;
  177. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  178. procedure g_maybe_loadself(list : taasmoutput);virtual; abstract;
  179. { copies len bytes from the source to destination, if }
  180. { loadref is true, it assumes that it first must load }
  181. { the source address from the memory location where }
  182. { source points to }
  183. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  184. { generates rangechecking code for a node }
  185. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  186. const todef: tdef); virtual;
  187. { returns the tcgsize corresponding with the size of reg }
  188. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  189. {$ifdef i386}
  190. { this one is only necessary due the the restrictions of the 80x86, }
  191. { so make it a special case (JM) }
  192. function makeregsize(var reg: tregister; size: tcgsize): topsize; virtual; abstract;
  193. {$endif i386}
  194. end;
  195. var
  196. cg : tcg; { this is the main code generator class }
  197. implementation
  198. uses
  199. globals,globtype,options,systems,cgbase,
  200. verbose,types,tgobj,symdef,cga,tainst,rgobj;
  201. const
  202. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  203. {*****************************************************************************
  204. basic functionallity
  205. ******************************************************************************}
  206. constructor tcg.create;
  207. var
  208. i : longint;
  209. begin
  210. scratch_register_array_pointer:=1;
  211. for i:=low(scratch_regs) to high(scratch_regs) do
  212. include(unusedscratchregisters,scratch_regs[i]);
  213. end;
  214. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  215. begin
  216. list.concat(tairegalloc.alloc(r));
  217. end;
  218. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  219. begin
  220. list.concat(tairegalloc.dealloc(r));
  221. end;
  222. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  223. begin
  224. list.concat(tai_label.create(l));
  225. end;
  226. function tcg.get_scratch_reg(list : taasmoutput) : tregister;
  227. var
  228. r : tregister;
  229. i : longint;
  230. begin
  231. if unusedscratchregisters=[] then
  232. internalerror(68996);
  233. for i:=scratch_register_array_pointer to
  234. (scratch_register_array_pointer+max_scratch_regs-1) do
  235. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  236. begin
  237. r:=scratch_regs[(i mod max_scratch_regs)+1];
  238. break;
  239. end;
  240. exclude(unusedscratchregisters,r);
  241. inc(scratch_register_array_pointer);
  242. if scratch_register_array_pointer>max_scratch_regs then
  243. scratch_register_array_pointer:=1;
  244. a_reg_alloc(list,r);
  245. get_scratch_reg:=r;
  246. end;
  247. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  248. begin
  249. {$ifdef i386}
  250. include(unusedscratchregisters,makereg32(r));
  251. {$else i386}
  252. include(unusedscratchregisters,r);
  253. {$endif i386}
  254. a_reg_dealloc(list,r);
  255. end;
  256. {*****************************************************************************
  257. this methods must be overridden for extra functionality
  258. ******************************************************************************}
  259. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  260. begin
  261. end;
  262. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput);
  263. begin
  264. end;
  265. procedure tcg.g_profilecode(list : taasmoutput);
  266. begin
  267. end;
  268. {*****************************************************************************
  269. for better code generation these methods should be overridden
  270. ******************************************************************************}
  271. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);
  272. var
  273. hr : tregister;
  274. begin
  275. hr:=get_scratch_reg(list);
  276. a_load_const_reg(list,size,a,hr);
  277. a_param_reg(list,size,hr,nr);
  278. free_scratch_reg(list,hr);
  279. end;
  280. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  281. var
  282. hr : tregister;
  283. begin
  284. hr:=get_scratch_reg(list);
  285. a_load_ref_reg(list,size,r,hr);
  286. a_param_reg(list,size,hr,nr);
  287. free_scratch_reg(list,hr);
  288. end;
  289. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;nr : longint);
  290. begin
  291. case l.loc of
  292. LOC_REGISTER,
  293. LOC_CREGISTER :
  294. a_param_reg(list,l.size,l.register,nr);
  295. LOC_CONSTANT :
  296. a_param_const(list,l.size,l.value,nr);
  297. LOC_CREFERENCE,
  298. LOC_REFERENCE :
  299. a_param_ref(list,l.size,l.reference,nr);
  300. else
  301. internalerror(2002032211);
  302. end;
  303. end;
  304. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  305. var
  306. hr : tregister;
  307. begin
  308. hr:=get_scratch_reg(list);
  309. a_loadaddr_ref_reg(list,r,hr);
  310. a_param_reg(list,OS_ADDR,hr,nr);
  311. free_scratch_reg(list,hr);
  312. end;
  313. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  314. begin
  315. a_param_const(list,OS_32,stackframesize,1);
  316. a_call_name(list,'FPC_STACKCHECK',0);
  317. end;
  318. {*****************************************************************************
  319. String helper routines
  320. *****************************************************************************}
  321. procedure tcg.g_removetemps(list : taasmoutput;p : tlinkedlist);
  322. (*
  323. var
  324. hp : ptemptodestroy;
  325. pushedregs : tpushed;
  326. *)
  327. begin
  328. (*
  329. hp:=ptemptodestroy(p^.first);
  330. if not(assigned(hp)) then
  331. exit;
  332. tg.pushusedregisters(pushedregs,$ff);
  333. while assigned(hp) do
  334. begin
  335. if is_ansistring(hp^.typ) then
  336. begin
  337. g_decrstrref(list,hp^.address,hp^.typ);
  338. tg.ungetiftemp(hp^.address);
  339. end;
  340. hp:=ptemptodestroy(hp^.next);
  341. end;
  342. tg.popusedregisters(pushedregs);
  343. *)
  344. runerror(211);
  345. end;
  346. procedure tcg.g_decrstrref(list : taasmoutput;const ref : treference;t : tdef);
  347. { var
  348. pushedregs : tpushedsaved; }
  349. begin
  350. (*
  351. tg.pushusedregisters(pushedregs,$ff);
  352. a_param_ref_addr(list,ref,1);
  353. if is_ansistring(t) then
  354. a_call_name(list,'FPC_ANSISTR_DECR_REF',0)
  355. else if is_widestring(t) then
  356. a_call_name(list,'FPC_WIDESTR_DECR_REF',0)
  357. else internalerror(58993);
  358. tg.popusedregisters(pushedregs);
  359. *)
  360. runerror(211);
  361. end;
  362. {*****************************************************************************
  363. Code generation for subroutine entry- and exit code
  364. *****************************************************************************}
  365. { initilizes data of type t }
  366. { if is_already_ref is true then the routines assumes }
  367. { that r points to the data to initialize }
  368. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  369. { var
  370. hr : treference; }
  371. begin
  372. (*
  373. if is_ansistring(t) or
  374. is_widestring(t) then
  375. a_load_const_ref(list,OS_8,0,ref)
  376. else
  377. begin
  378. reset_reference(hr);
  379. hr.symbol:=t^.get_inittable_label;
  380. a_param_ref_addr(list,hr,2);
  381. if is_already_ref then
  382. a_param_ref(list,OS_ADDR,ref,1)
  383. else
  384. a_param_ref_addr(list,ref,1);
  385. a_call_name(list,'FPC_INITIALIZE',0);
  386. end;
  387. *)
  388. runerror(211);
  389. end;
  390. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  391. { var
  392. r : treference; }
  393. begin
  394. (*
  395. if is_ansistring(t) or
  396. is_widestring(t) then
  397. begin
  398. g_decrstrref(list,ref,t);
  399. end
  400. else
  401. begin
  402. reset_reference(r);
  403. r.symbol:=t^.get_inittable_label;
  404. a_param_ref_addr(list,r,2);
  405. if is_already_ref then
  406. a_paramaddr_ref(list,ref,1)
  407. else
  408. a_param_ref_addr(list,ref,1);
  409. a_call_name(list,'FPC_FINALIZE',0);
  410. end;
  411. *)
  412. runerror(211);
  413. end;
  414. { generates the code for initialisation of local data }
  415. procedure tcg.g_initialize_data(list : taasmoutput;p : tsym);
  416. { var
  417. hr : treference; }
  418. begin
  419. (*
  420. if (tsym(p)^.typ=varsym) and
  421. assigned(pvarsym(p)^.vartype.def) and
  422. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  423. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  424. pvarsym(p)^.vartype.def^.needs_inittable then
  425. begin
  426. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  427. reset_reference(hr);
  428. if tsym(p)^.owner^.symtabletype=localsymtable then
  429. begin
  430. hr.base:=procinfo^.framepointer;
  431. hr.offset:=-pvarsym(p)^.address;
  432. end
  433. else
  434. begin
  435. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  436. end;
  437. g_initialize(list,pvarsym(p)^.vartype.def,hr,false);
  438. end;
  439. *)
  440. runerror(211);
  441. end;
  442. { generates the code for incrementing the reference count of parameters }
  443. procedure tcg.g_incr_data(list : taasmoutput;p : tsym);
  444. { var
  445. hr : treference; }
  446. begin
  447. (*
  448. if (tsym(p)^.typ=varsym) and
  449. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  450. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  451. pvarsym(p)^.vartype.def^.needs_inittable and
  452. ((pvarsym(p)^.varspez=vs_value)) then
  453. begin
  454. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  455. reset_reference(hr);
  456. hr.symbol:=pvarsym(p)^.vartype.def^.get_inittable_label;
  457. a_param_ref_addr(list,hr,2);
  458. reset_reference(hr);
  459. hr.base:=procinfo^.framepointer;
  460. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  461. a_param_ref_addr(list,hr,1);
  462. reset_reference(hr);
  463. a_call_name(list,'FPC_ADDREF',0);
  464. end;
  465. *)
  466. runerror(211);
  467. end;
  468. { generates the code for finalisation of local data }
  469. procedure tcg.g_finalize_data(list : taasmoutput;p : tnamedindexitem);
  470. { var
  471. hr : treference; }
  472. begin
  473. (*
  474. if (tsym(p)^.typ=varsym) and
  475. assigned(pvarsym(p)^.vartype.def) and
  476. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  477. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  478. pvarsym(p)^.vartype.def^.needs_inittable then
  479. begin
  480. { not all kind of parameters need to be finalized }
  481. if (tsym(p)^.owner^.symtabletype=parasymtable) and
  482. ((pvarsym(p)^.varspez=vs_var) or
  483. (pvarsym(p)^.varspez=vs_const) { and
  484. (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
  485. exit;
  486. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  487. reset_reference(hr);
  488. case tsym(p)^.owner^.symtabletype of
  489. localsymtable:
  490. begin
  491. hr.base:=procinfo^.framepointer;
  492. hr.offset:=-pvarsym(p)^.address;
  493. end;
  494. parasymtable:
  495. begin
  496. hr.base:=procinfo^.framepointer;
  497. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  498. end;
  499. else
  500. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  501. end;
  502. g_finalize(list,pvarsym(p)^.vartype.def,hr,false);
  503. end;
  504. *)
  505. runerror(211);
  506. end;
  507. { generates the code to make local copies of the value parameters }
  508. procedure tcg.g_copyvalueparas(list : taasmoutput;p : tnamedindexitem);
  509. begin
  510. runerror(255);
  511. end;
  512. (*
  513. var
  514. _list : taasmoutput;
  515. { wrappers for the methods, because TP doesn't know procedures }
  516. { of objects }
  517. procedure _copyvalueparas(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  518. begin
  519. cg^.g_copyvalueparas(_list,s);
  520. end;
  521. *)
  522. procedure tcg.g_finalizetempansistrings(list : taasmoutput);
  523. (*
  524. var
  525. hp : ptemprecord;
  526. hr : treference;
  527. *)
  528. begin
  529. (*
  530. hp:=tg.templist;
  531. while assigned(hp) do
  532. begin
  533. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  534. begin
  535. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  536. reset_reference(hr);
  537. hr.base:=procinfo^.framepointer;
  538. hr.offset:=hp^.pos;
  539. a_param_ref_addr(list,hr,1);
  540. a_call_name(list,'FPC_ANSISTR_DECR_REF',0);
  541. end;
  542. hp:=hp^.next;
  543. end;
  544. *)
  545. runerror(211);
  546. end;
  547. (*
  548. procedure _finalize_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  549. begin
  550. cg^.g_finalize_data(_list,s);
  551. end;
  552. procedure _incr_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  553. begin
  554. cg^.g_incr_data(_list,tsym(s));
  555. end;
  556. procedure _initialize_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  557. begin
  558. cg^.g_initialize_data(_list,tsym(s));
  559. end;
  560. *)
  561. { generates the entry code for a procedure }
  562. procedure tcg.g_entrycode(alist : TAAsmoutput;make_global:boolean;
  563. stackframe:longint;
  564. var parasize:longint;var nostackframe:boolean;
  565. inlined : boolean);
  566. (*
  567. var
  568. hs : string;
  569. hp : pused_unit;
  570. initcode : taasmoutput;
  571. {$ifdef GDB}
  572. stab_function_name : Pai_stab_function_name;
  573. {$endif GDB}
  574. hr : treference;
  575. r : tregister;
  576. *)
  577. begin
  578. (*
  579. { Align }
  580. if (not inlined) then
  581. begin
  582. { gprof uses 16 byte granularity !! }
  583. if (cs_profile in aktmoduleswitches) then
  584. list^.insert(new(pai_align,init(16)))
  585. else
  586. if not(cs_littlesize in aktglobalswitches) then
  587. list^.insert(new(pai_align,init(4)));
  588. end;
  589. { save registers on cdecl }
  590. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  591. begin
  592. for r:=firstreg to lastreg do
  593. begin
  594. if (r in registers_saved_on_cdecl) then
  595. if (r in (tg.availabletempregsint+
  596. tg.availabletempregsfpu+
  597. tg.availabletempregsmm)) then
  598. begin
  599. if not(r in tg.usedinproc) then
  600. {!!!!!!!!!!!! a_push_reg(list,r) }
  601. end
  602. else
  603. {!!!!!!!! a_push_reg(list,r) };
  604. end;
  605. end;
  606. { omit stack frame ? }
  607. if not inlined then
  608. if procinfo^.framepointer=stack_pointer then
  609. begin
  610. CGMessage(cg_d_stackframe_omited);
  611. nostackframe:=true;
  612. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  613. parasize:=0
  614. else
  615. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-pointersize;
  616. end
  617. else
  618. begin
  619. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  620. parasize:=0
  621. else
  622. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-pointersize*2;
  623. nostackframe:=false;
  624. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  625. g_interrupt_stackframe_entry(list);
  626. g_stackframe_entry(list,stackframe);
  627. if (cs_check_stack in aktlocalswitches) and
  628. (tf_supports_stack_checking in target_info.flags) then
  629. g_stackcheck(@initcode,stackframe);
  630. end;
  631. if cs_profile in aktmoduleswitches then
  632. g_profilecode(@initcode);
  633. if (not inlined) and (aktprocsym^.definition^.proctypeoption in [potype_unitinit]) then
  634. begin
  635. { needs the target a console flags ? }
  636. if tf_needs_isconsole in target_info.flags then
  637. begin
  638. hr.symbol:=newasmsymbol('U_'+target_info.system_unit+'_ISCONSOLE');
  639. if apptype=at_cui then
  640. a_load_const_ref(list,OS_8,1,hr)
  641. else
  642. a_load_const_ref(list,OS_8,0,hr);
  643. dispose(hr.symbol,done);
  644. end;
  645. hp:=pused_unit(usedunits.first);
  646. while assigned(hp) do
  647. begin
  648. { call the unit init code and make it external }
  649. if (hp^.u^.flags and uf_init)<>0 then
  650. a_call_name(list,
  651. 'INIT$$'+hp^.u^.modulename^,0);
  652. hp:=Pused_unit(hp^.next);
  653. end;
  654. end;
  655. {$ifdef dummy}
  656. { a constructor needs a help procedure }
  657. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  658. begin
  659. if procinfo^._class^.isclass then
  660. begin
  661. list^.concat(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol('FPC_NEW_CLASS'))));
  662. list^.concat(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  663. end
  664. else
  665. begin
  666. {
  667. list^.insert(new(pai_labeled,init(A_JZ,quickexitlabel)));
  668. list^.insert(new(paicpu,op_csymbol(A_CALL,S_NO,
  669. newcsymbol('FPC_HELP_CONSTRUCTOR',0))));
  670. list^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  671. concat_external('FPC_HELP_CONSTRUCTOR',EXT_NEAR);
  672. }
  673. end;
  674. end;
  675. {$endif dummy}
  676. {$ifdef GDB}
  677. if (cs_debuginfo in aktmoduleswitches) then
  678. list^.insert(new(pai_force_line,init));
  679. {$endif GDB}
  680. { initialize return value }
  681. if assigned(procinfo^.returntype.def) and
  682. is_ansistring(procinfo^.returntype.def) or
  683. is_widestring(procinfo^.returntype.def) then
  684. begin
  685. reset_reference(hr);
  686. hr.offset:=procinfo^.return_offset;
  687. hr.base:=procinfo^.framepointer;
  688. a_load_const_ref(list,OS_32,0,hr);
  689. end;
  690. _list:=list;
  691. { generate copies of call by value parameters }
  692. if (po_assembler in aktprocsym^.definition^.procoptions) then
  693. aktprocsym^.definition^.parast^.foreach({$ifdef FPCPROCVAR}@{$endif}_copyvalueparas);
  694. { initialisizes local data }
  695. aktprocsym^.definition^.localst^.foreach({$ifdef FPCPROCVAR}@{$endif}_initialize_data);
  696. { add a reference to all call by value/const parameters }
  697. aktprocsym^.definition^.parast^.foreach({$ifdef FPCPROCVAR}@{$endif}_incr_data);
  698. if (cs_profile in aktmoduleswitches) or
  699. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  700. (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
  701. make_global:=true;
  702. if not inlined then
  703. begin
  704. hs:=proc_names.get;
  705. {$ifdef GDB}
  706. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  707. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  708. {$endif GDB}
  709. { insert the names for the procedure }
  710. while hs<>'' do
  711. begin
  712. if make_global then
  713. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  714. else
  715. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  716. {$ifdef GDB}
  717. if (cs_debuginfo in aktmoduleswitches) then
  718. begin
  719. if target_os.use_function_relative_addresses then
  720. list^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  721. end;
  722. {$endif GDB}
  723. hs:=proc_names.get;
  724. end;
  725. end;
  726. {$ifdef GDB}
  727. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  728. begin
  729. if target_os.use_function_relative_addresses then
  730. list^.insert(stab_function_name);
  731. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  732. aktprocsym^.is_global := True;
  733. list^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  734. aktprocsym^.isstabwritten:=true;
  735. end;
  736. {$endif GDB}
  737. *)
  738. runerror(211);
  739. end;
  740. procedure tcg.g_exitcode(list : taasmoutput;parasize:longint;nostackframe,inlined:boolean);
  741. (*
  742. var
  743. {$ifdef GDB}
  744. mangled_length : longint;
  745. p : pchar;
  746. {$endif GDB}
  747. nofinal,noreraiselabel : tasmlabel;
  748. hr : treference;
  749. r : tregister;
  750. *)
  751. begin
  752. (*
  753. if aktexitlabel^.is_used then
  754. list^.insert(new(pai_label,init(aktexitlabel)));
  755. { call the destructor help procedure }
  756. if (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  757. begin
  758. if procinfo^._class^.is_class then
  759. a_call_name(list,'FPC_DISPOSE_CLASS',0)
  760. else
  761. begin
  762. if procinfo^._class^.needs_inittable then
  763. begin
  764. getlabel(nofinal);
  765. {!!!!!!!!!!
  766. reset_reference(hr);
  767. hr.base:=R_EBP;
  768. hr.offset:=8;
  769. a_cmp_reg_const_label(list,OS_ADDR,OZ_EQ,
  770. }
  771. reset_reference(hr);
  772. hr.symbol:=procinfo^._class^.get_inittable_label;
  773. a_paramaddr_ref(list,hr,2);
  774. a_param_reg(list,OS_ADDR,self_pointer,1);
  775. a_call_name(list,'FPC_FINALIZE',0);
  776. a_label(list,nofinal);
  777. end;
  778. { vmt_offset_reg can be a scratch register, }
  779. { but it must be always the same }
  780. a_reg_alloc(list,vmt_offset_reg);
  781. a_load_const_reg(list,OS_32,procinfo^._class^.vmt_offset,vmt_offset_reg);
  782. a_call_name(list,'FPC_HELP_DESTRUCTOR',0);
  783. a_reg_dealloc(list,vmt_offset_reg);
  784. end;
  785. end;
  786. { finalize temporary data }
  787. g_finalizetempansistrings(list);
  788. _list:=list;
  789. { finalize local data }
  790. aktprocsym^.definition^.localst^.foreach({$ifdef FPCPROCVAR}@{$endif}_finalize_data);
  791. { finalize paras data }
  792. if assigned(aktprocsym^.definition^.parast) then
  793. aktprocsym^.definition^.parast^.foreach({$ifdef FPCPROCVAR}@{$endif}_finalize_data);
  794. { do we need to handle exceptions because of ansi/widestrings ? }
  795. if (procinfo^.flags and pi_needs_implicit_finally)<>0 then
  796. begin
  797. getlabel(noreraiselabel);
  798. a_call_name(list,'FPC_POPADDRSTACK',0);
  799. a_reg_alloc(list,accumulator);
  800. g_pop_exception_value_reg(list,accumulator);
  801. a_cmp_const_reg_label(list,OS_32,OC_EQ,0,accumulator,noreraiselabel);
  802. a_reg_dealloc(list,accumulator);
  803. { must be the return value finalized before reraising the exception? }
  804. if (procinfo^.returntype.def<>tdef(voiddef)) and
  805. (procinfo^.returntype.def^.needs_inittable) and
  806. ((procinfo^.returntype.def^.deftype<>objectdef) or
  807. not(pobjectdef(procinfo^.returntype.def)^.is_class)) then
  808. begin
  809. reset_reference(hr);
  810. hr.offset:=procinfo^.return_offset;
  811. hr.base:=procinfo^.framepointer;
  812. g_finalize(list,procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  813. end;
  814. a_call_name(list,'FPC_RERAISE',0);
  815. a_label(list,noreraiselabel);
  816. end;
  817. { call __EXIT for main program }
  818. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  819. a_call_name(list,'FPC_DO_EXIT',0);
  820. { handle return value }
  821. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  822. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  823. { handle_return_value(inlined) }
  824. else
  825. begin
  826. { return self in EAX }
  827. a_label(list,quickexitlabel);
  828. a_reg_alloc(list,accumulator);
  829. a_load_reg_reg(list,OS_ADDR,self_pointer,accumulator);
  830. a_reg_dealloc(list,self_pointer);
  831. a_label(list,quickexitlabel);
  832. { we can't clear the zero flag because the Alpha }
  833. { for example doesn't have flags, we have to compare }
  834. { the accu. in the caller }
  835. end;
  836. { stabs uses the label also ! }
  837. if aktexit2label^.is_used or
  838. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  839. a_label(list,aktexit2label);
  840. {$ifdef dummy}
  841. { should we restore edi ? }
  842. { for all i386 gcc implementations }
  843. {!!!!!!!!!!! I don't know how to handle register saving yet }
  844. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  845. begin
  846. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  847. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  848. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  849. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  850. { here we could reset R_EBX
  851. but that is risky because it only works
  852. if genexitcode is called after genentrycode
  853. so lets skip this for the moment PM
  854. aktprocsym^.definition^.usedregisters:=
  855. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  856. }
  857. end;
  858. {$endif dummy}
  859. if not(nostackframe) and not inlined then
  860. g_restore_frame_pointer(list);
  861. { at last, the return is generated }
  862. if not inlined then
  863. if po_interrupt in aktprocsym^.definition^.procoptions then
  864. g_interrupt_stackframe_exit(list)
  865. else
  866. g_return_from_proc(list,parasize);
  867. list^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  868. {$ifdef GDB}
  869. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  870. begin
  871. aktprocsym^.concatstabto(list);
  872. if assigned(procinfo^._class) then
  873. if (not assigned(procinfo^.parent) or
  874. not assigned(procinfo^.parent^._class)) then
  875. list^.concat(new(pai_stabs,init(strpnew(
  876. '"$t:v'+procinfo^._class^.numberstring+'",'+
  877. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))));
  878. {!!!!!!!!!!!!
  879. else
  880. list^.concat(new(pai_stabs,init(strpnew(
  881. '"$t:r'+procinfo^._class^.numberstring+'",'+
  882. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  883. }
  884. if (tdef(aktprocsym^.definition^.rettype.def) <> tdef(voiddef)) then
  885. begin
  886. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  887. list^.concat(new(pai_stabs,init(strpnew(
  888. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  889. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  890. else
  891. list^.concat(new(pai_stabs,init(strpnew(
  892. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  893. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  894. if (m_result in aktmodeswitches) then
  895. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  896. list^.concat(new(pai_stabs,init(strpnew(
  897. '"RESULT:X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  898. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  899. else
  900. list^.concat(new(pai_stabs,init(strpnew(
  901. '"RESULT:X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  902. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  903. end;
  904. mangled_length:=length(aktprocsym^.definition^.mangledname);
  905. getmem(p,mangled_length+50);
  906. strpcopy(p,'192,0,0,');
  907. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  908. list^.concat(new(pai_stabn,init(strnew(p))));
  909. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  910. +aktprocsym^.definition^.mangledname))));
  911. p[0]:='2';p[1]:='2';p[2]:='4';
  912. strpcopy(strend(p),'_end');}
  913. freemem(p,mangled_length+50);
  914. list^.concat(new(pai_stabn,init(
  915. strpnew('224,0,0,'+aktexit2label^.name))));
  916. { strpnew('224,0,0,'
  917. +aktprocsym^.definition^.mangledname+'_end'))));}
  918. end;
  919. {$endif GDB}
  920. *)
  921. runerror(211);
  922. end;
  923. {*****************************************************************************
  924. some generic implementations
  925. ****************************************************************************}
  926. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  927. var
  928. tmpreg: tregister;
  929. begin
  930. tmpreg := get_scratch_reg(list);
  931. a_load_const_reg(list,size,a,tmpreg);
  932. a_load_reg_ref(list,size,tmpreg,ref);
  933. free_scratch_reg(list,tmpreg);
  934. end;
  935. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  936. begin
  937. case loc.loc of
  938. LOC_REFERENCE,LOC_CREFERENCE:
  939. a_load_ref_reg(list,loc.size,loc.reference,reg);
  940. LOC_REGISTER,LOC_CREGISTER:
  941. a_load_reg_reg(list,loc.size,loc.register,reg);
  942. LOC_CONSTANT:
  943. a_load_const_reg(list,loc.size,loc.value,reg);
  944. else
  945. internalerror(200109092);
  946. end;
  947. end;
  948. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  949. begin
  950. case loc.loc of
  951. LOC_REFERENCE,LOC_CREFERENCE:
  952. a_load_const_ref(list,loc.size,a,loc.reference);
  953. LOC_REGISTER,LOC_CREGISTER:
  954. a_load_const_reg(list,loc.size,a,loc.register);
  955. else
  956. internalerror(200203272);
  957. end;
  958. end;
  959. procedure tcg.a_load_reg_loc(list : taasmoutput;reg : tregister;const loc: tlocation);
  960. begin
  961. case loc.loc of
  962. LOC_REFERENCE,LOC_CREFERENCE:
  963. a_load_reg_ref(list,loc.size,reg,loc.reference);
  964. LOC_REGISTER,LOC_CREGISTER:
  965. a_load_reg_reg(list,loc.size,reg,loc.register);
  966. else
  967. internalerror(200203271);
  968. end;
  969. end;
  970. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  971. var
  972. tmpreg: tregister;
  973. begin
  974. case loc.loc of
  975. LOC_REFERENCE,LOC_CREFERENCE:
  976. begin
  977. {$ifdef i386}
  978. case loc.size of
  979. OS_8,OS_S8:
  980. tmpreg := reg32toreg8(rg.getregisterint(exprasmlist));
  981. OS_16,OS_S16:
  982. tmpreg := reg32toreg16(get_scratch_reg(list));
  983. else
  984. tmpreg := get_scratch_reg(list);
  985. end;
  986. {$else i386}
  987. tmpreg := get_scratch_reg(list);
  988. {$endif i386}
  989. a_load_ref_reg(list,loc.size,loc.reference,tmpreg);
  990. a_load_reg_ref(list,loc.size,tmpreg,ref);
  991. {$ifdef i386}
  992. if not (loc.size in [OS_32,OS_S32]) then
  993. rg.ungetregister(exprasmlist,tmpreg)
  994. else
  995. {$endif i386}
  996. free_scratch_reg(list,tmpreg);
  997. end;
  998. LOC_REGISTER,LOC_CREGISTER:
  999. a_load_reg_ref(list,loc.size,loc.register,ref);
  1000. LOC_CONSTANT:
  1001. a_load_const_ref(list,loc.size,loc.value,ref);
  1002. else
  1003. internalerror(200109302);
  1004. end;
  1005. end;
  1006. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  1007. begin
  1008. case loc.loc of
  1009. LOC_REFERENCE, LOC_CREFERENCE:
  1010. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  1011. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1012. a_loadfpu_reg_reg(list,loc.register,reg);
  1013. else
  1014. internalerror(200203301);
  1015. end;
  1016. end;
  1017. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  1018. begin
  1019. case loc.loc of
  1020. LOC_REFERENCE, LOC_CREFERENCE:
  1021. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  1022. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1023. a_loadfpu_reg_reg(list,reg,loc.register);
  1024. else
  1025. internalerror(48991);
  1026. end;
  1027. end;
  1028. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  1029. var
  1030. tmpreg: tregister;
  1031. begin
  1032. tmpreg := get_scratch_reg(list);
  1033. a_load_ref_reg(list,size,ref,tmpreg);
  1034. a_op_const_reg(list,op,a,tmpreg);
  1035. a_load_reg_ref(list,size,tmpreg,ref);
  1036. free_scratch_reg(list,tmpreg);
  1037. end;
  1038. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  1039. begin
  1040. case loc.loc of
  1041. LOC_REGISTER, LOC_CREGISTER:
  1042. a_op_const_reg(list,op,a,loc.register);
  1043. LOC_REFERENCE, LOC_CREFERENCE:
  1044. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1045. else
  1046. internalerror(200109061);
  1047. end;
  1048. end;
  1049. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1050. var
  1051. tmpreg: tregister;
  1052. begin
  1053. tmpreg := get_scratch_reg(list);
  1054. a_load_ref_reg(list,size,ref,tmpreg);
  1055. a_op_reg_reg(list,op,size,reg,tmpreg);
  1056. a_load_reg_ref(list,size,tmpreg,ref);
  1057. free_scratch_reg(list,tmpreg);
  1058. end;
  1059. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1060. var
  1061. tmpreg: tregister;
  1062. begin
  1063. case op of
  1064. OP_NOT,OP_NEG:
  1065. { handle it as "load ref,reg; op reg" }
  1066. begin
  1067. a_load_ref_reg(list,size,ref,reg);
  1068. a_op_reg_reg(list,op,size,reg,reg);
  1069. end;
  1070. else
  1071. begin
  1072. tmpreg := get_scratch_reg(list);
  1073. a_load_ref_reg(list,size,ref,tmpreg);
  1074. a_op_reg_reg(list,op,size,tmpreg,reg);
  1075. free_scratch_reg(list,tmpreg);
  1076. end;
  1077. end;
  1078. end;
  1079. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  1080. begin
  1081. case loc.loc of
  1082. LOC_REGISTER, LOC_CREGISTER:
  1083. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1084. LOC_REFERENCE, LOC_CREFERENCE:
  1085. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1086. else
  1087. internalerror(200109061);
  1088. end;
  1089. end;
  1090. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1091. var
  1092. tmpreg: tregister;
  1093. begin
  1094. case loc.loc of
  1095. LOC_REGISTER,LOC_CREGISTER:
  1096. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1097. LOC_REFERENCE,LOC_CREFERENCE:
  1098. begin
  1099. tmpreg := get_scratch_reg(list);
  1100. {$ifdef i386}
  1101. makeregsize(tmpreg,loc.size);
  1102. {$endif i386}
  1103. a_load_ref_reg(list,loc.size,ref,tmpreg);
  1104. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1105. free_scratch_reg(list,tmpreg);
  1106. end;
  1107. else
  1108. internalerror(200109061);
  1109. end;
  1110. end;
  1111. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  1112. size: tcgsize; a: aword; src, dst: tregister);
  1113. begin
  1114. a_load_reg_reg(list,size,src,dst);
  1115. a_op_const_reg(list,op,a,dst);
  1116. end;
  1117. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  1118. size: tcgsize; src1, src2, dst: tregister);
  1119. begin
  1120. a_load_reg_reg(list,size,src2,dst);
  1121. a_op_reg_reg(list,op,size,src1,dst);
  1122. end;
  1123. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  1124. l : tasmlabel);
  1125. var
  1126. tmpreg: tregister;
  1127. begin
  1128. tmpreg := get_scratch_reg(list);
  1129. a_load_ref_reg(list,size,ref,tmpreg);
  1130. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1131. free_scratch_reg(list,tmpreg);
  1132. end;
  1133. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  1134. l : tasmlabel);
  1135. begin
  1136. case loc.loc of
  1137. LOC_REGISTER,LOC_CREGISTER:
  1138. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1139. LOC_REFERENCE,LOC_CREFERENCE:
  1140. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1141. else
  1142. internalerror(200109061);
  1143. end;
  1144. end;
  1145. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1146. var
  1147. tmpreg: tregister;
  1148. begin
  1149. tmpreg := get_scratch_reg(list);
  1150. a_load_ref_reg(list,size,ref,tmpreg);
  1151. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1152. free_scratch_reg(list,tmpreg);
  1153. end;
  1154. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1155. begin
  1156. case loc.loc of
  1157. LOC_REGISTER,
  1158. LOC_CREGISTER:
  1159. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1160. LOC_REFERENCE,
  1161. LOC_CREFERENCE :
  1162. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1163. LOC_CONSTANT:
  1164. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1165. else
  1166. internalerror(200203231);
  1167. end;
  1168. end;
  1169. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1170. l : tasmlabel);
  1171. var
  1172. tmpreg: tregister;
  1173. begin
  1174. case loc.loc of
  1175. LOC_REGISTER,LOC_CREGISTER:
  1176. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1177. LOC_REFERENCE,LOC_CREFERENCE:
  1178. begin
  1179. {$ifdef i386}
  1180. { the following is done with defines to avoid a speed penalty, }
  1181. { since all this is only necessary for the 80x86 (because EDI }
  1182. { doesn't have an 8bit component which is directly addressable) }
  1183. if size in [OS_8,OS_S8] then
  1184. tmpreg := rg.getregisterint(exprasmlist)
  1185. else
  1186. {$endif i386}
  1187. tmpreg := get_scratch_reg(list);
  1188. {$ifdef i386}
  1189. makeregsize(tmpreg,size);
  1190. {$endif i386}
  1191. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1192. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1193. {$ifdef i386}
  1194. if makereg32(tmpreg) <> R_EDI then
  1195. rg.ungetregister(exprasmlist,tmpreg)
  1196. else
  1197. {$endif i386}
  1198. free_scratch_reg(list,tmpreg);
  1199. end
  1200. else
  1201. internalerror(200109061);
  1202. end;
  1203. end;
  1204. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;
  1205. const todef: tdef);
  1206. { generate range checking code for the value at location p. The type }
  1207. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1208. { is the original type used at that location. When both defs are equal }
  1209. { the check is also insert (needed for succ,pref,inc,dec) }
  1210. const
  1211. {$ifdef ver1_0}
  1212. awordsignedmax=high(longint);
  1213. {$else}
  1214. awordsignedmax=high(aword) div 2;
  1215. {$endif}
  1216. var
  1217. neglabel : tasmlabel;
  1218. hreg : tregister;
  1219. fromdef : tdef;
  1220. lto,hto,
  1221. lfrom,hfrom : TConstExprInt;
  1222. from_signed: boolean;
  1223. begin
  1224. { range checking on and range checkable value? }
  1225. if not(cs_check_range in aktlocalswitches) or
  1226. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1227. exit;
  1228. { only check when assigning to scalar, subranges are different, }
  1229. { when todef=fromdef then the check is always generated }
  1230. fromdef:=p.resulttype.def;
  1231. getrange(p.resulttype.def,lfrom,hfrom);
  1232. getrange(todef,lto,hto);
  1233. { no range check if from and to are equal and are both longint/dword }
  1234. { (if we have a 32bit processor) or int64/qword, since such }
  1235. { operations can at most cause overflows (JM) }
  1236. { Note that these checks are mostly processor independent, they only }
  1237. { have to be changed once we introduce 64bit subrange types }
  1238. if (fromdef = todef) and
  1239. { then fromdef and todef can only be orddefs }
  1240. (((sizeof(aword) = 4) and
  1241. (((torddef(fromdef).typ = s32bit) and
  1242. (lfrom = low(longint)) and
  1243. (hfrom = high(longint))) or
  1244. ((torddef(fromdef).typ = u32bit) and
  1245. (lfrom = low(cardinal)) and
  1246. (hfrom = high(cardinal))))) or
  1247. is_64bitint(fromdef)) then
  1248. exit;
  1249. if todef<>fromdef then
  1250. begin
  1251. { if the from-range falls completely in the to-range, no check }
  1252. { is necessary }
  1253. if (lto<=lfrom) and (hto>=hfrom) then
  1254. exit;
  1255. end;
  1256. { generate the rangecheck code for the def where we are going to }
  1257. { store the result }
  1258. { use the trick that }
  1259. { a <= x <= b <=> 0 <= x-a <= b-a <=> cardinal(x-a) <= cardinal(b-a) }
  1260. { To be able to do that, we have to make sure however that either }
  1261. { fromdef and todef are both signed or unsigned, or that we leave }
  1262. { the parts < 0 and > maxlongint out }
  1263. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1264. from_signed := is_signed(fromdef);
  1265. if from_signed xor is_signed(todef) then
  1266. if from_signed then
  1267. { from is signed, to is unsigned }
  1268. begin
  1269. { if high(from) < 0 -> always range error }
  1270. if (hfrom < 0) or
  1271. { if low(to) > maxlongint also range error }
  1272. (lto > awordsignedmax) then
  1273. begin
  1274. a_call_name(list,'FPC_RANGEERROR',0);
  1275. exit
  1276. end;
  1277. { from is signed and to is unsigned -> when looking at from }
  1278. { as an unsigned value, it must be < maxlongint (otherwise }
  1279. { it's negative, which is invalid since "to" is unsigned) }
  1280. if hto > awordsignedmax then
  1281. hto := awordsignedmax;
  1282. end
  1283. else
  1284. { from is unsigned, to is signed }
  1285. begin
  1286. if (lfrom > awordsignedmax) or
  1287. (hto < 0) then
  1288. begin
  1289. a_call_name(list,'FPC_RANGEERROR',0);
  1290. exit
  1291. end;
  1292. { from is unsigned and to is signed -> when looking at to }
  1293. { as an unsigned value, it must be >= 0 (since negative }
  1294. { values are the same as values > maxlongint) }
  1295. if lto < 0 then
  1296. lto := 0;
  1297. end;
  1298. hreg := get_scratch_reg(list);
  1299. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1300. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1301. aword(longint(lto and $ffffffff)),p.location.register,hreg)
  1302. else
  1303. begin
  1304. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1305. p.location.reference,hreg);
  1306. a_op_const_reg(list,OP_SUB,aword(longint(lto and $ffffffff)),hreg);
  1307. end;
  1308. getlabel(neglabel);
  1309. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(longint((hto-lto) and $ffffffff)),hreg,neglabel);
  1310. { !!! should happen right after the compare (JM) }
  1311. free_scratch_reg(list,hreg);
  1312. a_call_name(list,'FPC_RANGEERROR',0);
  1313. a_label(list,neglabel);
  1314. end;
  1315. function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  1316. begin
  1317. reg_cgsize := OS_INT;
  1318. end;
  1319. finalization
  1320. cg.free;
  1321. end.
  1322. {
  1323. $Log$
  1324. Revision 1.10 2002-04-04 19:05:54 peter
  1325. * removed unused units
  1326. * use tlocation.size in cg.a_*loc*() routines
  1327. Revision 1.9 2002/04/02 17:11:27 peter
  1328. * tlocation,treference update
  1329. * LOC_CONSTANT added for better constant handling
  1330. * secondadd splitted in multiple routines
  1331. * location_force_reg added for loading a location to a register
  1332. of a specified size
  1333. * secondassignment parses now first the right and then the left node
  1334. (this is compatible with Kylix). This saves a lot of push/pop especially
  1335. with string operations
  1336. * adapted some routines to use the new cg methods
  1337. Revision 1.8 2002/03/31 20:26:33 jonas
  1338. + a_loadfpu_* and a_loadmm_* methods in tcg
  1339. * register allocation is now handled by a class and is mostly processor
  1340. independent (+rgobj.pas and i386/rgcpu.pas)
  1341. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1342. * some small improvements and fixes to the optimizer
  1343. * some register allocation fixes
  1344. * some fpuvaroffset fixes in the unary minus node
  1345. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1346. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1347. also better optimizable)
  1348. * fixed and optimized register saving/restoring for new/dispose nodes
  1349. * LOC_FPU locations now also require their "register" field to be set to
  1350. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1351. - list field removed of the tnode class because it's not used currently
  1352. and can cause hard-to-find bugs
  1353. Revision 1.7 2002/03/04 19:10:11 peter
  1354. * removed compiler warnings
  1355. Revision 1.6 2001/12/30 17:24:48 jonas
  1356. * range checking is now processor independent (part in cgobj,
  1357. part in cg64f32) and should work correctly again (it needed
  1358. some changes after the changes of the low and high of
  1359. tordef's to int64)
  1360. * maketojumpbool() is now processor independent (in ncgutil)
  1361. * getregister32 is now called getregisterint
  1362. Revision 1.5 2001/12/29 15:28:58 jonas
  1363. * powerpc/cgcpu.pas compiles :)
  1364. * several powerpc-related fixes
  1365. * cpuasm unit is now based on common tainst unit
  1366. + nppcmat unit for powerpc (almost complete)
  1367. Revision 1.4 2001/09/30 21:26:42 peter
  1368. * removed obsolete newst defines
  1369. Revision 1.3 2001/09/30 16:17:17 jonas
  1370. * made most constant and mem handling processor independent
  1371. Revision 1.2 2001/09/28 20:39:32 jonas
  1372. * changed all flow control structures (except for exception handling
  1373. related things) to processor independent code (in new ncgflw unit)
  1374. + generic cgobj unit which contains lots of code generator helpers with
  1375. global "cg" class instance variable
  1376. + cgcpu unit for i386 (implements processor specific routines of the above
  1377. unit)
  1378. * updated cgbase and cpubase for the new code generator units
  1379. * include ncgflw unit in cpunode unit
  1380. Revision 1.5 2001/09/09 17:10:26 jonas
  1381. * some more things implemented
  1382. Revision 1.4 2001/09/06 15:25:55 jonas
  1383. * changed type of tcg from object to class -> abstract methods are now
  1384. a lot cleaner :)
  1385. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  1386. (if possible with geenric implementation and necessary ppc
  1387. implementations)
  1388. * worked a bit further on cgflw, now working on exitnode
  1389. Revision 1.3 2001/09/05 20:21:03 jonas
  1390. * new cgflow based on n386flw with all nodes until forn "translated"
  1391. + a_cmp_loc_*_label methods for tcg
  1392. + base implementatino for a_cmp_ref_*_label methods
  1393. * small bugfixes to powerpc cg
  1394. Revision 1.2 2001/08/26 13:37:04 florian
  1395. * some cg reorganisation
  1396. * some PPC updates
  1397. Revision 1.1 2000/07/13 06:30:07 michael
  1398. + Initial import
  1399. }