cgobj.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. var
  711. {$ifdef GDB}
  712. mangled_length : longint;
  713. p : pchar;
  714. {$endif GDB}
  715. nofinal,noreraiselabel : tasmlabel;
  716. hr : treference;
  717. r : tregister;
  718. begin
  719. (*
  720. if aktexitlabel^.is_used then
  721. list^.insert(new(pai_label,init(aktexitlabel)));
  722. { call the destructor help procedure }
  723. if (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  724. begin
  725. if procinfo^._class^.is_class then
  726. a_call_name(list,'FPC_DISPOSE_CLASS',0)
  727. else
  728. begin
  729. if procinfo^._class^.needs_inittable then
  730. begin
  731. getlabel(nofinal);
  732. {!!!!!!!!!!
  733. reset_reference(hr);
  734. hr.base:=R_EBP;
  735. hr.offset:=8;
  736. a_cmp_reg_const_label(list,OS_ADDR,OZ_EQ,
  737. }
  738. reset_reference(hr);
  739. hr.symbol:=procinfo^._class^.get_inittable_label;
  740. a_paramaddr_ref(list,hr,2);
  741. a_param_reg(list,OS_ADDR,self_pointer,1);
  742. a_call_name(list,'FPC_FINALIZE',0);
  743. a_label(list,nofinal);
  744. end;
  745. { vmt_offset_reg can be a scratch register, }
  746. { but it must be always the same }
  747. a_reg_alloc(list,vmt_offset_reg);
  748. a_load_const_reg(list,OS_32,procinfo^._class^.vmt_offset,vmt_offset_reg);
  749. a_call_name(list,'FPC_HELP_DESTRUCTOR',0);
  750. a_reg_dealloc(list,vmt_offset_reg);
  751. end;
  752. end;
  753. { finalize temporary data }
  754. g_finalizetempansistrings(list);
  755. _list:=list;
  756. { finalize local data }
  757. aktprocsym^.definition^.localst^.foreach({$ifdef FPCPROCVAR}@{$endif}_finalize_data);
  758. { finalize paras data }
  759. if assigned(aktprocsym^.definition^.parast) then
  760. aktprocsym^.definition^.parast^.foreach({$ifdef FPCPROCVAR}@{$endif}_finalize_data);
  761. { do we need to handle exceptions because of ansi/widestrings ? }
  762. if (procinfo^.flags and pi_needs_implicit_finally)<>0 then
  763. begin
  764. getlabel(noreraiselabel);
  765. a_call_name(list,'FPC_POPADDRSTACK',0);
  766. a_reg_alloc(list,accumulator);
  767. g_pop_exception_value_reg(list,accumulator);
  768. a_cmp_const_reg_label(list,OS_32,OC_EQ,0,accumulator,noreraiselabel);
  769. a_reg_dealloc(list,accumulator);
  770. { must be the return value finalized before reraising the exception? }
  771. if (procinfo^.returntype.def<>tdef(voiddef)) and
  772. (procinfo^.returntype.def^.needs_inittable) and
  773. ((procinfo^.returntype.def^.deftype<>objectdef) or
  774. not(pobjectdef(procinfo^.returntype.def)^.is_class)) then
  775. begin
  776. reset_reference(hr);
  777. hr.offset:=procinfo^.return_offset;
  778. hr.base:=procinfo^.framepointer;
  779. g_finalize(list,procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  780. end;
  781. a_call_name(list,'FPC_RERAISE',0);
  782. a_label(list,noreraiselabel);
  783. end;
  784. { call __EXIT for main program }
  785. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  786. a_call_name(list,'FPC_DO_EXIT',0);
  787. { handle return value }
  788. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  789. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  790. { handle_return_value(inlined) }
  791. else
  792. begin
  793. { return self in EAX }
  794. a_label(list,quickexitlabel);
  795. a_reg_alloc(list,accumulator);
  796. a_load_reg_reg(list,OS_ADDR,self_pointer,accumulator);
  797. a_reg_dealloc(list,self_pointer);
  798. a_label(list,quickexitlabel);
  799. { we can't clear the zero flag because the Alpha }
  800. { for example doesn't have flags, we have to compare }
  801. { the accu. in the caller }
  802. end;
  803. { stabs uses the label also ! }
  804. if aktexit2label^.is_used or
  805. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  806. a_label(list,aktexit2label);
  807. {$ifdef dummy}
  808. { should we restore edi ? }
  809. { for all i386 gcc implementations }
  810. {!!!!!!!!!!! I don't know how to handle register saving yet }
  811. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  812. begin
  813. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  814. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  815. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  816. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  817. { here we could reset R_EBX
  818. but that is risky because it only works
  819. if genexitcode is called after genentrycode
  820. so lets skip this for the moment PM
  821. aktprocsym^.definition^.usedregisters:=
  822. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  823. }
  824. end;
  825. {$endif dummy}
  826. if not(nostackframe) and not inlined then
  827. g_restore_frame_pointer(list);
  828. { at last, the return is generated }
  829. if not inlined then
  830. if po_interrupt in aktprocsym^.definition^.procoptions then
  831. g_interrupt_stackframe_exit(list)
  832. else
  833. g_return_from_proc(list,parasize);
  834. list^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  835. {$ifdef GDB}
  836. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  837. begin
  838. aktprocsym^.concatstabto(list);
  839. if assigned(procinfo^._class) then
  840. if (not assigned(procinfo^.parent) or
  841. not assigned(procinfo^.parent^._class)) then
  842. list^.concat(new(pai_stabs,init(strpnew(
  843. '"$t:v'+procinfo^._class^.numberstring+'",'+
  844. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))));
  845. {!!!!!!!!!!!!
  846. else
  847. list^.concat(new(pai_stabs,init(strpnew(
  848. '"$t:r'+procinfo^._class^.numberstring+'",'+
  849. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  850. }
  851. if (tdef(aktprocsym^.definition^.rettype.def) <> tdef(voiddef)) then
  852. begin
  853. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  854. list^.concat(new(pai_stabs,init(strpnew(
  855. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  856. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  857. else
  858. list^.concat(new(pai_stabs,init(strpnew(
  859. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  860. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  861. if (m_result in aktmodeswitches) then
  862. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  863. list^.concat(new(pai_stabs,init(strpnew(
  864. '"RESULT:X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  865. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  866. else
  867. list^.concat(new(pai_stabs,init(strpnew(
  868. '"RESULT:X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  869. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  870. end;
  871. mangled_length:=length(aktprocsym^.definition^.mangledname);
  872. getmem(p,mangled_length+50);
  873. strpcopy(p,'192,0,0,');
  874. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  875. list^.concat(new(pai_stabn,init(strnew(p))));
  876. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  877. +aktprocsym^.definition^.mangledname))));
  878. p[0]:='2';p[1]:='2';p[2]:='4';
  879. strpcopy(strend(p),'_end');}
  880. freemem(p,mangled_length+50);
  881. list^.concat(new(pai_stabn,init(
  882. strpnew('224,0,0,'+aktexit2label^.name))));
  883. { strpnew('224,0,0,'
  884. +aktprocsym^.definition^.mangledname+'_end'))));}
  885. end;
  886. {$endif GDB}
  887. *)
  888. runerror(211);
  889. end;
  890. {*****************************************************************************
  891. some generic implementations
  892. ****************************************************************************}
  893. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  894. var
  895. tmpreg: tregister;
  896. begin
  897. tmpreg := get_scratch_reg(list);
  898. a_load_const_reg(list,size,a,tmpreg);
  899. a_load_reg_ref(list,size,tmpreg,ref);
  900. free_scratch_reg(list,tmpreg);
  901. end;
  902. procedure tcg.a_load_loc_reg(list : taasmoutput;size : tcgsize;const loc: tlocation; reg : tregister);
  903. begin
  904. case loc.loc of
  905. LOC_REFERENCE,LOC_MEM:
  906. a_load_ref_reg(list,size,loc.reference,reg);
  907. LOC_REGISTER,LOC_CREGISTER:
  908. a_load_reg_reg(list,size,loc.register,reg);
  909. else
  910. internalerror(200109092);
  911. end;
  912. end;
  913. procedure tcg.a_load_loc_ref(list : taasmoutput;size : tcgsize;const loc: tlocation; const ref : treference);
  914. var
  915. tmpreg: tregister;
  916. begin
  917. case loc.loc of
  918. LOC_REFERENCE,LOC_MEM:
  919. begin
  920. {$ifdef i386}
  921. case size of
  922. OS_8,OS_S8:
  923. tmpreg := reg32toreg8(getregisterint);
  924. OS_16,OS_S16:
  925. tmpreg := reg32toreg16(get_scratch_reg(list));
  926. else
  927. tmpreg := get_scratch_reg(list);
  928. end;
  929. {$else i386}
  930. tmpreg := get_scratch_reg(list);
  931. {$endif i386}
  932. a_load_ref_reg(list,size,loc.reference,tmpreg);
  933. a_load_reg_ref(list,size,tmpreg,ref);
  934. {$ifdef i386}
  935. if not (size in [OS_32,OS_S32]) then
  936. ungetregister(tmpreg)
  937. else
  938. {$endif i386}
  939. free_scratch_reg(list,tmpreg);
  940. end;
  941. LOC_REGISTER,LOC_CREGISTER:
  942. a_load_reg_ref(list,size,loc.register,ref);
  943. else
  944. internalerror(200109302);
  945. end;
  946. end;
  947. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  948. var
  949. tmpreg: tregister;
  950. begin
  951. tmpreg := get_scratch_reg(list);
  952. a_load_ref_reg(list,size,ref,tmpreg);
  953. a_op_const_reg(list,op,a,tmpreg);
  954. a_load_reg_ref(list,size,tmpreg,ref);
  955. free_scratch_reg(list,tmpreg);
  956. end;
  957. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const loc: tlocation);
  958. begin
  959. case loc.loc of
  960. LOC_REGISTER, LOC_CREGISTER:
  961. a_op_const_reg(list,op,a,loc.register);
  962. LOC_REFERENCE, LOC_MEM:
  963. a_op_const_ref(list,op,size,a,loc.reference);
  964. else
  965. internalerror(200109061);
  966. end;
  967. end;
  968. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  969. var
  970. tmpreg: tregister;
  971. begin
  972. tmpreg := get_scratch_reg(list);
  973. a_load_ref_reg(list,size,ref,tmpreg);
  974. a_op_reg_reg(list,op,size,reg,tmpreg);
  975. a_load_reg_ref(list,size,tmpreg,ref);
  976. free_scratch_reg(list,tmpreg);
  977. end;
  978. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  979. var
  980. tmpreg: tregister;
  981. begin
  982. case op of
  983. OP_NOT,OP_NEG:
  984. { handle it as "load ref,reg; op reg" }
  985. begin
  986. a_load_ref_reg(list,size,ref,reg);
  987. a_op_reg_reg(list,op,size,reg,reg);
  988. end;
  989. else
  990. begin
  991. tmpreg := get_scratch_reg(list);
  992. a_load_ref_reg(list,size,ref,tmpreg);
  993. a_op_reg_reg(list,op,size,tmpreg,reg);
  994. free_scratch_reg(list,tmpreg);
  995. end;
  996. end;
  997. end;
  998. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: tregister; const loc: tlocation);
  999. begin
  1000. case loc.loc of
  1001. LOC_REGISTER, LOC_CREGISTER:
  1002. a_op_reg_reg(list,op,size,reg,loc.register);
  1003. LOC_REFERENCE, LOC_MEM:
  1004. a_op_reg_ref(list,op,size,reg,loc.reference);
  1005. else
  1006. internalerror(200109061);
  1007. end;
  1008. end;
  1009. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; const loc: tlocation);
  1010. var
  1011. tmpreg: tregister;
  1012. begin
  1013. case loc.loc of
  1014. LOC_REGISTER,LOC_CREGISTER:
  1015. a_op_ref_reg(list,op,size,ref,loc.register);
  1016. LOC_REFERENCE,LOC_MEM:
  1017. begin
  1018. tmpreg := get_scratch_reg(list);
  1019. {$ifdef i386}
  1020. makeregsize(tmpreg,size);
  1021. {$endif i386}
  1022. a_load_ref_reg(list,size,ref,tmpreg);
  1023. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  1024. free_scratch_reg(list,tmpreg);
  1025. end;
  1026. else
  1027. internalerror(200109061);
  1028. end;
  1029. end;
  1030. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  1031. size: tcgsize; a: aword; src, dst: tregister);
  1032. begin
  1033. a_load_reg_reg(list,size,src,dst);
  1034. a_op_const_reg(list,op,a,dst);
  1035. end;
  1036. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  1037. size: tcgsize; src1, src2, dst: tregister);
  1038. begin
  1039. a_load_reg_reg(list,size,src2,dst);
  1040. a_op_reg_reg(list,op,size,src1,dst);
  1041. end;
  1042. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  1043. l : tasmlabel);
  1044. var
  1045. tmpreg: tregister;
  1046. begin
  1047. tmpreg := get_scratch_reg(list);
  1048. a_load_ref_reg(list,size,ref,tmpreg);
  1049. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1050. free_scratch_reg(list,tmpreg);
  1051. end;
  1052. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  1053. l : tasmlabel);
  1054. begin
  1055. case loc.loc of
  1056. LOC_REGISTER,LOC_CREGISTER:
  1057. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1058. LOC_REFERENCE,LOC_MEM:
  1059. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1060. else
  1061. internalerror(200109061);
  1062. end;
  1063. end;
  1064. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1065. var
  1066. tmpreg: tregister;
  1067. begin
  1068. tmpreg := get_scratch_reg(list);
  1069. a_load_ref_reg(list,size,ref,tmpreg);
  1070. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1071. free_scratch_reg(list,tmpreg);
  1072. end;
  1073. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1074. l : tasmlabel);
  1075. var
  1076. tmpreg: tregister;
  1077. begin
  1078. case loc.loc of
  1079. LOC_REGISTER,LOC_CREGISTER:
  1080. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1081. LOC_REFERENCE,LOC_MEM:
  1082. begin
  1083. {$ifdef i386}
  1084. { the following is done with defines to avoid a speed penalty, }
  1085. { since all this is only necessary for the 80x86 (because EDI }
  1086. { doesn't have an 8bit component which is directly addressable) }
  1087. if size in [OS_8,OS_S8] then
  1088. tmpreg := getregisterint
  1089. else
  1090. {$endif i386}
  1091. tmpreg := get_scratch_reg(list);
  1092. {$ifdef i386}
  1093. makeregsize(tmpreg,size);
  1094. {$endif i386}
  1095. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1096. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1097. {$ifdef i386}
  1098. if makereg32(tmpreg) <> R_EDI then
  1099. ungetregister(tmpreg)
  1100. else
  1101. {$endif i386}
  1102. free_scratch_reg(list,tmpreg);
  1103. end
  1104. else
  1105. internalerror(200109061);
  1106. end;
  1107. end;
  1108. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;
  1109. const todef: tdef);
  1110. { generate range checking code for the value at location p. The type }
  1111. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1112. { is the original type used at that location. When both defs are equal }
  1113. { the check is also insert (needed for succ,pref,inc,dec) }
  1114. var
  1115. neglabel : tasmlabel;
  1116. hreg : tregister;
  1117. fromdef : tdef;
  1118. lto,hto,
  1119. lfrom,hfrom : TConstExprInt;
  1120. from_signed: boolean;
  1121. begin
  1122. { range checking on and range checkable value? }
  1123. if not(cs_check_range in aktlocalswitches) or
  1124. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1125. exit;
  1126. { only check when assigning to scalar, subranges are different, }
  1127. { when todef=fromdef then the check is always generated }
  1128. fromdef:=p.resulttype.def;
  1129. getrange(p.resulttype.def,lfrom,hfrom);
  1130. getrange(todef,lto,hto);
  1131. { no range check if from and to are equal and are both longint/dword }
  1132. { (if we have a 32bit processor) or int64/qword, since such }
  1133. { operations can at most cause overflows (JM) }
  1134. { Note that these checks are mostly processor independent, they only }
  1135. { have to be changed once we introduce 64bit subrange types }
  1136. if (fromdef = todef) and
  1137. { then fromdef and todef can only be orddefs }
  1138. (((sizeof(aword) = 4) and
  1139. (((torddef(fromdef).typ = s32bit) and
  1140. (lfrom = low(longint)) and
  1141. (hfrom = high(longint))) or
  1142. ((torddef(fromdef).typ = u32bit) and
  1143. (lfrom = low(cardinal)) and
  1144. (hfrom = high(cardinal))))) or
  1145. is_64bitint(fromdef)) then
  1146. exit;
  1147. if todef<>fromdef then
  1148. begin
  1149. { if the from-range falls completely in the to-range, no check }
  1150. { is necessary }
  1151. if (lto<=lfrom) and (hto>=hfrom) then
  1152. exit;
  1153. end;
  1154. { generate the rangecheck code for the def where we are going to }
  1155. { store the result }
  1156. { use the trick that }
  1157. { a <= x <= b <=> 0 <= x-a <= b-a <=> cardinal(x-a) <= cardinal(b-a) }
  1158. { To be able to do that, we have to make sure however that either }
  1159. { fromdef and todef are both signed or unsigned, or that we leave }
  1160. { the parts < 0 and > maxlongint out }
  1161. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1162. from_signed := is_signed(fromdef);
  1163. if from_signed xor is_signed(todef) then
  1164. if from_signed then
  1165. { from is signed, to is unsigned }
  1166. begin
  1167. { if high(from) < 0 -> always range error }
  1168. if (hfrom < 0) or
  1169. { if low(to) > maxlongint also range error }
  1170. (lto > (high(aword) div 2)) then
  1171. begin
  1172. a_call_name(list,'FPC_RANGEERROR',0);
  1173. exit
  1174. end;
  1175. { from is signed and to is unsigned -> when looking at from }
  1176. { as an unsigned value, it must be < maxlongint (otherwise }
  1177. { it's negative, which is invalid since "to" is unsigned) }
  1178. if hto > (high(aword) div 2) then
  1179. hto := (high(aword) div 2);
  1180. end
  1181. else
  1182. { from is unsigned, to is signed }
  1183. begin
  1184. if (lfrom > (high(aword) div 2)) or
  1185. (hto < 0) then
  1186. begin
  1187. a_call_name(list,'FPC_RANGEERROR',0);
  1188. exit
  1189. end;
  1190. { from is unsigned and to is signed -> when looking at to }
  1191. { as an unsigned value, it must be >= 0 (since negative }
  1192. { values are the same as values > maxlongint) }
  1193. if lto < 0 then
  1194. lto := 0;
  1195. end;
  1196. hreg := get_scratch_reg(list);
  1197. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1198. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1199. aword(lto),p.location.register,hreg)
  1200. else
  1201. begin
  1202. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1203. p.location.reference,hreg);
  1204. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1205. end;
  1206. getlabel(neglabel);
  1207. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1208. { !!! should happen right after the compare (JM) }
  1209. free_scratch_reg(list,hreg);
  1210. a_call_name(list,'FPC_RANGEERROR',0);
  1211. a_label(list,neglabel);
  1212. end;
  1213. function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  1214. begin
  1215. reg_cgsize := OS_INT;
  1216. end;
  1217. finalization
  1218. cg.free;
  1219. end.
  1220. {
  1221. $Log$
  1222. Revision 1.6 2001-12-30 17:24:48 jonas
  1223. * 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
  1224. Revision 1.5 2001/12/29 15:28:58 jonas
  1225. * powerpc/cgcpu.pas compiles :)
  1226. * several powerpc-related fixes
  1227. * cpuasm unit is now based on common tainst unit
  1228. + nppcmat unit for powerpc (almost complete)
  1229. Revision 1.4 2001/09/30 21:26:42 peter
  1230. * removed obsolete newst defines
  1231. Revision 1.3 2001/09/30 16:17:17 jonas
  1232. * made most constant and mem handling processor independent
  1233. Revision 1.2 2001/09/28 20:39:32 jonas
  1234. * changed all flow control structures (except for exception handling
  1235. related things) to processor independent code (in new ncgflw unit)
  1236. + generic cgobj unit which contains lots of code generator helpers with
  1237. global "cg" class instance variable
  1238. + cgcpu unit for i386 (implements processor specific routines of the above
  1239. unit)
  1240. * updated cgbase and cpubase for the new code generator units
  1241. * include ncgflw unit in cpunode unit
  1242. Revision 1.5 2001/09/09 17:10:26 jonas
  1243. * some more things implemented
  1244. Revision 1.4 2001/09/06 15:25:55 jonas
  1245. * changed type of tcg from object to class -> abstract methods are now
  1246. a lot cleaner :)
  1247. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  1248. (if possible with geenric implementation and necessary ppc
  1249. implementations)
  1250. * worked a bit further on cgflw, now working on exitnode
  1251. Revision 1.3 2001/09/05 20:21:03 jonas
  1252. * new cgflow based on n386flw with all nodes until forn "translated"
  1253. + a_cmp_loc_*_label methods for tcg
  1254. + base implementatino for a_cmp_ref_*_label methods
  1255. * small bugfixes to powerpc cg
  1256. Revision 1.2 2001/08/26 13:37:04 florian
  1257. * some cg reorganisation
  1258. * some PPC updates
  1259. Revision 1.1 2000/07/13 06:30:07 michael
  1260. + Initial import
  1261. }