cgobj.pas 56 KB

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