cgppc.pas 47 KB

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