cgppc.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. {
  2. Copyright (c) 2006 by Florian Klaempfl
  3. This unit implements the common part of 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 cgppc;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,symtype,symdef,
  22. cgbase,cgobj,
  23. aasmbase,aasmdef,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,rgcpu,
  25. parabase;
  26. type
  27. tcgppcgen = class(tcg)
  28. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara); override;
  29. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister); override;
  30. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  31. { stores the contents of register reg to the memory location described by
  32. ref }
  33. procedure a_load_reg_ref(list: TAsmList; fromsize, tosize: TCGSize;
  34. reg: tregister; const ref: treference); override;
  35. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  36. { fpu move instructions }
  37. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  38. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  39. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  40. { overflow checking }
  41. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);override;
  42. { entry code }
  43. procedure g_profilecode(list: TAsmList); override;
  44. procedure a_jmp_flags(list: TAsmList; const f: TResFlags; l: tasmlabel); override;
  45. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  46. procedure g_maybe_got_init(list: TAsmList); override;
  47. procedure get_aix_toc_sym(list: TAsmList; const symname: string; const flags: tindsymflags; out ref: treference; force_direct_toc: boolean);
  48. procedure g_load_check_simple(list: TAsmList; const ref: treference; size: aint);
  49. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  50. { returns true if the offset of the given reference can not be }
  51. { represented by a 16 bit immediate as required by some PowerPC }
  52. { instructions }
  53. function hasLargeOffset(const ref : TReference) : Boolean; inline;
  54. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  55. protected
  56. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister; override;
  57. { Make sure ref is a valid reference for the PowerPC and sets the }
  58. { base to the value of the index if (base = R_NO). }
  59. { Returns true if the reference contained a base, index and an }
  60. { offset or symbol, in which case the base will have been changed }
  61. { to a tempreg (which has to be freed by the caller) containing }
  62. { the sum of part of the original reference }
  63. function fixref(list: TAsmList; var ref: treference): boolean;
  64. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  65. procedure a_load_store(list:TAsmList;op: tasmop;reg:tregister;ref: treference);virtual;
  66. { creates the correct branch instruction for a given combination }
  67. { of asmcondflags and destination addressing mode }
  68. procedure a_jmp(list: TAsmList; op: tasmop;
  69. c: tasmcondflag; crval: longint; l: tasmlabel);
  70. function get_rtoc_offset: longint;
  71. function save_lr_in_prologue: boolean;
  72. function load_got_symbol(list : TAsmList; const symbol : string; const flags: tindsymflags) : tregister;
  73. end;
  74. TPPCAsmData = class(TAsmDataDef)
  75. private
  76. { number of entries in the TOC }
  77. fdirecttocentries,
  78. { number of fake TOC subsections we have created }
  79. ftocsections,
  80. { number of fake TOC entries in the current TOC subsection }
  81. fcurrenttocentries: longint;
  82. public
  83. procedure GetNextSmallTocEntry(out tocnr, entrynr: longint);
  84. property DirectTOCEntries: longint read fdirecttocentries write fdirecttocentries;
  85. end;
  86. TTOCAsmSymbol = class(TAsmSymbol)
  87. private
  88. { we split the toc into several sections of 32KB each, this number
  89. indicates which subsection this symbol is defined in }
  90. ftocsecnr: longint;
  91. public
  92. property TocSecNr: longint read ftocsecnr;
  93. end;
  94. const
  95. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  96. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  97. TocSecBaseName = 'toc_table';
  98. {$ifdef extdebug}
  99. function ref2string(const ref : treference) : string;
  100. function cgop2string(const op : TOpCg) : String;
  101. {$endif extdebug}
  102. implementation
  103. uses
  104. {$ifdef extdebug}sysutils,{$endif}
  105. globals,verbose,systems,cutils,
  106. symconst,symsym,symtable,fmodule,
  107. rgobj,tgobj,cpupi,procinfo,paramgr;
  108. { We know that macos_direct_globals is a const boolean
  109. but we don't care about this warning }
  110. {$NOTE Is macos_direct_globals still useful?}
  111. {$WARN 6018 OFF}
  112. {$ifdef extdebug}
  113. function ref2string(const ref : treference) : string;
  114. begin
  115. result := 'base : ' + inttostr(ord(ref.base)) + ' index : ' + inttostr(ord(ref.index)) + ' refaddr : ' + inttostr(ord(ref.refaddr)) + ' offset : ' + inttostr(ref.offset) + ' symbol : ';
  116. if (assigned(ref.symbol)) then
  117. result := result + ref.symbol.name;
  118. end;
  119. function cgop2string(const op : TOpCg) : String;
  120. const
  121. opcg_strings : array[TOpCg] of string[6] = (
  122. 'None', 'Move', 'Add', 'And', 'Div', 'IDiv', 'IMul', 'Mul',
  123. 'Neg', 'Not', 'Or', 'Sar', 'Shl', 'Shr', 'Sub', 'Xor', 'Rol', 'Ror'
  124. );
  125. begin
  126. result := opcg_strings[op];
  127. end;
  128. {$endif extdebug}
  129. function tcgppcgen.hasLargeOffset(const ref : TReference) : Boolean;
  130. begin
  131. result := aword(ref.offset-low(smallint)) > high(smallint)-low(smallint);
  132. end;
  133. function tcgppcgen.save_lr_in_prologue: boolean;
  134. begin
  135. result:=
  136. (not (po_assembler in current_procinfo.procdef.procoptions) and
  137. ((pi_do_call in current_procinfo.flags) or
  138. (cs_profile in init_settings.moduleswitches))) or
  139. ([cs_lineinfo,cs_debuginfo] * current_settings.moduleswitches <> []);
  140. end;
  141. procedure tcgppcgen.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  142. var
  143. ref: treference;
  144. tmpreg: tregister;
  145. begin
  146. paraloc.check_simple_location;
  147. paramanager.allocparaloc(list,paraloc.location);
  148. case paraloc.location^.loc of
  149. LOC_REGISTER,LOC_CREGISTER:
  150. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  151. LOC_REFERENCE:
  152. begin
  153. reference_reset(ref,paraloc.alignment,[]);
  154. ref.base := paraloc.location^.reference.index;
  155. ref.offset := paraloc.location^.reference.offset;
  156. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  157. a_loadaddr_ref_reg(list,r,tmpreg);
  158. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  159. end;
  160. else
  161. internalerror(2002080701);
  162. end;
  163. end;
  164. procedure tcgppcgen.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  165. var
  166. tmpreg: tregister;
  167. cntlzop: tasmop;
  168. bitsizem1: longint;
  169. begin
  170. { we only have a cntlz(w|d) instruction, which corresponds to bsr(x)
  171. (well, regsize_in_bits - bsr(x), as x86 numbers bits in reverse).
  172. Fortunately, bsf(x) can be calculated easily based on that, see
  173. "Figure 5-13. Number of Powers of 2 Code Sequence" in the PowerPC
  174. Compiler Writer's Guide
  175. }
  176. if srcsize in [OS_64,OS_S64] then
  177. begin
  178. {$ifdef powerpc64}
  179. cntlzop:=A_CNTLZD;
  180. {$else}
  181. internalerror(2015022601);
  182. {$endif}
  183. bitsizem1:=63;
  184. end
  185. else
  186. begin
  187. cntlzop:=A_CNTLZW;
  188. bitsizem1:=31;
  189. end;
  190. if not reverse then
  191. begin
  192. { cntlzw(src and -src) }
  193. tmpreg:=getintregister(list,srcsize);
  194. { don't use a_op_reg_reg, as this will adjust the result
  195. after the neg in case of a non-32/64 bit operation, which
  196. is not necessary since we're only using it as an
  197. AND-mask }
  198. list.concat(taicpu.op_reg_reg(A_NEG,tmpreg,src));
  199. a_op_reg_reg(list,OP_AND,srcsize,src,tmpreg);
  200. end
  201. else
  202. tmpreg:=src;
  203. { count leading zeroes }
  204. list.concat(taicpu.op_reg_reg(cntlzop,dst,tmpreg));
  205. { (bitsize-1) - cntlz (which is 32/64 in case src was 0) }
  206. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,dst,dst,bitsizem1));
  207. { set to 255 is source was 0 }
  208. a_op_const_reg(list,OP_AND,dstsize,255,dst);
  209. end;
  210. procedure tcgppcgen.g_maybe_got_init(list: TAsmList);
  211. var
  212. instr: taicpu;
  213. cond: tasmcond;
  214. savedlr: boolean;
  215. begin
  216. if not(po_assembler in current_procinfo.procdef.procoptions) then
  217. begin
  218. if (cs_create_pic in current_settings.moduleswitches) and
  219. (pi_needs_got in current_procinfo.flags) then
  220. case target_info.system of
  221. system_powerpc_darwin,
  222. system_powerpc64_darwin:
  223. begin
  224. savedlr:=save_lr_in_prologue;
  225. if not savedlr then
  226. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  227. fillchar(cond,sizeof(cond),0);
  228. cond.simple:=false;
  229. cond.bo:=20;
  230. cond.bi:=31;
  231. instr:=taicpu.op_sym(A_BCL,current_procinfo.CurrGOTLabel);
  232. instr.setcondition(cond);
  233. list.concat(instr);
  234. a_label(list,current_procinfo.CurrGOTLabel);
  235. a_reg_alloc(list,current_procinfo.got);
  236. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  237. if not savedlr or
  238. { in the following case lr is saved, but not restored }
  239. { (happens e.g. when generating debug info for leaf }
  240. { procedures) }
  241. not(pi_do_call in current_procinfo.flags) then
  242. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  243. end;
  244. else
  245. ;
  246. end;
  247. end;
  248. end;
  249. function tcgppcgen.g_indirect_sym_load(list: TAsmList; const symname: string; const flags: tindsymflags): tregister;
  250. begin
  251. case target_info.system of
  252. system_powerpc_aix,
  253. system_powerpc64_aix:
  254. result:=load_got_symbol(list,symname,flags);
  255. else
  256. result:=inherited;
  257. end;
  258. end;
  259. function tcgppcgen.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  260. var
  261. stubname: string;
  262. instr: taicpu;
  263. href: treference;
  264. l1: tasmsymbol;
  265. localgotlab: tasmlabel;
  266. cond: tasmcond;
  267. stubalign: byte;
  268. begin
  269. { function declared in the current unit? }
  270. { doesn't work correctly, because this will also return a hit if we }
  271. { previously took the address of an external procedure. It doesn't }
  272. { really matter, the linker will remove all unnecessary stubs. }
  273. stubname := 'L'+s+'$stub';
  274. result := current_asmdata.getasmsymbol(stubname);
  275. if assigned(result) then
  276. exit;
  277. if current_asmdata.asmlists[al_imports]=nil then
  278. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  279. if (cs_create_pic in current_settings.moduleswitches) then
  280. stubalign:=32
  281. else
  282. stubalign:=16;
  283. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',stubalign);
  284. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION,voidcodepointertype);
  285. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  286. { register as a weak symbol if necessary }
  287. if weak then
  288. current_asmdata.weakrefasmsymbol(s,AT_FUNCTION);
  289. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  290. l1 := current_asmdata.DefineAsmSymbol('L'+s+'$lazy_ptr',AB_LOCAL,AT_DATA,voidpointertype);
  291. reference_reset_symbol(href,l1,0,sizeof(pint),[]);
  292. href.refaddr := addr_higha;
  293. if (cs_create_pic in current_settings.moduleswitches) then
  294. begin
  295. current_asmdata.getjumplabel(localgotlab);
  296. href.relsymbol:=localgotlab;
  297. fillchar(cond,sizeof(cond),0);
  298. cond.simple:=false;
  299. cond.bo:=20;
  300. cond.bi:=31;
  301. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MFLR,NR_R0));
  302. instr:=taicpu.op_sym(A_BCL,localgotlab);
  303. instr.setcondition(cond);
  304. current_asmdata.asmlists[al_imports].concat(instr);
  305. a_label(current_asmdata.asmlists[al_imports],localgotlab);
  306. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MFLR,NR_R11));
  307. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_reg_ref(A_ADDIS,NR_R11,NR_R11,href));
  308. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MTLR,NR_R0));
  309. end
  310. else
  311. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  312. href.refaddr := addr_low;
  313. href.base := NR_R11;
  314. {$ifndef cpu64bitaddr}
  315. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  316. {$else cpu64bitaddr}
  317. { darwin/ppc64 uses a 32 bit absolute address here, strange... }
  318. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LDU,NR_R12,href));
  319. {$endif cpu64bitaddr}
  320. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MTCTR,NR_R12));
  321. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_BCTR));
  322. new_section(current_asmdata.asmlists[al_imports],sec_data_lazy,'',sizeof(pint));
  323. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  324. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  325. current_asmdata.asmlists[al_imports].concat(tai_const.createname('dyld_stub_binding_helper',0));
  326. end;
  327. procedure tcgppcgen.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  328. var
  329. ref2, tmpref: treference;
  330. begin
  331. ref2 := ref;
  332. fixref(list,ref2);
  333. if assigned(ref2.symbol) then
  334. begin
  335. if target_info.system = system_powerpc_macosclassic then
  336. begin
  337. if macos_direct_globals then
  338. begin
  339. reference_reset(tmpref,ref2.alignment,ref2.volatility);
  340. tmpref.offset := ref2.offset;
  341. tmpref.symbol := ref2.symbol;
  342. tmpref.base := NR_NO;
  343. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  344. end
  345. else
  346. begin
  347. reference_reset(tmpref,ref2.alignment,ref2.volatility);
  348. tmpref.symbol := ref2.symbol;
  349. tmpref.offset := 0;
  350. tmpref.base := NR_RTOC;
  351. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  352. if ref2.offset<>0 then
  353. a_op_const_reg(list,OP_ADD,OS_ADDR,ref2.offset,r);
  354. end;
  355. if ref2.base <> NR_NO then
  356. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  357. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  358. end
  359. else
  360. begin
  361. { add the symbol's value to the base of the reference, and if the }
  362. { reference doesn't have a base, create one }
  363. reference_reset(tmpref,ref2.alignment,ref2.volatility);
  364. tmpref.offset := ref2.offset;
  365. tmpref.symbol := ref2.symbol;
  366. tmpref.relsymbol := ref2.relsymbol;
  367. tmpref.refaddr := addr_higha;
  368. if ref2.base<> NR_NO then
  369. begin
  370. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  371. ref2.base,tmpref));
  372. end
  373. else
  374. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  375. tmpref.base := NR_NO;
  376. tmpref.refaddr := addr_low;
  377. { can be folded with one of the next instructions by the }
  378. { optimizer probably }
  379. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  380. end
  381. end
  382. else if ref2.offset <> 0 Then
  383. if ref2.base <> NR_NO then
  384. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref2.offset,ref2.base,r)
  385. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  386. { occurs, so now only ref.offset has to be loaded }
  387. else
  388. a_load_const_reg(list,OS_ADDR,ref2.offset,r)
  389. else if ref2.index <> NR_NO Then
  390. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  391. else if (ref2.base <> NR_NO) and
  392. (r <> ref2.base) then
  393. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  394. else
  395. list.concat(taicpu.op_reg_const(A_LI,r,0));
  396. end;
  397. { calling a procedure by address }
  398. procedure tcgppcgen.a_call_reg(list : TAsmList;reg: tregister);
  399. var
  400. tmpref: treference;
  401. tmpreg: tregister;
  402. toc_offset: longint;
  403. begin
  404. tmpreg:=NR_NO;
  405. if target_info.system in systems_aix then
  406. begin
  407. { load function address in R0, and swap "reg" for R0 }
  408. reference_reset_base(tmpref,reg,0,ctempposinvalid,sizeof(pint),[]);
  409. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  410. tmpreg:=reg;
  411. { no need to allocate/free R0, is already allocated by call node
  412. because it's a volatile register }
  413. reg:=NR_R0;
  414. end;
  415. list.concat(taicpu.op_reg(A_MTCTR,reg));
  416. if target_info.system in systems_aix then
  417. begin
  418. { load target TOC and possible link register }
  419. reference_reset_base(tmpref,tmpreg,sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  420. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_RTOC);
  421. tmpref.offset:=2*sizeof(pint);
  422. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R11);
  423. end
  424. else if target_info.abi=abi_powerpc_elfv2 then
  425. begin
  426. { functions must be called via R12 for this ABI }
  427. if reg<>NR_R12 then
  428. begin
  429. getcpuregister(list,NR_R12);
  430. a_load_reg_reg(list,OS_ADDR,OS_ADDR,reg,NR_R12)
  431. end;
  432. end;
  433. list.concat(taicpu.op_none(A_BCTRL));
  434. if target_info.abi in abis_ppc_toc then
  435. begin
  436. if (target_info.abi=abi_powerpc_elfv2) and
  437. (reg<>NR_R12) then
  438. ungetcpuregister(list,NR_R12);
  439. { restore our TOC }
  440. toc_offset:=get_rtoc_offset;
  441. reference_reset_base(tmpref,NR_STACK_POINTER_REG,toc_offset,ctempposinvalid,sizeof(pint),[]);
  442. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_RTOC);
  443. end;
  444. include(current_procinfo.flags,pi_do_call);
  445. end;
  446. procedure tcgppcgen.a_load_reg_ref(list: TAsmList; fromsize, tosize: TCGSize;
  447. reg: tregister; const ref: treference);
  448. const
  449. StoreInstr: array[OS_8..OS_INT, boolean, boolean] of TAsmOp =
  450. { indexed? updating?}
  451. (((A_STB, A_STBU), (A_STBX, A_STBUX)),
  452. ((A_STH, A_STHU), (A_STHX, A_STHUX)),
  453. ((A_STW, A_STWU), (A_STWX, A_STWUX))
  454. {$ifdef cpu64bitalu}
  455. ,
  456. ((A_STD, A_STDU), (A_STDX, A_STDUX))
  457. {$endif cpu64bitalu}
  458. );
  459. var
  460. ref2: TReference;
  461. op: TAsmOp;
  462. begin
  463. if not (fromsize in [OS_8..OS_INT,OS_S8..OS_SINT]) then
  464. internalerror(2002090911);
  465. if not (tosize in [OS_8..OS_INT,OS_S8..OS_SINT]) then
  466. internalerror(2002090905);
  467. if tosize in [OS_S8..OS_SINT] then
  468. { storing is the same for signed and unsigned values }
  469. tosize := tcgsize(ord(tosize) - (ord(OS_S8) - ord(OS_8)));
  470. ref2 := ref;
  471. fixref(list, ref2);
  472. op := storeinstr[tcgsize2unsigned[tosize], ref2.index <> NR_NO, false];
  473. a_load_store(list, op, reg, ref2);
  474. end;
  475. procedure tcgppcgen.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  476. var
  477. op: tasmop;
  478. instr: taicpu;
  479. begin
  480. if not(fromsize in [OS_F32,OS_F64]) or
  481. not(tosize in [OS_F32,OS_F64]) then
  482. internalerror(2006123110);
  483. if (tosize < fromsize) then
  484. op:=A_FRSP
  485. else
  486. op:=A_FMR;
  487. instr := taicpu.op_reg_reg(op,reg2,reg1);
  488. list.concat(instr);
  489. if (op = A_FMR) then
  490. rg[R_FPUREGISTER].add_move_instruction(instr);
  491. end;
  492. procedure tcgppcgen.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  493. const
  494. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  495. { indexed? updating?}
  496. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  497. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  498. var
  499. op: tasmop;
  500. ref2: treference;
  501. begin
  502. if target_info.system in systems_aix then
  503. g_load_check_simple(list,ref,65536);
  504. if not(fromsize in [OS_F32,OS_F64]) or
  505. not(tosize in [OS_F32,OS_F64]) then
  506. internalerror(200201121);
  507. ref2 := ref;
  508. fixref(list,ref2);
  509. op := fpuloadinstr[fromsize,ref2.index <> NR_NO,false];
  510. a_load_store(list,op,reg,ref2);
  511. if (fromsize > tosize) then
  512. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  513. end;
  514. procedure tcgppcgen.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  515. const
  516. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  517. { indexed? updating?}
  518. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  519. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  520. var
  521. op: tasmop;
  522. ref2: treference;
  523. reg2: tregister;
  524. begin
  525. if not(fromsize in [OS_F32,OS_F64]) or
  526. not(tosize in [OS_F32,OS_F64]) then
  527. internalerror(200201122);
  528. ref2 := ref;
  529. fixref(list,ref2);
  530. op := fpustoreinstr[tosize,ref2.index <> NR_NO,false];
  531. { some PPCs have a bug whereby storing a double to memory }
  532. { as single corrupts the value -> convert double to single }
  533. { first (bug confirmed on some G4s, but not on G5s) }
  534. if (tosize < fromsize) and
  535. (current_settings.cputype < cpu_PPC970) then
  536. begin
  537. reg2:=getfpuregister(list,tosize);
  538. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg2);
  539. reg:=reg2;
  540. end;
  541. a_load_store(list,op,reg,ref2);
  542. end;
  543. procedure tcgppcgen.g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);
  544. var
  545. hl : tasmlabel;
  546. flags : TResFlags;
  547. begin
  548. if not(cs_check_overflow in current_settings.localswitches) then
  549. exit;
  550. current_asmdata.getjumplabel(hl);
  551. if not ((def.typ=pointerdef) or
  552. ((def.typ=orddef) and
  553. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  554. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  555. begin
  556. if (current_settings.optimizecputype >= cpu_ppc970) or
  557. (current_settings.cputype >= cpu_ppc970) then
  558. begin
  559. { ... instructions setting overflow flag ...
  560. mfxerf R0
  561. mtcrf 128, R0
  562. ble cr0, label }
  563. list.concat(taicpu.op_reg(A_MFXER, NR_R0));
  564. list.concat(taicpu.op_const_reg(A_MTCRF, 128, NR_R0));
  565. flags.cr := RS_CR0;
  566. flags.flag := F_LE;
  567. a_jmp_flags(list, flags, hl);
  568. end
  569. else
  570. begin
  571. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  572. a_jmp(list,A_BC,C_NO,7,hl)
  573. end;
  574. end
  575. else
  576. a_jmp_cond(list,OC_AE,hl);
  577. a_call_name(list,'FPC_OVERFLOW',false);
  578. a_label(list,hl);
  579. end;
  580. procedure tcgppcgen.g_profilecode(list: TAsmList);
  581. var
  582. paraloc1 : tcgpara;
  583. pd : tprocdef;
  584. begin
  585. if (target_info.system in [system_powerpc_darwin]) then
  586. begin
  587. pd:=search_system_proc('mcount');
  588. paraloc1.init;
  589. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  590. a_load_reg_cgpara(list,OS_ADDR,NR_R0,paraloc1);
  591. paramanager.freecgpara(list,paraloc1);
  592. paraloc1.done;
  593. allocallcpuregisters(list);
  594. a_call_name(list,'mcount',false);
  595. deallocallcpuregisters(list);
  596. a_reg_dealloc(list,NR_R0);
  597. end;
  598. end;
  599. procedure tcgppcgen.a_jmp_flags(list: TAsmList; const f: TResFlags; l: tasmlabel);
  600. var
  601. c: tasmcond;
  602. f2: TResFlags;
  603. testbit: longint;
  604. begin
  605. f2:=f;
  606. testbit:=(f.cr-RS_CR0)*4;
  607. case f.flag of
  608. F_FA:
  609. f2.flag:=F_GT;
  610. F_FAE:
  611. begin
  612. list.concat(taicpu.op_const_const_const(A_CROR,testbit+1,testbit+1,testbit+2));
  613. f2.flag:=F_GT;
  614. end;
  615. F_FB:
  616. f2.flag:=F_LT;
  617. F_FBE:
  618. begin
  619. list.concat(taicpu.op_const_const_const(A_CROR,testbit,testbit,testbit+2));
  620. f2.flag:=F_LT;
  621. end;
  622. else
  623. ;
  624. end;
  625. c := flags_to_cond(f2);
  626. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  627. end;
  628. procedure tcgppcgen.a_jmp_cond(list : TAsmList;cond : TOpCmp; l: tasmlabel);
  629. begin
  630. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  631. end;
  632. procedure tcgppcgen.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  633. crval: longint; l: tasmlabel);
  634. var
  635. p: taicpu;
  636. begin
  637. p := taicpu.op_sym(op,l);
  638. if op <> A_B then
  639. create_cond_norm(c,crval,p.condition);
  640. p.is_jmp := true;
  641. list.concat(p)
  642. end;
  643. function tcgppcgen.get_rtoc_offset: longint;
  644. begin
  645. case target_info.abi of
  646. abi_powerpc_aix:
  647. result:=LA_RTOC_AIX;
  648. {$ifdef powerpc64}
  649. { no TOC on Linux/ppc32 }
  650. abi_powerpc_elfv1:
  651. result:=LA_RTOC_SYSV;
  652. {$endif}
  653. abi_powerpc_elfv2:
  654. result:=LA_RTOC_ELFV2;
  655. else
  656. internalerror(2015021001);
  657. end;
  658. end;
  659. function tcgppcgen.load_got_symbol(list: TAsmList; const symbol : string; const flags: tindsymflags) : tregister;
  660. var
  661. l: tasmsymbol;
  662. ref: treference;
  663. begin
  664. if target_info.system=system_powerpc64_linux then
  665. begin
  666. l:=current_asmdata.getasmsymbol(symbol);
  667. reference_reset_symbol(ref,l,0,sizeof(pint),[]);
  668. ref.base:=NR_RTOC;
  669. ref.refaddr:=addr_pic;
  670. end
  671. else if target_info.system in systems_aix then
  672. get_aix_toc_sym(list,symbol,flags,ref,false)
  673. else
  674. internalerror(2007102010);
  675. result := getaddressregister(list);
  676. {$ifdef cpu64bitaddr}
  677. list.concat(taicpu.op_reg_ref(A_LD, result, ref));
  678. {$else cpu64bitaddr}
  679. list.concat(taicpu.op_reg_ref(A_LWZ, result, ref));
  680. {$endif cpu64bitaddr}
  681. end;
  682. procedure tcgppcgen.get_aix_toc_sym(list: TAsmList; const symname: string; const flags: tindsymflags; out ref: treference; force_direct_toc: boolean);
  683. const
  684. { The TOC on AIX is limited to 32KB worth of entries on AIX. If you need
  685. more entries, you have to add a level of indirection. In some cases,
  686. it's not possible to do this (e.g. assembler code). So by default, we
  687. use direct TOC entries until we're 500 from the maximum, and then start
  688. using indirect TOC entries. }
  689. AutoDirectTOCLimit = (high(smallint) div sizeof(pint)) - 500;
  690. var
  691. tmpref: treference;
  692. nlsymname: string;
  693. newsymname: ansistring;
  694. sym: TAsmSymbol;
  695. tocsym: TTOCAsmSymbol;
  696. tocnr,
  697. entrynr: longint;
  698. tmpreg: tregister;
  699. begin
  700. { all global symbol accesses always must be done via the TOC }
  701. nlsymname:='LC..'+symname;
  702. reference_reset_symbol(ref,current_asmdata.getasmsymbol(nlsymname),0,sizeof(pint),[]);
  703. if (assigned(ref.symbol) and
  704. not(ref.symbol is TTOCAsmSymbol)) or
  705. (not(ts_small_toc in current_settings.targetswitches) and
  706. (TPPCAsmData(current_asmdata).DirectTOCEntries<AutoDirectTOCLimit)) or
  707. force_direct_toc then
  708. begin
  709. ref.refaddr:=addr_pic_no_got;
  710. ref.base:=NR_RTOC;
  711. if not assigned(ref.symbol) then
  712. begin
  713. TPPCAsmData(current_asmdata).DirectTOCEntries:=TPPCAsmData(current_asmdata).DirectTOCEntries+1;
  714. new_section(current_asmdata.AsmLists[al_picdata],sec_toc,'',sizeof(pint));
  715. ref.symbol:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA,voidpointertype);
  716. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(ref.symbol,0));
  717. { do not assign the result of these statements to ref.symbol: the
  718. access must be done via the LC..symname symbol; these are just
  719. to define the symbol that's being accessed as either weak or
  720. not }
  721. if not(is_weak in flags) then
  722. current_asmdata.RefAsmSymbol(symname,AT_DATA)
  723. else if is_data in flags then
  724. current_asmdata.WeakRefAsmSymbol(symname,AT_DATA)
  725. else
  726. current_asmdata.WeakRefAsmSymbol('.'+symname,AT_DATA);
  727. newsymname:=ApplyAsmSymbolRestrictions(symname);
  728. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_toc_entry,newsymname+'[TC],'+newsymname));
  729. end;
  730. end
  731. else
  732. begin
  733. if not assigned(ref.symbol) then
  734. begin
  735. TPPCAsmData(current_asmdata).GetNextSmallTocEntry(tocnr,entrynr);
  736. { new TOC entry? }
  737. if entrynr=0 then
  738. begin
  739. { create new toc entry that contains the address of the next
  740. table of addresses }
  741. get_aix_toc_sym(list,'tocsubtable'+tostr(tocnr),[is_data],tmpref,true);
  742. sym:=tmpref.symbol;
  743. { base address for this batch of toc table entries that we'll
  744. put in a data block instead }
  745. new_section(current_asmdata.AsmLists[al_indirectpicdata],sec_rodata,'',sizeof(pint));
  746. sym:=current_asmdata.DefineAsmSymbol('tocsubtable'+tostr(tocnr),AB_LOCAL,AT_DATA,voidpointertype);
  747. current_asmdata.asmlists[al_indirectpicdata].concat(tai_symbol.create(sym,0));
  748. end;
  749. { add the reference to the actual symbol inside the tocsubtable }
  750. if not(is_weak in flags) then
  751. current_asmdata.RefAsmSymbol(symname,AT_DATA)
  752. else if is_data in flags then
  753. current_asmdata.WeakRefAsmSymbol(symname,AT_DATA)
  754. else
  755. current_asmdata.WeakRefAsmSymbol('.'+symname,AT_DATA);
  756. tocsym:=TTOCAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TTOCAsmSymbol,nlsymname,AB_LOCAL,AT_DATA,voidpointertype));
  757. ref.symbol:=tocsym;
  758. tocsym.ftocsecnr:=tocnr;
  759. current_asmdata.asmlists[al_indirectpicdata].concat(tai_symbol.create(tocsym,0));
  760. newsymname:=ApplyAsmSymbolRestrictions(symname);
  761. sym:=current_asmdata.RefAsmSymbol(newsymname,AT_DATA);
  762. current_asmdata.asmlists[al_indirectpicdata].concat(tai_const.Create_sym(sym));
  763. end;
  764. { first load the address of the table from the TOC }
  765. get_aix_toc_sym(list,'tocsubtable'+tostr(TTOCAsmSymbol(ref.symbol).ftocsecnr),[is_data],tmpref,true);
  766. tmpreg:=getaddressregister(list);
  767. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,tmpreg);
  768. { and now set up the address of the entry, relative to the start of
  769. the table }
  770. ref.base:=tmpreg;
  771. ref.refaddr:=addr_pic;
  772. ref.relsymbol:=current_asmdata.GetAsmSymbol('tocsubtable'+tostr(TTOCAsmSymbol(ref.symbol).ftocsecnr));
  773. end;
  774. end;
  775. procedure tcgppcgen.g_load_check_simple(list: TAsmList; const ref: treference; size: aint);
  776. var
  777. reg: tregister;
  778. lab: tasmlabel;
  779. begin
  780. if not(cs_check_low_addr_load in current_settings.localswitches) then
  781. exit;
  782. { this is mainly for AIX, which does not trap loads from address 0. A
  783. global symbol (if not weak) will always map to a proper address, and
  784. the same goes for stack addresses -> skip }
  785. if assigned(ref.symbol) and
  786. (ref.symbol.bind<>AB_WEAK_EXTERNAL) then
  787. exit;
  788. if (ref.base=NR_STACK_POINTER_REG) or
  789. (ref.index=NR_STACK_POINTER_REG) or
  790. (assigned(current_procinfo) and
  791. ((ref.base=current_procinfo.framepointer) or
  792. (ref.index=current_procinfo.framepointer))) then
  793. exit;
  794. if assigned(ref.symbol) or
  795. (ref.offset<>0) or
  796. ((ref.base<>NR_NO) and (ref.index<>NR_NO)) then
  797. begin
  798. { can't allocate register, also used in wrappers and the like }
  799. reg:=NR_R0;
  800. a_reg_alloc(list,reg);
  801. a_loadaddr_ref_reg(list,ref,reg);
  802. end
  803. else if ref.base<>NR_NO then
  804. reg:=ref.base
  805. else
  806. reg:=ref.index;
  807. current_asmdata.getjumplabel(lab);
  808. if reg=NR_R0 then
  809. a_reg_dealloc(list,reg);
  810. a_cmp_const_reg_label(list,OS_ADDR,OC_A,size-1,reg,lab);
  811. a_call_name(list,'FPC_INVALIDPOINTER',false);
  812. a_label(list,lab);
  813. end;
  814. procedure tcgppcgen.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  815. var
  816. testbit: byte;
  817. bitvalue: boolean;
  818. hreg: tregister;
  819. needsecondreg: boolean;
  820. begin
  821. hreg:=NR_NO;
  822. needsecondreg:=false;
  823. { get the bit to extract from the conditional register + its requested value (0 or 1) }
  824. testbit := ((f.cr - RS_CR0) * 4);
  825. case f.flag of
  826. F_EQ, F_NE:
  827. begin
  828. inc(testbit, 2);
  829. bitvalue := f.flag = F_EQ;
  830. end;
  831. F_LT, F_GE, F_FB:
  832. begin
  833. bitvalue := f.flag in [F_LT,F_FB];
  834. end;
  835. F_GT, F_LE, F_FA:
  836. begin
  837. inc(testbit);
  838. bitvalue := f.flag in [F_GT,F_FA];
  839. end;
  840. F_FAE:
  841. begin
  842. inc(testbit);
  843. bitvalue:=true;
  844. needsecondreg:=true;
  845. end;
  846. F_FBE:
  847. begin
  848. bitvalue:=true;
  849. needsecondreg:=true;
  850. end;
  851. else
  852. internalerror(200112261);
  853. end;
  854. { load the conditional register in the destination reg }
  855. list.concat(taicpu.op_reg(A_MFCR, reg));
  856. { we will move the bit that has to be tested to bit 0 by rotating left }
  857. testbit := (testbit + 1) and 31;
  858. { for floating-point >= and <=, extract equality bit first }
  859. if needsecondreg then
  860. begin
  861. hreg:=getintregister(list,OS_INT);
  862. list.concat(taicpu.op_reg_reg_const_const_const(
  863. A_RLWINM,hreg,reg,(((f.cr-RS_CR0)*4)+3) and 31,31,31));
  864. end;
  865. { extract bit }
  866. list.concat(taicpu.op_reg_reg_const_const_const(
  867. A_RLWINM,reg,reg,testbit,31,31));
  868. if needsecondreg then
  869. list.concat(taicpu.op_reg_reg_reg(A_OR,reg,hreg,reg))
  870. { if we need the inverse, xor with 1 }
  871. else if not bitvalue then
  872. list.concat(taicpu.op_reg_reg_const(A_XORI, reg, reg, 1));
  873. end;
  874. function tcgppcgen.fixref(list: TAsmList; var ref: treference): boolean;
  875. var
  876. tmpreg: tregister;
  877. begin
  878. result := false;
  879. { Avoid recursion. }
  880. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  881. exit;
  882. {$IFDEF EXTDEBUG}
  883. list.concat(tai_comment.create(strpnew('fixref0 ' + ref2string(ref))));
  884. {$ENDIF EXTDEBUG}
  885. if (target_info.system in [system_powerpc_darwin,system_powerpc64_darwin]) and
  886. assigned(ref.symbol) and
  887. not assigned(ref.relsymbol) and
  888. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) or
  889. (cs_create_pic in current_settings.moduleswitches))then
  890. begin
  891. if (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) or
  892. ((target_info.system=system_powerpc64_darwin) and
  893. (ref.symbol.bind=AB_GLOBAL)) then
  894. begin
  895. tmpreg := g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  896. ref.symbol:=nil;
  897. end
  898. else
  899. begin
  900. include(current_procinfo.flags,pi_needs_got);
  901. tmpreg := getaddressregister(list);
  902. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,tmpreg);
  903. if assigned(ref.relsymbol) then
  904. internalerror(2007093501);
  905. ref.relsymbol := current_procinfo.CurrGOTLabel;
  906. end;
  907. if (ref.base = NR_NO) then
  908. ref.base := tmpreg
  909. else if (ref.index = NR_NO) then
  910. ref.index := tmpreg
  911. else
  912. begin
  913. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  914. ref.base := tmpreg;
  915. end;
  916. end;
  917. { if we have to create PIC, add the symbol to the TOC/GOT }
  918. if (((target_info.system = system_powerpc64_linux) and
  919. (cs_create_pic in current_settings.moduleswitches)) or
  920. (target_info.system in systems_aix)) and
  921. (assigned(ref.symbol) and
  922. not assigned(ref.relsymbol)) then
  923. begin
  924. include(current_procinfo.flags,pi_needs_got);
  925. tmpreg := load_got_symbol(list, ref.symbol.name, asmsym2indsymflags(ref.symbol));
  926. if (ref.base = NR_NO) then
  927. ref.base := tmpreg
  928. else if (ref.index = NR_NO) then
  929. ref.index := tmpreg
  930. else begin
  931. a_op_reg_reg_reg(list, OP_ADD, OS_ADDR, ref.base, tmpreg, tmpreg);
  932. ref.base := tmpreg;
  933. end;
  934. ref.symbol := nil;
  935. {$IFDEF EXTDEBUG}
  936. list.concat(tai_comment.create(strpnew('fixref-pic ' + ref2string(ref))));
  937. {$ENDIF EXTDEBUG}
  938. end;
  939. if (ref.base = NR_NO) then
  940. begin
  941. ref.base := ref.index;
  942. ref.index := NR_NO;
  943. end;
  944. if (ref.base <> NR_NO) then
  945. begin
  946. if (ref.index <> NR_NO) and
  947. ((ref.offset <> 0) or assigned(ref.symbol)) then
  948. begin
  949. result := true;
  950. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  951. list.concat(taicpu.op_reg_reg_reg(
  952. A_ADD,tmpreg,ref.base,ref.index));
  953. ref.index := NR_NO;
  954. ref.base := tmpreg;
  955. end
  956. end;
  957. if (ref.index <> NR_NO) and
  958. (assigned(ref.symbol) or
  959. (ref.offset <> 0)) then
  960. internalerror(200208102);
  961. {$IFDEF EXTDEBUG}
  962. list.concat(tai_comment.create(strpnew('fixref1 ' + ref2string(ref))));
  963. {$ENDIF EXTDEBUG}
  964. end;
  965. procedure tcgppcgen.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  966. ref: treference);
  967. var
  968. tmpreg: tregister;
  969. {$ifdef cpu64bitaddr}
  970. tmpreg2: tregister;
  971. {$endif cpu64bitaddr}
  972. tmpref: treference;
  973. largeOffset: Boolean;
  974. begin
  975. tmpreg := NR_NO;
  976. largeOffset:= hasLargeOffset(ref);
  977. if target_info.system in ([system_powerpc_macosclassic]+systems_aix) then
  978. begin
  979. if assigned(ref.symbol) and
  980. (ref.refaddr<>addr_pic_no_got) then
  981. begin {Load symbol's value}
  982. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  983. reference_reset(tmpref,sizeof(pint),[]);
  984. tmpref.symbol := ref.symbol;
  985. tmpref.base := NR_RTOC;
  986. tmpref.refaddr := addr_pic_no_got;
  987. if macos_direct_globals then
  988. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  989. else
  990. {$ifdef cpu64bitaddr}
  991. list.concat(taicpu.op_reg_ref(A_LD,tmpreg,tmpref));
  992. {$else cpu64bitaddr}
  993. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  994. {$endif cpu64bitaddr}
  995. end;
  996. if largeOffset then
  997. begin {Add hi part of offset}
  998. reference_reset(tmpref,ref.alignment,[]);
  999. {$ifdef cpu64bitaddr}
  1000. if (ref.offset < low(longint)) or
  1001. (ref.offset > high(longint)) then
  1002. begin
  1003. { load upper 32 bits of the offset, adjusted for adding
  1004. the lower 32 bits later }
  1005. tmpreg2:=getintregister(list,OS_ADDR);
  1006. a_load_const_reg(list,OS_ADDR,(ref.offset and $ffffffff00000000) + ord(longint(ref.offset)<0),tmpreg2);
  1007. if tmpreg=NR_NO then
  1008. tmpreg:=tmpreg2
  1009. else
  1010. a_op_reg_reg(list,OP_ADD,OS_ADDR,tmpreg2,tmpreg);
  1011. ref.offset:=longint(ref.offset);
  1012. end;
  1013. {$endif cpu64bitaddr}
  1014. {Compensate when lo part is negative}
  1015. tmpref.offset := Smallint(ref.offset >> 16) + ord(Smallint(ref.offset) < 0);
  1016. if (tmpreg <> NR_NO) then
  1017. list.concat(taicpu.op_reg_reg_const(A_ADDIS,tmpreg, tmpreg,tmpref.offset))
  1018. else
  1019. begin
  1020. tmpreg := getintregister(list,OS_ADDR);
  1021. list.concat(taicpu.op_reg_const(A_LIS,tmpreg,tmpref.offset));
  1022. end;
  1023. end;
  1024. if (tmpreg <> NR_NO) then
  1025. begin
  1026. {Add content of base register}
  1027. if ref.base <> NR_NO then
  1028. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1029. ref.base,tmpreg));
  1030. {Make ref ready to be used by op}
  1031. ref.symbol:= nil;
  1032. ref.base:= tmpreg;
  1033. if largeOffset then
  1034. ref.offset := Smallint(ref.offset);
  1035. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1036. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1037. end
  1038. else
  1039. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1040. end
  1041. else {if target_info.system <> system_powerpc_macosclassic}
  1042. begin
  1043. if assigned(ref.symbol) or
  1044. largeOffset then
  1045. begin
  1046. // TODO: offsets > 32 bit
  1047. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1048. reference_reset(tmpref,ref.alignment,[]);
  1049. tmpref.symbol := ref.symbol;
  1050. tmpref.relsymbol := ref.relsymbol;
  1051. tmpref.offset := ref.offset;
  1052. tmpref.refaddr := addr_higha;
  1053. if ref.base <> NR_NO then
  1054. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1055. ref.base,tmpref))
  1056. else
  1057. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1058. ref.base := tmpreg;
  1059. ref.refaddr := addr_low;
  1060. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1061. end
  1062. else
  1063. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1064. end;
  1065. end;
  1066. { TPPCAsmData }
  1067. procedure TPPCAsmData.GetNextSmallTocEntry(out tocnr, entrynr: longint);
  1068. begin
  1069. if fcurrenttocentries>(high(word) div sizeof(pint)) then
  1070. begin
  1071. fcurrenttocentries:=0;
  1072. inc(ftocsections);
  1073. end;
  1074. tocnr:=ftocsections;
  1075. entrynr:=fcurrenttocentries;
  1076. inc(fcurrenttocentries);
  1077. end;
  1078. begin
  1079. casmdata:=TPPCAsmData;
  1080. end.