cgobj.pas 59 KB

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