cgcpu.pas 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. globtype, symtype, symdef,
  22. cgbase, cgobj,
  23. aasmbase, aasmcpu, aasmtai,
  24. cpubase, cpuinfo, cgutils, rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcg)
  28. procedure init_register_allocators; override;
  29. procedure done_register_allocators; override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_const(list: taasmoutput; size: tcgsize; a: aint; const
  36. paraloc: tcgpara); override;
  37. procedure a_param_ref(list: taasmoutput; size: tcgsize; const r: treference;
  38. const paraloc: tcgpara); override;
  39. procedure a_paramaddr_ref(list: taasmoutput; const r: treference; const
  40. paraloc: tcgpara); override;
  41. procedure a_call_name(list: taasmoutput; const s: string); override;
  42. procedure a_call_reg(list: taasmoutput; reg: tregister); override;
  43. procedure a_op_const_reg(list: taasmoutput; Op: TOpCG; size: TCGSize; a:
  44. aint; reg: TRegister); override;
  45. procedure a_op_reg_reg(list: taasmoutput; Op: TOpCG; size: TCGSize; src,
  46. dst: TRegister); override;
  47. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  48. size: tcgsize; a: aint; src, dst: tregister); override;
  49. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  50. size: tcgsize; src1, src2, dst: tregister); override;
  51. { move instructions }
  52. procedure a_load_const_reg(list: taasmoutput; size: tcgsize; a: aint; reg:
  53. tregister); override;
  54. { stores the contents of register reg to the memory location described by
  55. ref }
  56. procedure a_load_reg_ref(list: taasmoutput; fromsize, tosize: tcgsize; reg:
  57. tregister; const ref: treference); override;
  58. { loads the memory pointed to by ref into register reg }
  59. procedure a_load_ref_reg(list: taasmoutput; fromsize, tosize: tcgsize; const
  60. Ref: treference; reg: tregister); override;
  61. procedure a_load_reg_reg(list: taasmoutput; fromsize, tosize: tcgsize; reg1,
  62. reg2: tregister); override;
  63. { fpu move instructions }
  64. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2:
  65. tregister); override;
  66. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref:
  67. treference; reg: tregister); override;
  68. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg:
  69. tregister; const ref: treference); override;
  70. { comparison operations }
  71. procedure a_cmp_const_reg_label(list: taasmoutput; size: tcgsize; cmp_op:
  72. topcmp; a: aint; reg: tregister;
  73. l: tasmlabel); override;
  74. procedure a_cmp_reg_reg_label(list: taasmoutput; size: tcgsize; cmp_op:
  75. topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  76. procedure a_jmp_name(list: taasmoutput; const s: string); override;
  77. procedure a_jmp_always(list: taasmoutput; l: tasmlabel); override;
  78. procedure a_jmp_flags(list: taasmoutput; const f: TResFlags; l: tasmlabel);
  79. override;
  80. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags;
  81. reg: TRegister); override;
  82. procedure g_proc_entry(list: taasmoutput; localsize: longint; nostackframe:
  83. boolean); override;
  84. procedure g_proc_exit(list: taasmoutput; parasize: longint; nostackframe:
  85. boolean); override;
  86. procedure g_save_standard_registers(list: Taasmoutput); override;
  87. procedure g_restore_standard_registers(list: Taasmoutput); override;
  88. procedure a_loadaddr_ref_reg(list: taasmoutput; const ref: treference; r:
  89. tregister); override;
  90. procedure g_concatcopy(list: taasmoutput; const source, dest: treference;
  91. len: aint); override;
  92. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  93. override;
  94. procedure a_jmp_cond(list: taasmoutput; cond: TOpCmp; l: tasmlabel);
  95. procedure g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const
  96. labelname: string; ioffset: longint); override;
  97. private
  98. { Make sure ref is a valid reference for the PowerPC and sets the }
  99. { base to the value of the index if (base = R_NO). }
  100. { Returns true if the reference contained a base, index and an }
  101. { offset or symbol, in which case the base will have been changed }
  102. { to a tempreg (which has to be freed by the caller) containing }
  103. { the sum of part of the original reference }
  104. function fixref(list: taasmoutput; var ref: treference; const size : TCgsize): boolean;
  105. { returns whether a reference can be used immediately in a powerpc }
  106. { instruction }
  107. function issimpleref(const ref: treference): boolean;
  108. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  109. procedure a_load_store(list: taasmoutput; op: tasmop; reg: tregister;
  110. ref: treference);
  111. { creates the correct branch instruction for a given combination }
  112. { of asmcondflags and destination addressing mode }
  113. procedure a_jmp(list: taasmoutput; op: tasmop;
  114. c: tasmcondflag; crval: longint; l: tasmlabel);
  115. { returns the lowest numbered FP register in use, and the number of used FP registers
  116. for the current procedure }
  117. procedure calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  118. { returns the lowest numbered GP register in use, and the number of used GP registers
  119. for the current procedure }
  120. procedure calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  121. { returns true if the offset of the given reference can not be represented by a 16 bit
  122. immediate as required by some PowerPC instructions }
  123. function hasLargeOffset(const ref : TReference) : Boolean; inline;
  124. procedure a_call_name_direct(list: taasmoutput; s: string; prependDot : boolean; addNOP : boolean);
  125. end;
  126. const
  127. TShiftOpCG2AsmOpConst : array[boolean, OP_SAR..OP_SHR] of TAsmOp = (
  128. (A_SRAWI, A_SLWI, A_SRWI), (A_SRADI, A_SLDI, A_SRDI)
  129. );
  130. TOpCmp2AsmCond: array[topcmp] of TAsmCondFlag = (C_NONE, C_EQ, C_GT,
  131. C_LT, C_GE, C_LE, C_NE, C_LE, C_LT, C_GE, C_GT);
  132. implementation
  133. uses
  134. sysutils,
  135. globals, verbose, systems, cutils,
  136. symconst, symsym, fmodule,
  137. rgobj, tgobj, cpupi, procinfo, paramgr;
  138. procedure tcgppc.init_register_allocators;
  139. begin
  140. inherited init_register_allocators;
  141. rg[R_INTREGISTER] := trgcpu.create(R_INTREGISTER, R_SUBWHOLE,
  142. [RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  143. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  144. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  145. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  146. RS_R14, RS_R13], first_int_imreg, []);
  147. rg[R_FPUREGISTER] := trgcpu.create(R_FPUREGISTER, R_SUBNONE,
  148. [RS_F0, RS_F1, RS_F2, RS_F3, RS_F4, RS_F5, RS_F6, RS_F7, RS_F8, RS_F9,
  149. RS_F10, RS_F11, RS_F12, RS_F13, RS_F31, RS_F30, RS_F29, RS_F28, RS_F27,
  150. RS_F26, RS_F25, RS_F24, RS_F23, RS_F22, RS_F21, RS_F20, RS_F19, RS_F18,
  151. RS_F17, RS_F16, RS_F15, RS_F14], first_fpu_imreg, []);
  152. {$WARNING FIX ME}
  153. rg[R_MMREGISTER] := trgcpu.create(R_MMREGISTER, R_SUBNONE,
  154. [RS_M0, RS_M1, RS_M2], first_mm_imreg, []);
  155. end;
  156. procedure tcgppc.done_register_allocators;
  157. begin
  158. rg[R_INTREGISTER].free;
  159. rg[R_FPUREGISTER].free;
  160. rg[R_MMREGISTER].free;
  161. inherited done_register_allocators;
  162. end;
  163. procedure tcgppc.a_param_const(list: taasmoutput; size: tcgsize; a: aint; const
  164. paraloc: tcgpara);
  165. var
  166. ref: treference;
  167. begin
  168. paraloc.check_simple_location;
  169. case paraloc.location^.loc of
  170. LOC_REGISTER, LOC_CREGISTER:
  171. a_load_const_reg(list, size, a, paraloc.location^.register);
  172. LOC_REFERENCE:
  173. begin
  174. reference_reset(ref);
  175. ref.base := paraloc.location^.reference.index;
  176. ref.offset := paraloc.location^.reference.offset;
  177. a_load_const_ref(list, size, a, ref);
  178. end;
  179. else
  180. internalerror(2002081101);
  181. end;
  182. end;
  183. procedure tcgppc.a_param_ref(list: taasmoutput; size: tcgsize; const r:
  184. treference; const paraloc: tcgpara);
  185. var
  186. tmpref, ref: treference;
  187. location: pcgparalocation;
  188. sizeleft: aint;
  189. begin
  190. location := paraloc.location;
  191. tmpref := r;
  192. sizeleft := paraloc.intsize;
  193. while assigned(location) do begin
  194. case location^.loc of
  195. LOC_REGISTER, LOC_CREGISTER:
  196. begin
  197. if (size <> OS_NO) then
  198. a_load_ref_reg(list, size, location^.size, tmpref,
  199. location^.register)
  200. else
  201. { load non-integral sized memory location into register. This
  202. memory location be 1-sizeleft byte sized.
  203. Always assume that this memory area is properly aligned, eg. start
  204. loading the larger quantities for "odd" quantities first }
  205. case sizeleft of
  206. 1,2,4,8 :
  207. a_load_ref_reg(list, int_cgsize(sizeleft), location^.size, tmpref,
  208. location^.register);
  209. 3 : begin
  210. a_reg_alloc(list, NR_R12);
  211. a_load_ref_reg(list, OS_16, location^.size, tmpref,
  212. NR_R12);
  213. inc(tmpref.offset, tcgsize2size[OS_16]);
  214. a_load_ref_reg(list, OS_8, location^.size, tmpref,
  215. location^.register);
  216. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R12, 8, 40));
  217. a_reg_dealloc(list, NR_R12);
  218. end;
  219. 5 : begin
  220. a_reg_alloc(list, NR_R12);
  221. a_load_ref_reg(list, OS_32, location^.size, tmpref, NR_R12);
  222. inc(tmpref.offset, tcgsize2size[OS_32]);
  223. a_load_ref_reg(list, OS_8, location^.size, tmpref, location^.register);
  224. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R12, 8, 24));
  225. a_reg_dealloc(list, NR_R12);
  226. end;
  227. 6 : begin
  228. a_reg_alloc(list, NR_R12);
  229. a_load_ref_reg(list, OS_32, location^.size, tmpref, NR_R12);
  230. inc(tmpref.offset, tcgsize2size[OS_32]);
  231. a_load_ref_reg(list, OS_16, location^.size, tmpref, location^.register);
  232. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R12, 16, 16));
  233. a_reg_dealloc(list, NR_R12);
  234. end;
  235. 7 : begin
  236. a_reg_alloc(list, NR_R12);
  237. a_reg_alloc(list, NR_R0);
  238. a_load_ref_reg(list, OS_32, location^.size, tmpref, NR_R12);
  239. inc(tmpref.offset, tcgsize2size[OS_32]);
  240. a_load_ref_reg(list, OS_16, location^.size, tmpref, NR_R0);
  241. inc(tmpref.offset, tcgsize2size[OS_16]);
  242. a_load_ref_reg(list, OS_8, location^.size, tmpref, location^.register);
  243. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, NR_R0, NR_R12, 16, 16));
  244. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R0, 8, 8));
  245. a_reg_dealloc(list, NR_R0);
  246. a_reg_dealloc(list, NR_R12);
  247. end;
  248. else
  249. { still > 8 bytes to load, so load data single register now }
  250. a_load_ref_reg(list, location^.size, location^.size, tmpref,
  251. location^.register);
  252. end;
  253. end;
  254. LOC_REFERENCE:
  255. begin
  256. reference_reset_base(ref, location^.reference.index,
  257. location^.reference.offset);
  258. g_concatcopy(list, tmpref, ref, sizeleft);
  259. if assigned(location^.next) then
  260. internalerror(2005010710);
  261. end;
  262. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  263. case location^.size of
  264. OS_F32, OS_F64:
  265. a_loadfpu_ref_reg(list, location^.size, tmpref, location^.register);
  266. else
  267. internalerror(2002072801);
  268. end;
  269. LOC_VOID:
  270. { nothing to do }
  271. ;
  272. else
  273. internalerror(2002081103);
  274. end;
  275. inc(tmpref.offset, tcgsize2size[location^.size]);
  276. dec(sizeleft, tcgsize2size[location^.size]);
  277. location := location^.next;
  278. end;
  279. end;
  280. procedure tcgppc.a_paramaddr_ref(list: taasmoutput; const r: treference; const
  281. paraloc: tcgpara);
  282. var
  283. ref: treference;
  284. tmpreg: tregister;
  285. begin
  286. paraloc.check_simple_location;
  287. case paraloc.location^.loc of
  288. LOC_REGISTER, LOC_CREGISTER:
  289. a_loadaddr_ref_reg(list, r, paraloc.location^.register);
  290. LOC_REFERENCE:
  291. begin
  292. reference_reset(ref);
  293. ref.base := paraloc.location^.reference.index;
  294. ref.offset := paraloc.location^.reference.offset;
  295. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  296. a_loadaddr_ref_reg(list, r, tmpreg);
  297. a_load_reg_ref(list, OS_ADDR, OS_ADDR, tmpreg, ref);
  298. end;
  299. else
  300. internalerror(2002080701);
  301. end;
  302. end;
  303. { calling a procedure by name }
  304. procedure tcgppc.a_call_name(list: taasmoutput; const s: string);
  305. begin
  306. a_call_name_direct(list, s, true, true);
  307. end;
  308. procedure tcgppc.a_call_name_direct(list: taasmoutput; s: string; prependDot : boolean; addNOP : boolean);
  309. begin
  310. if (prependDot) then
  311. s := '.' + s;
  312. list.concat(taicpu.op_sym(A_BL, objectlibrary.newasmsymbol(s, AB_EXTERNAL,
  313. AT_FUNCTION)));
  314. if (addNOP) then
  315. list.concat(taicpu.op_none(A_NOP));
  316. { the compiler does not properly set this flag anymore in pass 1, and
  317. for now we only need it after pass 2 (I hope) (JM) }
  318. include(current_procinfo.flags, pi_do_call);
  319. end;
  320. { calling a procedure by address }
  321. procedure tcgppc.a_call_reg(list: taasmoutput; reg: tregister);
  322. var
  323. tmpref: treference;
  324. begin
  325. if (not (cs_littlesize in aktglobalswitches)) then begin
  326. { load actual function entry (reg contains the reference to the function descriptor)
  327. into R0 }
  328. reference_reset_base(tmpref, reg, 0);
  329. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_R0);
  330. { save TOC pointer in stackframe }
  331. reference_reset_base(tmpref, NR_STACK_POINTER_REG, LA_RTOC_ELF);
  332. a_load_reg_ref(list, OS_ADDR, OS_ADDR, NR_RTOC, tmpref);
  333. { move actual function pointer to CTR register }
  334. list.concat(taicpu.op_reg(A_MTCTR, NR_R0));
  335. { load new TOC pointer from function descriptor into RTOC register }
  336. reference_reset_base(tmpref, reg, tcgsize2size[OS_ADDR]);
  337. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  338. { load new environment pointer from function descriptor into R11 register }
  339. reference_reset_base(tmpref, reg, 2*tcgsize2size[OS_ADDR]);
  340. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_R11);
  341. { call function }
  342. list.concat(taicpu.op_none(A_BCTRL));
  343. end else begin
  344. { call ptrgl helper routine which expects the pointer to the function descriptor
  345. in R11 }
  346. a_load_reg_reg(list, OS_ADDR, OS_ADDR, reg, NR_R11);
  347. a_call_name_direct(list, 'ptrgl', true, false);
  348. end;
  349. { we need to load the old RTOC from stackframe because we changed it}
  350. reference_reset_base(tmpref, NR_STACK_POINTER_REG, LA_RTOC_ELF);
  351. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  352. include(current_procinfo.flags, pi_do_call);
  353. end;
  354. {********************** load instructions ********************}
  355. procedure tcgppc.a_load_const_reg(list: taasmoutput; size: TCGSize; a: aint;
  356. reg: TRegister);
  357. { loads a 32 bit constant into the given register, using an optimal instruction sequence.
  358. This is either LIS, LI or LI+ADDIS.
  359. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  360. sign extension was performed) }
  361. function load32bitconstant(list : taasmoutput; size : TCGSize; a : longint;
  362. reg : TRegister) : boolean;
  363. var
  364. is_half_signed : byte;
  365. begin
  366. { if the lower 16 bits are zero, do a single LIS }
  367. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  368. list.concat(taicpu.op_reg_const(A_LIS, reg, smallint(hi(a))));
  369. load32bitconstant := longint(a) < 0;
  370. end else begin
  371. is_half_signed := ord(smallint(lo(a)) < 0);
  372. list.concat(taicpu.op_reg_const(A_LI, reg, smallint(a and $ffff)));
  373. if smallint(hi(a) + is_half_signed) <> 0 then begin
  374. list.concat(taicpu.op_reg_reg_const(A_ADDIS, reg, reg, smallint(hi(a) + is_half_signed)));
  375. end;
  376. load32bitconstant := (smallint(a) < 0) or (a < 0);
  377. end;
  378. end;
  379. { R0-safe version of the above (ADDIS doesn't work the same way with R0 as base), without
  380. the return value }
  381. procedure load32bitconstantR0(list : taasmoutput; size : TCGSize; a : longint;
  382. reg : TRegister);
  383. begin
  384. { only 16 bit constant? (-2^15 <= a <= +2^15-1) }
  385. if (a >= low(smallint)) and (a <= high(smallint)) then begin
  386. list.concat(taicpu.op_reg_const(A_LI, reg, smallint(a)));
  387. end else begin
  388. { check if we have to start with LI or LIS, load as 32 bit constant }
  389. if ((a and $FFFF) <> 0) then begin
  390. list.concat(taicpu.op_reg_const(A_LIS, reg, smallint(a shr 16)));
  391. list.concat(taicpu.op_reg_reg_const(A_ORI, reg, reg, word(a)));
  392. end else begin
  393. list.concat(taicpu.op_reg_const(A_LIS, reg, smallint(a shr 16)));
  394. end;
  395. end;
  396. end;
  397. var
  398. extendssign : boolean;
  399. {$IFDEF EXTDEBUG}
  400. astring : string;
  401. {$ENDIF EXTDEBUG}
  402. begin
  403. {$IFDEF EXTDEBUG}
  404. astring := 'a_load_const reg ' + inttostr(hi(a)) + ' ' + inttostr(lo(a)) + ' ' + inttostr(ord(size)) + ' ' + inttostr(tcgsize2size[size]);
  405. list.concat(tai_comment.create(strpnew(astring)));
  406. {$ENDIF EXTDEBUG}
  407. if not (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  408. internalerror(2002090902);
  409. if (lo(a) = 0) and (hi(a) <> 0) then begin
  410. { load only upper 32 bits, and shift }
  411. load32bitconstant(list, size, hi(a), reg);
  412. list.concat(taicpu.op_reg_reg_const(A_SLDI, reg, reg, 32));
  413. end else begin
  414. { load lower 32 bits }
  415. extendssign := load32bitconstant(list, size, lo(a), reg);
  416. if (extendssign) and (hi(a) = 0) then
  417. { if upper 32 bits are zero, but loading the lower 32 bit resulted in automatic
  418. sign extension, clear those bits }
  419. a_load_reg_reg(list, OS_32, OS_64, reg, reg)
  420. else if (not
  421. ((extendssign and (longint(hi(a)) = -1)) or
  422. ((not extendssign) and (hi(a)=0)))
  423. ) then begin
  424. { only load the upper 32 bits, if the automatic sign extension is not okay,
  425. that is, _not_ if
  426. - loading the lower 32 bits resulted in -1 in the upper 32 bits, and the upper
  427. 32 bits should contain -1
  428. - loading the lower 32 bits resulted in 0 in the upper 32 bits, and the upper
  429. 32 bits should contain 0 }
  430. load32bitconstant(list, size, hi(a), NR_R12);
  431. { combine both registers }
  432. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, reg, NR_R12, 32, 0));
  433. end;
  434. end;
  435. end;
  436. procedure tcgppc.a_load_reg_ref(list: taasmoutput; fromsize, tosize: TCGSize;
  437. reg: tregister; const ref: treference);
  438. const
  439. StoreInstr: array[OS_8..OS_64, boolean, boolean] of TAsmOp =
  440. { indexed? updating?}
  441. (((A_STB, A_STBU), (A_STBX, A_STBUX)),
  442. ((A_STH, A_STHU), (A_STHX, A_STHUX)),
  443. ((A_STW, A_STWU), (A_STWX, A_STWUX)),
  444. ((A_STD, A_STDU), (A_STDX, A_STDUX))
  445. );
  446. var
  447. op: TAsmOp;
  448. ref2: TReference;
  449. begin
  450. ref2 := ref;
  451. fixref(list, ref2, tosize);
  452. if tosize in [OS_S8..OS_S64] then
  453. { storing is the same for signed and unsigned values }
  454. tosize := tcgsize(ord(tosize) - (ord(OS_S8) - ord(OS_8)));
  455. op := storeinstr[tcgsize2unsigned[tosize], ref2.index <> NR_NO, false];
  456. a_load_store(list, op, reg, ref2);
  457. end;
  458. procedure tcgppc.a_load_ref_reg(list: taasmoutput; fromsize, tosize: tcgsize;
  459. const ref: treference; reg: tregister);
  460. const
  461. LoadInstr: array[OS_8..OS_S64, boolean, boolean] of TAsmOp =
  462. { indexed? updating? }
  463. (((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  464. ((A_LHZ, A_LHZU), (A_LHZX, A_LHZUX)),
  465. ((A_LWZ, A_LWZU), (A_LWZX, A_LWZUX)),
  466. ((A_LD, A_LDU), (A_LDX, A_LDUX)),
  467. { 128bit stuff too }
  468. ((A_NONE, A_NONE), (A_NONE, A_NONE)),
  469. { there's no load-byte-with-sign-extend :( }
  470. ((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  471. ((A_LHA, A_LHAU), (A_LHAX, A_LHAUX)),
  472. { there's no load-word-arithmetic-indexed with update, simulate it in code :( }
  473. ((A_LWA, A_NOP), (A_LWAX, A_LWAUX)),
  474. ((A_LD, A_LDU), (A_LDX, A_LDUX))
  475. );
  476. var
  477. op: tasmop;
  478. ref2: treference;
  479. begin
  480. if not (fromsize in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  481. internalerror(2002090902);
  482. ref2 := ref;
  483. fixref(list, ref2, tosize);
  484. { the caller is expected to have adjusted the reference already
  485. in this case }
  486. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  487. fromsize := tosize;
  488. op := loadinstr[fromsize, ref2.index <> NR_NO, false];
  489. { there is no LWAU instruction, simulate using ADDI and LWA }
  490. if (op = A_NOP) then begin
  491. list.concat(taicpu.op_reg_reg_const(A_ADDI, reg, reg, ref2.offset));
  492. ref2.offset := 0;
  493. op := A_LWA;
  494. end;
  495. a_load_store(list, op, reg, ref2);
  496. { sign extend shortint if necessary, since there is no
  497. load instruction that does that automatically (JM) }
  498. if fromsize = OS_S8 then
  499. list.concat(taicpu.op_reg_reg(A_EXTSB, reg, reg));
  500. end;
  501. procedure tcgppc.a_load_reg_reg(list: taasmoutput; fromsize, tosize: tcgsize;
  502. reg1, reg2: tregister);
  503. const
  504. movemap : array[OS_8..OS_S128, OS_8..OS_S128] of tasmop = (
  505. { to -> OS_8 OS_16 OS_32 OS_64 OS_128 OS_S8 OS_S16 OS_S32 OS_S64 OS_S128 }
  506. { from }
  507. { OS_8 } (A_MR, A_RLDICL, A_RLDICL, A_RLDICL, A_NONE, A_RLDICL, A_RLDICL, A_RLDICL, A_RLDICL, A_NOP ),
  508. { OS_16 } (A_RLDICL, A_MR, A_RLDICL, A_RLDICL, A_NONE, A_RLDICL, A_RLDICL, A_RLDICL, A_RLDICL, A_NOP ),
  509. { OS_32 } (A_RLDICL, A_RLDICL, A_MR, A_RLDICL, A_NONE, A_RLDICL, A_RLDICL, A_RLDICL, A_RLDICL, A_NOP ),
  510. { OS_64 } (A_RLDICL, A_RLDICL, A_RLDICL, A_MR, A_NONE, A_RLDICL, A_RLDICL, A_RLDICL, A_RLDICL, A_NOP ),
  511. { OS_128 } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NOP ),
  512. { OS_S8 } (A_EXTSB, A_EXTSB, A_EXTSB, A_EXTSB, A_NONE, A_MR, A_EXTSB, A_EXTSB, A_EXTSB, A_NOP ),
  513. { OS_S16 } (A_RLDICL, A_EXTSH, A_EXTSH, A_EXTSH, A_NONE, A_EXTSB, A_MR, A_EXTSH, A_EXTSH, A_NOP ),
  514. { OS_S32 } (A_RLDICL, A_RLDICL, A_EXTSW, A_EXTSW, A_NONE, A_EXTSB, A_EXTSH, A_MR, A_EXTSW, A_NOP ),
  515. { OS_S64 } (A_RLDICL, A_RLDICL, A_RLDICL, A_MR, A_NONE, A_EXTSB, A_EXTSH, A_EXTSW, A_MR, A_NOP ),
  516. { OS_S128 } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NOP )
  517. );
  518. var
  519. instr: taicpu;
  520. op : tasmop;
  521. begin
  522. op := movemap[fromsize, tosize];
  523. case op of
  524. A_MR, A_EXTSB, A_EXTSH, A_EXTSW : instr := taicpu.op_reg_reg(op, reg2, reg1);
  525. A_RLDICL : instr := taicpu.op_reg_reg_const_const(A_RLDICL, reg2, reg1, 0, (8-tcgsize2size[fromsize])*8);
  526. else
  527. internalerror(2002090901);
  528. end;
  529. list.concat(instr);
  530. rg[R_INTREGISTER].add_move_instruction(instr);
  531. end;
  532. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize;
  533. reg1, reg2: tregister);
  534. var
  535. instr: taicpu;
  536. begin
  537. instr := taicpu.op_reg_reg(A_FMR, reg2, reg1);
  538. list.concat(instr);
  539. rg[R_FPUREGISTER].add_move_instruction(instr);
  540. end;
  541. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize;
  542. const ref: treference; reg: tregister);
  543. const
  544. FpuLoadInstr: array[OS_F32..OS_F64, boolean, boolean] of TAsmOp =
  545. { indexed? updating?}
  546. (((A_LFS, A_LFSU), (A_LFSX, A_LFSUX)),
  547. ((A_LFD, A_LFDU), (A_LFDX, A_LFDUX)));
  548. var
  549. op: tasmop;
  550. ref2: treference;
  551. begin
  552. { several functions call this procedure with OS_32 or OS_64
  553. so this makes life easier (FK) }
  554. case size of
  555. OS_32, OS_F32:
  556. size := OS_F32;
  557. OS_64, OS_F64, OS_C64:
  558. size := OS_F64;
  559. else
  560. internalerror(200201121);
  561. end;
  562. ref2 := ref;
  563. fixref(list, ref2, size);
  564. op := fpuloadinstr[size, ref2.index <> NR_NO, false];
  565. a_load_store(list, op, reg, ref2);
  566. end;
  567. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg:
  568. tregister; const ref: treference);
  569. const
  570. FpuStoreInstr: array[OS_F32..OS_F64, boolean, boolean] of TAsmOp =
  571. { indexed? updating? }
  572. (((A_STFS, A_STFSU), (A_STFSX, A_STFSUX)),
  573. ((A_STFD, A_STFDU), (A_STFDX, A_STFDUX)));
  574. var
  575. op: tasmop;
  576. ref2: treference;
  577. begin
  578. if not (size in [OS_F32, OS_F64]) then
  579. internalerror(200201122);
  580. ref2 := ref;
  581. fixref(list, ref2, size);
  582. op := fpustoreinstr[size, ref2.index <> NR_NO, false];
  583. a_load_store(list, op, reg, ref2);
  584. end;
  585. procedure tcgppc.a_op_const_reg(list: taasmoutput; Op: TOpCG; size: TCGSize; a:
  586. aint; reg: TRegister);
  587. begin
  588. a_op_const_reg_reg(list, op, size, a, reg, reg);
  589. end;
  590. procedure tcgppc.a_op_reg_reg(list: taasmoutput; Op: TOpCG; size: TCGSize; src,
  591. dst: TRegister);
  592. begin
  593. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  594. end;
  595. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  596. size: tcgsize; a: aint; src, dst: tregister);
  597. var
  598. useReg : boolean;
  599. procedure do_lo_hi(loOp, hiOp : TAsmOp);
  600. begin
  601. { Optimization for logical ops (excluding AND), trying to do this as efficiently
  602. as possible by only generating code for the affected halfwords. Note that all
  603. the instructions handled here must have "X op 0 = X" for every halfword. }
  604. usereg := false;
  605. if (aword(a) > high(dword)) then begin
  606. usereg := true;
  607. end else begin
  608. if (word(a) <> 0) then begin
  609. list.concat(taicpu.op_reg_reg_const(loOp, dst, src, word(a)));
  610. if (word(a shr 16) <> 0) then
  611. list.concat(taicpu.op_reg_reg_const(hiOp, dst, dst, word(a shr 16)));
  612. end else if (word(a shr 16) <> 0) then
  613. list.concat(taicpu.op_reg_reg_const(hiOp, dst, src, word(a shr 16)));
  614. end;
  615. end;
  616. procedure do_lo_hi_and;
  617. begin
  618. { optimization logical and with immediate: only use "andi." for 16 bit
  619. ands, otherwise use register method. Doing this for 32 bit constants
  620. would not give any advantage to the register method (via useReg := true),
  621. requiring a scratch register and three instructions. }
  622. usereg := false;
  623. if (aword(a) > high(word)) then
  624. usereg := true
  625. else
  626. list.concat(taicpu.op_reg_reg_const(A_ANDI_, dst, src, word(a)));
  627. end;
  628. var
  629. scratchreg: tregister;
  630. shift, shiftmask : longint;
  631. begin
  632. { subtraction is the same as addition with negative constant }
  633. if op = OP_SUB then begin
  634. a_op_const_reg_reg(list, OP_ADD, size, -a, src, dst);
  635. exit;
  636. end;
  637. { This case includes some peephole optimizations for the various operations,
  638. (e.g. AND, OR, XOR, ..) - can't this be done at some higher level,
  639. independent of architecture? }
  640. { assume that we do not need a scratch register for the operation }
  641. useReg := false;
  642. case (op) of
  643. OP_DIV, OP_IDIV:
  644. { actually, this method should be never called directly with OP_DIV or
  645. OP_IDIV, so just provide basic support.
  646. TODO: move division by constant stuff from nppcmat.pas here }
  647. if (a = 0) then
  648. internalerror(200208103)
  649. else if (a = 1) then
  650. a_load_reg_reg(list, size, size, src, dst)
  651. else
  652. usereg := true;
  653. OP_IMUL, OP_MUL:
  654. { idea: factorize constant multiplicands and use adds/shifts with few factors;
  655. however, even a 64 bit multiply is already quite fast on PPC64 }
  656. if (a = 0) then
  657. a_load_const_reg(list, size, 0, dst)
  658. else if (a = -1) then
  659. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst))
  660. else if (a = 1) then
  661. a_load_reg_reg(list, OS_INT, OS_INT, src, dst)
  662. else if ispowerof2(a, shift) then
  663. list.concat(taicpu.op_reg_reg_const(A_SLDI, dst, src, shift))
  664. else if (a >= low(smallint)) and (a <= high(smallint)) then
  665. list.concat(taicpu.op_reg_reg_const(A_MULLI, dst, src,
  666. smallint(a)))
  667. else
  668. usereg := true;
  669. OP_ADD:
  670. if (a = 0) then
  671. a_load_reg_reg(list, size, size, src, dst)
  672. else if (a >= low(smallint)) and (a <= high(smallint)) then
  673. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst, src, smallint(a)))
  674. else
  675. useReg := true;
  676. OP_OR:
  677. if (a = 0) then
  678. a_load_reg_reg(list, size, size, src, dst)
  679. else if (a = -1) then
  680. a_load_const_reg(list, size, -1, dst)
  681. else
  682. do_lo_hi(A_ORI, A_ORIS);
  683. OP_AND:
  684. if (a = 0) then
  685. a_load_const_reg(list, size, 0, dst)
  686. else if (a = -1) then
  687. a_load_reg_reg(list, size, size, src, dst)
  688. else
  689. do_lo_hi_and;
  690. OP_XOR:
  691. if (a = 0) then
  692. a_load_reg_reg(list, size, size, src, dst)
  693. else if (a = -1) then
  694. list.concat(taicpu.op_reg_reg(A_NOT, dst, src))
  695. else
  696. do_lo_hi(A_XORI, A_XORIS);
  697. OP_SHL, OP_SHR, OP_SAR:
  698. begin
  699. if (size in [OS_64, OS_S64]) then
  700. shift := 6
  701. else
  702. shift := 5;
  703. shiftmask := (1 shl shift)-1;
  704. if (a and shiftmask) <> 0 then
  705. list.concat(taicpu.op_reg_reg_const(
  706. TShiftOpCG2AsmOpConst[size in [OS_64, OS_S64], op], dst, src, a and shiftmask))
  707. else
  708. a_load_reg_reg(list, size, size, src, dst);
  709. if ((a shr shift) <> 0) then
  710. internalError(68991);
  711. end
  712. else
  713. internalerror(200109091);
  714. end;
  715. { if all else failed, load the constant in a register and then
  716. perform the operation }
  717. if (useReg) then begin
  718. scratchreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  719. a_load_const_reg(list, size, a, scratchreg);
  720. a_op_reg_reg_reg(list, op, size, scratchreg, src, dst);
  721. end;
  722. end;
  723. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  724. size: tcgsize; src1, src2, dst: tregister);
  725. const
  726. op_reg_reg_opcg2asmop32: array[TOpCG] of tasmop =
  727. (A_NONE, A_ADD, A_AND, A_DIVWU, A_DIVW, A_MULLW, A_MULLW, A_NEG, A_NOT, A_OR,
  728. A_SRAW, A_SLW, A_SRW, A_SUB, A_XOR);
  729. op_reg_reg_opcg2asmop64: array[TOpCG] of tasmop =
  730. (A_NONE, A_ADD, A_AND, A_DIVDU, A_DIVD, A_MULLD, A_MULLD, A_NEG, A_NOT, A_OR,
  731. A_SRAD, A_SLD, A_SRD, A_SUB, A_XOR);
  732. begin
  733. case op of
  734. OP_NEG, OP_NOT:
  735. begin
  736. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src1));
  737. if (op = OP_NOT) and not (size in [OS_64, OS_S64]) then
  738. { zero/sign extend result again, fromsize is not important here }
  739. a_load_reg_reg(list, OS_S64, size, dst, dst)
  740. end;
  741. else
  742. if (size in [OS_64, OS_S64]) then begin
  743. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src2,
  744. src1));
  745. end else begin
  746. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop32[op], dst, src2,
  747. src1));
  748. end;
  749. end;
  750. end;
  751. {*************** compare instructructions ****************}
  752. procedure tcgppc.a_cmp_const_reg_label(list: taasmoutput; size: tcgsize;
  753. cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  754. var
  755. scratch_register: TRegister;
  756. signed: boolean;
  757. begin
  758. signed := cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE];
  759. { in the following case, we generate more efficient code when }
  760. { signed is true }
  761. if (cmp_op in [OC_EQ, OC_NE]) and
  762. (aword(a) > $FFFF) then
  763. signed := true;
  764. if signed then
  765. if (a >= low(smallint)) and (a <= high(smallint)) then
  766. list.concat(taicpu.op_reg_reg_const(A_CMPDI, NR_CR0, reg, a))
  767. else begin
  768. scratch_register := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  769. a_load_const_reg(list, OS_64, a, scratch_register);
  770. list.concat(taicpu.op_reg_reg_reg(A_CMPD, NR_CR0, reg, scratch_register));
  771. end
  772. else if (aword(a) <= $FFFF) then
  773. list.concat(taicpu.op_reg_reg_const(A_CMPLDI, NR_CR0, reg, aword(a)))
  774. else begin
  775. scratch_register := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  776. a_load_const_reg(list, OS_64, a, scratch_register);
  777. list.concat(taicpu.op_reg_reg_reg(A_CMPLD, NR_CR0, reg,
  778. scratch_register));
  779. end;
  780. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  781. end;
  782. procedure tcgppc.a_cmp_reg_reg_label(list: taasmoutput; size: tcgsize;
  783. cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  784. var
  785. op: tasmop;
  786. begin
  787. if cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE] then
  788. if (size in [OS_64, OS_S64]) then
  789. op := A_CMPD
  790. else
  791. op := A_CMPW
  792. else
  793. if (size in [OS_64, OS_S64]) then
  794. op := A_CMPLD
  795. else
  796. op := A_CMPLW;
  797. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg2, reg1));
  798. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  799. end;
  800. procedure tcgppc.a_jmp_cond(list: taasmoutput; cond: TOpCmp; l: tasmlabel);
  801. begin
  802. a_jmp(list, A_BC, TOpCmp2AsmCond[cond], 0, l);
  803. end;
  804. procedure tcgppc.a_jmp_name(list: taasmoutput; const s: string);
  805. var
  806. p: taicpu;
  807. begin
  808. p := taicpu.op_sym(A_B, objectlibrary.newasmsymbol(s, AB_EXTERNAL,
  809. AT_LABEL));
  810. p.is_jmp := true;
  811. list.concat(p)
  812. end;
  813. procedure tcgppc.a_jmp_always(list: taasmoutput; l: tasmlabel);
  814. begin
  815. a_jmp(list, A_B, C_None, 0, l);
  816. end;
  817. procedure tcgppc.a_jmp_flags(list: taasmoutput; const f: TResFlags; l:
  818. tasmlabel);
  819. var
  820. c: tasmcond;
  821. begin
  822. c := flags_to_cond(f);
  823. a_jmp(list, A_BC, c.cond, c.cr - RS_CR0, l);
  824. end;
  825. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f:
  826. TResFlags; reg: TRegister);
  827. var
  828. testbit: byte;
  829. bitvalue: boolean;
  830. begin
  831. { get the bit to extract from the conditional register + its requested value (0 or 1) }
  832. testbit := ((f.cr - RS_CR0) * 4);
  833. case f.flag of
  834. F_EQ, F_NE:
  835. begin
  836. inc(testbit, 2);
  837. bitvalue := f.flag = F_EQ;
  838. end;
  839. F_LT, F_GE:
  840. begin
  841. bitvalue := f.flag = F_LT;
  842. end;
  843. F_GT, F_LE:
  844. begin
  845. inc(testbit);
  846. bitvalue := f.flag = F_GT;
  847. end;
  848. else
  849. internalerror(200112261);
  850. end;
  851. { load the conditional register in the destination reg }
  852. list.concat(taicpu.op_reg(A_MFCR, reg));
  853. { we will move the bit that has to be tested to bit 0 by rotating left }
  854. testbit := (testbit + 1) and 31;
  855. { extract bit }
  856. list.concat(taicpu.op_reg_reg_const_const_const(
  857. A_RLWINM,reg,reg,testbit,31,31));
  858. { if we need the inverse, xor with 1 }
  859. if not bitvalue then
  860. list.concat(taicpu.op_reg_reg_const(A_XORI, reg, reg, 1));
  861. end;
  862. { *********** entry/exit code and address loading ************ }
  863. procedure tcgppc.g_save_standard_registers(list: Taasmoutput);
  864. begin
  865. { this work is done in g_proc_entry }
  866. end;
  867. procedure tcgppc.g_restore_standard_registers(list: Taasmoutput);
  868. begin
  869. { this work is done in g_proc_exit }
  870. end;
  871. procedure tcgppc.calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  872. var
  873. reg : TSuperRegister;
  874. begin
  875. fprcount := 0;
  876. firstfpr := RS_F31;
  877. if not (po_assembler in current_procinfo.procdef.procoptions) then begin
  878. for reg := RS_F14 to RS_F31 do begin
  879. if reg in rg[R_FPUREGISTER].used_in_proc then begin
  880. fprcount := ord(RS_F31)-ord(reg)+1;
  881. firstfpr := reg;
  882. break;
  883. end;
  884. end;
  885. end;
  886. end;
  887. procedure tcgppc.calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  888. var
  889. reg : TSuperRegister;
  890. begin
  891. gprcount := 0;
  892. firstgpr := RS_R31;
  893. if not (po_assembler in current_procinfo.procdef.procoptions) then begin
  894. for reg := RS_R14 to RS_R31 do begin
  895. if reg in rg[R_INTREGISTER].used_in_proc then begin
  896. gprcount := ord(RS_R31)-ord(reg)+1;
  897. firstgpr := reg;
  898. break;
  899. end;
  900. end;
  901. end;
  902. end;
  903. procedure tcgppc.g_proc_entry(list: taasmoutput; localsize: longint;
  904. nostackframe: boolean);
  905. { generated the entry code of a procedure/function. Note: localsize is the
  906. sum of the size necessary for local variables and the maximum possible
  907. combined size of ALL the parameters of a procedure called by the current
  908. one.
  909. This procedure may be called before, as well as after g_return_from_proc
  910. is called. NOTE registers are not to be allocated through the register
  911. allocator here, because the register colouring has already occured !! }
  912. var
  913. firstregfpu, firstreggpr: TSuperRegister;
  914. href: treference;
  915. needslinkreg: boolean;
  916. regcount : TSuperRegister;
  917. fprcount, gprcount : aint;
  918. begin
  919. { CR and LR only have to be saved in case they are modified by the current
  920. procedure, but currently this isn't checked, so save them always
  921. following is the entry code as described in "Altivec Programming
  922. Interface Manual", bar the saving of AltiVec registers }
  923. a_reg_alloc(list, NR_STACK_POINTER_REG);
  924. a_reg_alloc(list, NR_R0);
  925. calcFirstUsedFPR(firstregfpu, fprcount);
  926. calcFirstUsedGPR(firstreggpr, gprcount);
  927. { calculate real stack frame size }
  928. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  929. gprcount, fprcount);
  930. { determine whether we need to save the link register }
  931. needslinkreg := ((not (po_assembler in current_procinfo.procdef.procoptions)) and
  932. (pi_do_call in current_procinfo.flags));
  933. { move link register to r0 }
  934. if (needslinkreg) then begin
  935. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  936. end;
  937. { save old stack frame pointer }
  938. if (localsize > 0) then begin
  939. a_reg_alloc(list, NR_OLD_STACK_POINTER_REG);
  940. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  941. end;
  942. { save registers, FPU first, then GPR }
  943. reference_reset_base(href, NR_STACK_POINTER_REG, -8);
  944. if (fprcount > 0) then begin
  945. for regcount := RS_F31 downto firstregfpu do begin
  946. a_loadfpu_reg_ref(list, OS_FLOAT, newreg(R_FPUREGISTER, regcount,
  947. R_SUBNONE), href);
  948. dec(href.offset, tcgsize2size[OS_FLOAT]);
  949. end;
  950. end;
  951. if (gprcount > 0) then begin
  952. for regcount := RS_R31 downto firstreggpr do begin
  953. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  954. R_SUBNONE), href);
  955. dec(href.offset, tcgsize2size[OS_INT]);
  956. end;
  957. end;
  958. { VMX registers not supported by FPC atm }
  959. { we may need to store R0 (=LR) ourselves }
  960. if (needslinkreg) then begin
  961. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  962. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  963. end;
  964. { create stack frame }
  965. if (not nostackframe) and (localsize > 0) then begin
  966. if (localsize <= high(smallint)) then begin
  967. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize);
  968. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  969. end else begin
  970. reference_reset_base(href, NR_NO, -localsize);
  971. { use R0 for loading the constant (which is definitely > 32k when entering
  972. this branch)
  973. Inlined at this position because it must not use temp registers because
  974. register allocations have already been done :( }
  975. { Code template:
  976. lis r0,ofs@highest
  977. ori r0,r0,ofs@higher
  978. sldi r0,r0,32
  979. oris r0,r0,ofs@h
  980. ori r0,r0,ofs@l
  981. }
  982. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  983. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  984. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  985. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  986. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  987. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  988. end;
  989. end;
  990. { CR register not used by FPC atm }
  991. { keep R1 allocated??? }
  992. a_reg_dealloc(list, NR_R0);
  993. end;
  994. procedure tcgppc.g_proc_exit(list: taasmoutput; parasize: longint; nostackframe:
  995. boolean);
  996. { This procedure may be called before, as well as after g_stackframe_entry }
  997. { is called. NOTE registers are not to be allocated through the register }
  998. { allocator here, because the register colouring has already occured !! }
  999. var
  1000. regcount, firstregfpu, firstreggpr: TSuperRegister;
  1001. href: treference;
  1002. needslinkreg : boolean;
  1003. localsize,
  1004. fprcount, gprcount: aint;
  1005. begin
  1006. calcFirstUsedFPR(firstregfpu, fprcount);
  1007. calcFirstUsedGPR(firstreggpr, gprcount);
  1008. { determine whether we need to restore the link register }
  1009. needslinkreg := ((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1010. (pi_do_call in current_procinfo.flags));
  1011. { calculate stack frame }
  1012. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1013. gprcount, fprcount);
  1014. { CR register not supported }
  1015. { restore stack pointer }
  1016. if (not nostackframe) and (localsize > 0) then begin
  1017. if (localsize <= high(smallint)) then begin
  1018. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1019. end else begin
  1020. reference_reset_base(href, NR_NO, localsize);
  1021. { use R0 for loading the constant (which is definitely > 32k when entering
  1022. this branch)
  1023. Inlined because it must not use temp registers because register allocations
  1024. have already been done :( }
  1025. { Code template:
  1026. lis r0,ofs@highest
  1027. ori r0,ofs@higher
  1028. sldi r0,r0,32
  1029. oris r0,r0,ofs@h
  1030. ori r0,r0,ofs@l
  1031. }
  1032. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1033. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1034. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1035. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1036. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1037. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1038. end;
  1039. end;
  1040. { load registers, FPR first, then GPR }
  1041. {$note ts:todo change order of loading}
  1042. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT]);
  1043. if (fprcount > 0) then begin
  1044. for regcount := RS_F31 downto firstregfpu do begin
  1045. a_loadfpu_ref_reg(list, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1046. R_SUBNONE));
  1047. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1048. end;
  1049. end;
  1050. if (gprcount > 0) then begin
  1051. for regcount := RS_R31 downto firstreggpr do begin
  1052. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1053. R_SUBNONE));
  1054. dec(href.offset, tcgsize2size[OS_INT]);
  1055. end;
  1056. end;
  1057. { VMX not supported... }
  1058. { restore LR (if needed) }
  1059. if (needslinkreg) then begin
  1060. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  1061. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1062. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1063. end;
  1064. { generate return instruction }
  1065. list.concat(taicpu.op_none(A_BLR));
  1066. end;
  1067. procedure tcgppc.a_loadaddr_ref_reg(list: taasmoutput; const ref: treference; r:
  1068. tregister);
  1069. var
  1070. ref2, tmpref: treference;
  1071. { register used to construct address }
  1072. tempreg : TRegister;
  1073. begin
  1074. ref2 := ref;
  1075. fixref(list, ref2, OS_64);
  1076. { load a symbol }
  1077. if assigned(ref2.symbol) or (hasLargeOffset(ref2)) then begin
  1078. { add the symbol's value to the base of the reference, and if the }
  1079. { reference doesn't have a base, create one }
  1080. reference_reset(tmpref);
  1081. tmpref.offset := ref2.offset;
  1082. tmpref.symbol := ref2.symbol;
  1083. tmpref.relsymbol := ref2.relsymbol;
  1084. { load 64 bit reference into r. If the reference already has a base register,
  1085. first load the 64 bit value into a temp register, then add it to the result
  1086. register rD }
  1087. if (ref2.base <> NR_NO) then begin
  1088. { already have a base register, so allocate a new one }
  1089. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1090. end else begin
  1091. tempreg := r;
  1092. end;
  1093. { code for loading a reference from a symbol into a register rD }
  1094. (*
  1095. lis rX,SYM@highest
  1096. ori rX,SYM@higher
  1097. sldi rX,rX,32
  1098. oris rX,rX,SYM@h
  1099. ori rX,rX,SYM@l
  1100. *)
  1101. tmpref.refaddr := addr_highest;
  1102. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1103. tmpref.refaddr := addr_higher;
  1104. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1105. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1106. tmpref.refaddr := addr_high;
  1107. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1108. tmpref.refaddr := addr_low;
  1109. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1110. { if there's already a base register, add the temp register contents to
  1111. the base register }
  1112. if (ref2.base <> NR_NO) then begin
  1113. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1114. end;
  1115. end else if ref2.offset <> 0 then begin
  1116. { no symbol, but offset <> 0 }
  1117. if ref2.base <> NR_NO then begin
  1118. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1119. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1120. occurs, so now only ref.offset has to be loaded }
  1121. end else begin
  1122. a_load_const_reg(list, OS_64, ref2.offset, r)
  1123. end;
  1124. end else if ref.index <> NR_NO then
  1125. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1126. else if (ref2.base <> NR_NO) and
  1127. (r <> ref2.base) then
  1128. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1129. else begin
  1130. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1131. end;
  1132. end;
  1133. { ************* concatcopy ************ }
  1134. const
  1135. maxmoveunit = 8;
  1136. procedure tcgppc.g_concatcopy(list: taasmoutput; const source, dest: treference;
  1137. len: aint);
  1138. var
  1139. countreg, tempreg: TRegister;
  1140. src, dst: TReference;
  1141. lab: tasmlabel;
  1142. count, count2: longint;
  1143. size: tcgsize;
  1144. begin
  1145. {$IFDEF extdebug}
  1146. if len > high(aint) then
  1147. internalerror(2002072704);
  1148. {$ENDIF extdebug}
  1149. { make sure short loads are handled as optimally as possible }
  1150. if (len <= maxmoveunit) and
  1151. (byte(len) in [1, 2, 4, 8]) then
  1152. begin
  1153. if len < 8 then
  1154. begin
  1155. size := int_cgsize(len);
  1156. a_load_ref_ref(list, size, size, source, dest);
  1157. end
  1158. else
  1159. begin
  1160. a_reg_alloc(list, NR_F0);
  1161. a_loadfpu_ref_reg(list, OS_F64, source, NR_F0);
  1162. a_loadfpu_reg_ref(list, OS_F64, NR_F0, dest);
  1163. a_reg_dealloc(list, NR_F0);
  1164. end;
  1165. exit;
  1166. end;
  1167. count := len div maxmoveunit;
  1168. reference_reset(src);
  1169. reference_reset(dst);
  1170. { load the address of source into src.base }
  1171. if (count > 4) or
  1172. not issimpleref(source) or
  1173. ((source.index <> NR_NO) and
  1174. ((source.offset + len) > high(smallint))) then begin
  1175. src.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1176. a_loadaddr_ref_reg(list, source, src.base);
  1177. end else begin
  1178. src := source;
  1179. end;
  1180. { load the address of dest into dst.base }
  1181. if (count > 4) or
  1182. not issimpleref(dest) or
  1183. ((dest.index <> NR_NO) and
  1184. ((dest.offset + len) > high(smallint))) then begin
  1185. dst.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1186. a_loadaddr_ref_reg(list, dest, dst.base);
  1187. end else begin
  1188. dst := dest;
  1189. end;
  1190. { generate a loop }
  1191. if count > 4 then begin
  1192. { the offsets are zero after the a_loadaddress_ref_reg and just
  1193. have to be set to 8. I put an Inc there so debugging may be
  1194. easier (should offset be different from zero here, it will be
  1195. easy to notice in the generated assembler }
  1196. inc(dst.offset, 8);
  1197. inc(src.offset, 8);
  1198. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, 8));
  1199. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, 8));
  1200. countreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1201. a_load_const_reg(list, OS_32, count, countreg);
  1202. { explicitely allocate F0 since it can be used safely here
  1203. (for holding date that's being copied) }
  1204. a_reg_alloc(list, NR_F0);
  1205. objectlibrary.getjumplabel(lab);
  1206. a_label(list, lab);
  1207. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1208. list.concat(taicpu.op_reg_ref(A_LFDU, NR_F0, src));
  1209. list.concat(taicpu.op_reg_ref(A_STFDU, NR_F0, dst));
  1210. a_jmp(list, A_BC, C_NE, 0, lab);
  1211. a_reg_dealloc(list, NR_F0);
  1212. len := len mod 8;
  1213. end;
  1214. count := len div 8;
  1215. { unrolled loop }
  1216. if count > 0 then begin
  1217. a_reg_alloc(list, NR_F0);
  1218. for count2 := 1 to count do begin
  1219. a_loadfpu_ref_reg(list, OS_F64, src, NR_F0);
  1220. a_loadfpu_reg_ref(list, OS_F64, NR_F0, dst);
  1221. inc(src.offset, 8);
  1222. inc(dst.offset, 8);
  1223. end;
  1224. a_reg_dealloc(list, NR_F0);
  1225. len := len mod 8;
  1226. end;
  1227. if (len and 4) <> 0 then begin
  1228. a_reg_alloc(list, NR_R0);
  1229. a_load_ref_reg(list, OS_32, OS_32, src, NR_R0);
  1230. a_load_reg_ref(list, OS_32, OS_32, NR_R0, dst);
  1231. inc(src.offset, 4);
  1232. inc(dst.offset, 4);
  1233. a_reg_dealloc(list, NR_R0);
  1234. end;
  1235. { copy the leftovers }
  1236. if (len and 2) <> 0 then begin
  1237. a_reg_alloc(list, NR_R0);
  1238. a_load_ref_reg(list, OS_16, OS_16, src, NR_R0);
  1239. a_load_reg_ref(list, OS_16, OS_16, NR_R0, dst);
  1240. inc(src.offset, 2);
  1241. inc(dst.offset, 2);
  1242. a_reg_dealloc(list, NR_R0);
  1243. end;
  1244. if (len and 1) <> 0 then begin
  1245. a_reg_alloc(list, NR_R0);
  1246. a_load_ref_reg(list, OS_8, OS_8, src, NR_R0);
  1247. a_load_reg_ref(list, OS_8, OS_8, NR_R0, dst);
  1248. a_reg_dealloc(list, NR_R0);
  1249. end;
  1250. end;
  1251. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def:
  1252. tdef);
  1253. var
  1254. hl: tasmlabel;
  1255. flags : TResFlags;
  1256. begin
  1257. if not (cs_check_overflow in aktlocalswitches) then
  1258. exit;
  1259. objectlibrary.getjumplabel(hl);
  1260. if not ((def.deftype = pointerdef) or
  1261. ((def.deftype = orddef) and
  1262. (torddef(def).typ in [u64bit, u16bit, u32bit, u8bit, uchar,
  1263. bool8bit, bool16bit, bool32bit]))) then
  1264. begin
  1265. { ... instructions setting overflow flag ...
  1266. mfxerf R0
  1267. mtcrf 128, R0
  1268. ble cr0, label }
  1269. list.concat(taicpu.op_reg(A_MFXER, NR_R0));
  1270. list.concat(taicpu.op_const_reg(A_MTCRF, 128, NR_R0));
  1271. flags.cr := RS_CR0;
  1272. flags.flag := F_LE;
  1273. a_jmp_flags(list, flags, hl);
  1274. end else
  1275. a_jmp_cond(list, OC_AE, hl);
  1276. a_call_name(list, 'FPC_OVERFLOW');
  1277. a_label(list, hl);
  1278. end;
  1279. procedure tcgppc.g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const
  1280. labelname: string; ioffset: longint);
  1281. procedure loadvmttor11;
  1282. var
  1283. href: treference;
  1284. begin
  1285. reference_reset_base(href, NR_R3, 0);
  1286. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R11);
  1287. end;
  1288. procedure op_onr11methodaddr;
  1289. var
  1290. href: treference;
  1291. begin
  1292. if (procdef.extnumber = $FFFF) then
  1293. Internalerror(200006139);
  1294. { call/jmp vmtoffs(%eax) ; method offs }
  1295. reference_reset_base(href, NR_R11,
  1296. procdef._class.vmtmethodoffset(procdef.extnumber));
  1297. if not (hasLargeOffset(href)) then begin
  1298. list.concat(taicpu.op_reg_reg_const(A_ADDIS, NR_R11, NR_R11,
  1299. smallint((href.offset shr 16) + ord(smallint(href.offset and $FFFF) <
  1300. 0))));
  1301. href.offset := smallint(href.offset and $FFFF);
  1302. end else
  1303. { add support for offsets > 16 bit }
  1304. internalerror(200510201);
  1305. list.concat(taicpu.op_reg_ref(A_LD, NR_R11, href));
  1306. { the loaded reference is a function descriptor reference, so deref again
  1307. (at ofs 0 there's the real pointer) }
  1308. {$warning ts:TODO: update GOT reference}
  1309. reference_reset_base(href, NR_R11, 0);
  1310. list.concat(taicpu.op_reg_ref(A_LD, NR_R11, href));
  1311. list.concat(taicpu.op_reg(A_MTCTR, NR_R11));
  1312. list.concat(taicpu.op_none(A_BCTR));
  1313. { NOP needed for the linker...? }
  1314. list.concat(taicpu.op_none(A_NOP));
  1315. end;
  1316. var
  1317. make_global: boolean;
  1318. begin
  1319. if (not (procdef.proctypeoption in [potype_function, potype_procedure])) then
  1320. Internalerror(200006137);
  1321. if not assigned(procdef._class) or
  1322. (procdef.procoptions * [po_classmethod, po_staticmethod,
  1323. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  1324. Internalerror(200006138);
  1325. if procdef.owner.symtabletype <> objectsymtable then
  1326. Internalerror(200109191);
  1327. make_global := false;
  1328. if (not current_module.is_unit) or
  1329. (cs_create_smart in aktmoduleswitches) or
  1330. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  1331. make_global := true;
  1332. if make_global then
  1333. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0))
  1334. else
  1335. List.concat(Tai_symbol.Createname(labelname, AT_FUNCTION, 0));
  1336. { set param1 interface to self }
  1337. g_adjust_self_value(list, procdef, ioffset);
  1338. if po_virtualmethod in procdef.procoptions then begin
  1339. loadvmttor11;
  1340. op_onr11methodaddr;
  1341. end else
  1342. {$note ts:todo add GOT change?? - think not needed :) }
  1343. list.concat(taicpu.op_sym(A_B,
  1344. objectlibrary.newasmsymbol('.' + procdef.mangledname, AB_EXTERNAL,
  1345. AT_FUNCTION)));
  1346. List.concat(Tai_symbol_end.Createname(labelname));
  1347. end;
  1348. {***************** This is private property, keep out! :) *****************}
  1349. function tcgppc.issimpleref(const ref: treference): boolean;
  1350. begin
  1351. if (ref.base = NR_NO) and
  1352. (ref.index <> NR_NO) then
  1353. internalerror(200208101);
  1354. result :=
  1355. not (assigned(ref.symbol)) and
  1356. (((ref.index = NR_NO) and
  1357. (ref.offset >= low(smallint)) and
  1358. (ref.offset <= high(smallint))) or
  1359. ((ref.index <> NR_NO) and
  1360. (ref.offset = 0)));
  1361. end;
  1362. function tcgppc.fixref(list: taasmoutput; var ref: treference; const size : TCgsize): boolean;
  1363. var
  1364. tmpreg: tregister;
  1365. needsAlign : boolean;
  1366. begin
  1367. result := false;
  1368. needsAlign := size in [OS_S32, OS_64, OS_S64];
  1369. if (ref.base = NR_NO) then begin
  1370. ref.base := ref.index;
  1371. ref.index := NR_NO;
  1372. end;
  1373. if (ref.base <> NR_NO) and (ref.index <> NR_NO) and
  1374. ((ref.offset <> 0) or assigned(ref.symbol)) then begin
  1375. result := true;
  1376. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1377. a_op_reg_reg_reg(list, OP_ADD, size, ref.base, ref.index, tmpreg);
  1378. ref.index := NR_NO;
  1379. ref.base := tmpreg;
  1380. end;
  1381. end;
  1382. procedure tcgppc.a_load_store(list: taasmoutput; op: tasmop; reg: tregister;
  1383. ref: treference);
  1384. var
  1385. tmpreg, tmpreg2: tregister;
  1386. tmpref: treference;
  1387. largeOffset: Boolean;
  1388. begin
  1389. { at this point there must not be a combination of values in the ref treference
  1390. which is not possible to directly map to instructions of the PowerPC architecture }
  1391. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1392. internalerror(200310131);
  1393. { for some instructions we need to check that the offset is divisible by at
  1394. least four. If not, add the bytes which are "off" to the base register and
  1395. adjust the offset accordingly }
  1396. case op of
  1397. A_LD, A_LDU, A_STD, A_STDU, A_LWA, A_LWAU :
  1398. if ((ref.offset mod 4) <> 0) then begin
  1399. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1400. if (ref.base <> NR_NO) then begin
  1401. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1402. ref.base := tmpreg;
  1403. end else begin
  1404. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1405. ref.base := tmpreg;
  1406. end;
  1407. ref.offset := (ref.offset div 4) * 4;
  1408. end;
  1409. end;
  1410. { if we have to load/store from a symbol or large addresses, use a temporary register
  1411. containing the address }
  1412. if assigned(ref.symbol) or (hasLargeOffset(ref)) then begin
  1413. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1414. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1415. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1416. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1417. ref.offset := 0;
  1418. end;
  1419. reference_reset(tmpref);
  1420. tmpref.symbol := ref.symbol;
  1421. tmpref.relsymbol := ref.relsymbol;
  1422. tmpref.offset := ref.offset;
  1423. if (ref.base <> NR_NO) then begin
  1424. { As long as the TOC isn't working we try to achieve highest speed (in this
  1425. case by allowing instructions execute in parallel) as possible at the cost
  1426. of using another temporary register. So the code template when there is
  1427. a base register and an offset is the following:
  1428. lis rT1, SYM+offs@highest
  1429. ori rT1, rT1, SYM+offs@higher
  1430. lis rT2, SYM+offs@hi
  1431. ori rT2, SYM+offs@lo
  1432. rldimi rT2, rT1, 32
  1433. <op>X reg, base, rT2
  1434. }
  1435. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1436. tmpref.refaddr := addr_highest;
  1437. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1438. tmpref.refaddr := addr_higher;
  1439. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1440. tmpref.refaddr := addr_high;
  1441. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  1442. tmpref.refaddr := addr_low;
  1443. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  1444. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  1445. reference_reset(tmpref);
  1446. tmpref.base := ref.base;
  1447. tmpref.index := tmpreg2;
  1448. case op of
  1449. { the code generator doesn't generate update instructions anyway }
  1450. A_LBZ : op := A_LBZX;
  1451. A_LHZ : op := A_LHZX;
  1452. A_LWZ : op := A_LWZX;
  1453. A_LD : op := A_LDX;
  1454. A_LHA : op := A_LHAX;
  1455. A_LWA : op := A_LWAX;
  1456. A_LFS : op := A_LFSX;
  1457. A_LFD : op := A_LFDX;
  1458. A_STB : op := A_STBX;
  1459. A_STH : op := A_STHX;
  1460. A_STW : op := A_STWX;
  1461. A_STD : op := A_STDX;
  1462. A_STFS : op := A_STFSX;
  1463. A_STFD : op := A_STFDX;
  1464. else
  1465. { unknown load/store opcode }
  1466. internalerror(2005101302);
  1467. end;
  1468. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1469. end else begin
  1470. { when accessing value from a reference without a base register, use the
  1471. following code template:
  1472. lis rT,SYM+offs@highesta
  1473. ori rT,SYM+offs@highera
  1474. sldi rT,rT,32
  1475. oris rT,rT,SYM+offs@ha
  1476. ld rD,SYM+offs@l(rT)
  1477. }
  1478. tmpref.refaddr := addr_highesta;
  1479. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1480. tmpref.refaddr := addr_highera;
  1481. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1482. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  1483. tmpref.refaddr := addr_higha;
  1484. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  1485. tmpref.base := tmpreg;
  1486. tmpref.refaddr := addr_low;
  1487. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1488. end;
  1489. end else begin
  1490. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1491. end;
  1492. end;
  1493. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1494. crval: longint; l: tasmlabel);
  1495. var
  1496. p: taicpu;
  1497. begin
  1498. p := taicpu.op_sym(op, objectlibrary.newasmsymbol(l.name, AB_EXTERNAL,
  1499. AT_LABEL));
  1500. if op <> A_B then
  1501. create_cond_norm(c, crval, p.condition);
  1502. p.is_jmp := true;
  1503. list.concat(p)
  1504. end;
  1505. function tcgppc.hasLargeOffset(const ref : TReference) : Boolean;
  1506. begin
  1507. { this rather strange calculation is required because offsets of TReferences are unsigned }
  1508. result := aword(ref.offset-low(smallint)) > high(smallint)-low(smallint);
  1509. end;
  1510. begin
  1511. cg := tcgppc.create;
  1512. end.