cgobj.pas 62 KB

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