2
0

cgppc.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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,symtable,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_M256', 'OS_MS8', 'OS_MS16', 'OS_MS32',
  128. 'OS_MS64', 'OS_MS128', 'OS_MS256');
  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. pd : tprocdef;
  567. begin
  568. if (target_info.system in [system_powerpc_darwin]) then
  569. begin
  570. pd:=search_system_proc('mcount');
  571. paraloc1.init;
  572. paramanager.getintparaloc(pd,1,paraloc1);
  573. a_load_reg_cgpara(list,OS_ADDR,NR_R0,paraloc1);
  574. paramanager.freecgpara(list,paraloc1);
  575. paraloc1.done;
  576. allocallcpuregisters(list);
  577. a_call_name(list,'mcount',false);
  578. deallocallcpuregisters(list);
  579. a_reg_dealloc(list,NR_R0);
  580. end;
  581. end;
  582. procedure tcgppcgen.a_jmp_cond(list : TAsmList;cond : TOpCmp; l: tasmlabel);
  583. begin
  584. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  585. end;
  586. procedure tcgppcgen.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  587. crval: longint; l: tasmlabel);
  588. var
  589. p: taicpu;
  590. begin
  591. p := taicpu.op_sym(op,l);
  592. if op <> A_B then
  593. create_cond_norm(c,crval,p.condition);
  594. p.is_jmp := true;
  595. list.concat(p)
  596. end;
  597. procedure tcgppcgen.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  598. procedure loadvmttor11;
  599. var
  600. href : treference;
  601. begin
  602. reference_reset_base(href,NR_R3,0,sizeof(pint));
  603. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  604. end;
  605. procedure op_onr11methodaddr;
  606. var
  607. href : treference;
  608. begin
  609. if (procdef.extnumber=$ffff) then
  610. Internalerror(200006139);
  611. { call/jmp vmtoffs(%eax) ; method offs }
  612. reference_reset_base(href,NR_R11,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  613. if hasLargeOffset(href) then
  614. begin
  615. {$ifdef cpu64}
  616. if (longint(href.offset) <> href.offset) then
  617. { add support for offsets > 32 bit }
  618. internalerror(200510201);
  619. {$endif cpu64}
  620. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  621. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  622. href.offset := smallint(href.offset and $ffff);
  623. end;
  624. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  625. if (target_info.system in ([system_powerpc64_linux]+systems_aix)) then
  626. begin
  627. reference_reset_base(href, NR_R11, 0, sizeof(pint));
  628. a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R11);
  629. end;
  630. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  631. list.concat(taicpu.op_none(A_BCTR));
  632. if (target_info.system in ([system_powerpc64_linux]+systems_aix)) then
  633. list.concat(taicpu.op_none(A_NOP));
  634. end;
  635. var
  636. make_global : boolean;
  637. begin
  638. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  639. Internalerror(200006137);
  640. if not assigned(procdef.struct) or
  641. (procdef.procoptions*[po_classmethod, po_staticmethod,
  642. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  643. Internalerror(200006138);
  644. if procdef.owner.symtabletype<>ObjectSymtable then
  645. Internalerror(200109191);
  646. make_global:=false;
  647. if (not current_module.is_unit) or
  648. create_smartlink or
  649. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  650. make_global:=true;
  651. if make_global then
  652. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  653. else
  654. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  655. { set param1 interface to self }
  656. g_adjust_self_value(list,procdef,ioffset);
  657. { case 4 }
  658. if (po_virtualmethod in procdef.procoptions) and
  659. not is_objectpascal_helper(procdef.struct) then
  660. begin
  661. loadvmttor11;
  662. op_onr11methodaddr;
  663. end
  664. { case 0 }
  665. else
  666. case target_info.system of
  667. system_powerpc_darwin,
  668. system_powerpc64_darwin:
  669. list.concat(taicpu.op_sym(A_B,get_darwin_call_stub(procdef.mangledname,false)));
  670. system_powerpc64_linux,
  671. system_powerpc_aix,
  672. system_powerpc64_aix:
  673. {$note ts:todo add GOT change?? - think not needed :) }
  674. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol('.' + procdef.mangledname)));
  675. else
  676. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)))
  677. end;
  678. List.concat(Tai_symbol_end.Createname(labelname));
  679. end;
  680. function tcgppcgen.load_got_symbol(list: TAsmList; const symbol : string; const flags: tindsymflags) : tregister;
  681. var
  682. l: tasmsymbol;
  683. ref: treference;
  684. begin
  685. if target_info.system=system_powerpc64_linux then
  686. begin
  687. l:=current_asmdata.getasmsymbol(symbol);
  688. reference_reset_symbol(ref,l,0,sizeof(pint));
  689. ref.base:=NR_RTOC;
  690. ref.refaddr:=addr_pic;
  691. end
  692. else if target_info.system in systems_aix then
  693. get_aix_toc_sym(list,symbol,flags,ref,false)
  694. else
  695. internalerror(2007102010);
  696. result := getaddressregister(list);
  697. {$ifdef cpu64bitaddr}
  698. list.concat(taicpu.op_reg_ref(A_LD, result, ref));
  699. {$else cpu64bitaddr}
  700. list.concat(taicpu.op_reg_ref(A_LWZ, result, ref));
  701. {$endif cpu64bitaddr}
  702. end;
  703. procedure tcgppcgen.get_aix_toc_sym(list: TAsmList; const symname: string; const flags: tindsymflags; out ref: treference; force_direct_toc: boolean);
  704. const
  705. { The TOC on AIX is limited to 32KB worth of entries on AIX. If you need
  706. more entries, you have to add a level of indirection. In some cases,
  707. it's not possible to do this (e.g. assembler code). So by default, we
  708. use direct TOC entries until we're 500 from the maximum, and then start
  709. using indirect TOC entries. }
  710. AutoDirectTOCLimit = (high(smallint) div sizeof(pint)) - 500;
  711. var
  712. tmpref: treference;
  713. { can have more than 16384 (32 bit) or 8192 (64 bit) toc entries and, as
  714. as consequence, toc subsections -> 5 extra characters for the number}
  715. tocsecname: string[length('tocsubtable')+5];
  716. nlsymname: string;
  717. newsymname: ansistring;
  718. sym: TAsmSymbol;
  719. tocsym: TTOCAsmSymbol;
  720. tocnr,
  721. entrynr: longint;
  722. tmpreg: tregister;
  723. begin
  724. { all global symbol accesses always must be done via the TOC }
  725. nlsymname:='LC..'+symname;
  726. reference_reset_symbol(ref,current_asmdata.getasmsymbol(nlsymname),0,sizeof(pint));
  727. if (assigned(ref.symbol) and
  728. not(ref.symbol is TTOCAsmSymbol)) or
  729. (not(ts_small_toc in current_settings.targetswitches) and
  730. (TPPCAsmData(current_asmdata).DirectTOCEntries<AutoDirectTOCLimit)) or
  731. force_direct_toc then
  732. begin
  733. ref.refaddr:=addr_pic_no_got;
  734. ref.base:=NR_RTOC;
  735. if not assigned(ref.symbol) then
  736. begin
  737. TPPCAsmData(current_asmdata).DirectTOCEntries:=TPPCAsmData(current_asmdata).DirectTOCEntries+1;
  738. new_section(current_asmdata.AsmLists[al_picdata],sec_toc,'',sizeof(pint));
  739. ref.symbol:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  740. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(ref.symbol,0));
  741. { do not assign the result of these statements to ref.symbol: the
  742. access must be done via the LC..symname symbol; these are just
  743. to define the symbol that's being accessed as either weak or
  744. not }
  745. if not(is_weak in flags) then
  746. current_asmdata.RefAsmSymbol(symname)
  747. else if is_data in flags then
  748. current_asmdata.WeakRefAsmSymbol(symname)
  749. else
  750. current_asmdata.WeakRefAsmSymbol('.'+symname);
  751. newsymname:=ReplaceForbiddenAsmSymbolChars(symname);
  752. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_toc_entry,newsymname+'[TC],'+newsymname));
  753. end;
  754. end
  755. else
  756. begin
  757. if not assigned(ref.symbol) then
  758. begin
  759. TPPCAsmData(current_asmdata).GetNextSmallTocEntry(tocnr,entrynr);
  760. { new TOC entry? }
  761. if entrynr=0 then
  762. begin
  763. { create new toc entry that contains the address of the next
  764. table of addresses }
  765. get_aix_toc_sym(list,'tocsubtable'+tostr(tocnr),[is_data],tmpref,true);
  766. sym:=tmpref.symbol;
  767. { base address for this batch of toc table entries that we'll
  768. put in a data block instead }
  769. new_section(current_asmdata.AsmLists[al_indirectpicdata],sec_rodata,'',sizeof(pint));
  770. sym:=current_asmdata.DefineAsmSymbol('tocsubtable'+tostr(tocnr),AB_LOCAL,AT_DATA);
  771. current_asmdata.asmlists[al_indirectpicdata].concat(tai_symbol.create(sym,0));
  772. end;
  773. { add the reference to the actual symbol inside the tocsubtable }
  774. if not(is_weak in flags) then
  775. current_asmdata.RefAsmSymbol(symname)
  776. else if is_data in flags then
  777. current_asmdata.WeakRefAsmSymbol(symname)
  778. else
  779. current_asmdata.WeakRefAsmSymbol('.'+symname);
  780. tocsym:=TTOCAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TTOCAsmSymbol,nlsymname,AB_LOCAL,AT_DATA));
  781. ref.symbol:=tocsym;
  782. tocsym.ftocsecnr:=tocnr;
  783. current_asmdata.asmlists[al_indirectpicdata].concat(tai_symbol.create(tocsym,0));
  784. newsymname:=ReplaceForbiddenAsmSymbolChars(symname);
  785. sym:=current_asmdata.RefAsmSymbol(newsymname);
  786. current_asmdata.asmlists[al_indirectpicdata].concat(tai_const.Create_sym(sym));
  787. end;
  788. { first load the address of the table from the TOC }
  789. get_aix_toc_sym(list,'tocsubtable'+tostr(TTOCAsmSymbol(ref.symbol).ftocsecnr),[is_data],tmpref,true);
  790. tmpreg:=getaddressregister(list);
  791. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,tmpreg);
  792. { and now set up the address of the entry, relative to the start of
  793. the table }
  794. ref.base:=tmpreg;
  795. ref.refaddr:=addr_pic;
  796. ref.relsymbol:=current_asmdata.GetAsmSymbol('tocsubtable'+tostr(TTOCAsmSymbol(ref.symbol).ftocsecnr));
  797. end;
  798. end;
  799. procedure tcgppcgen.g_load_check_simple(list: TAsmList; const ref: treference; size: aint);
  800. var
  801. reg: tregister;
  802. lab: tasmlabel;
  803. begin
  804. if not(cs_check_low_addr_load in current_settings.localswitches) then
  805. exit;
  806. { this is mainly for AIX, which does not trap loads from address 0. A
  807. global symbol (if not weak) will always map to a proper address, and
  808. the same goes for stack addresses -> skip }
  809. if assigned(ref.symbol) and
  810. (ref.symbol.bind<>AB_WEAK_EXTERNAL) then
  811. exit;
  812. if (ref.base=NR_STACK_POINTER_REG) or
  813. (ref.index=NR_STACK_POINTER_REG) or
  814. (assigned(current_procinfo) and
  815. ((ref.base=current_procinfo.framepointer) or
  816. (ref.index=current_procinfo.framepointer))) then
  817. exit;
  818. if assigned(ref.symbol) or
  819. (ref.offset<>0) or
  820. ((ref.base<>NR_NO) and (ref.index<>NR_NO)) then
  821. begin
  822. { can't allocate register, also used in wrappers and the like }
  823. reg:=NR_R0;
  824. a_reg_alloc(list,reg);
  825. a_loadaddr_ref_reg(list,ref,reg);
  826. end
  827. else if ref.base<>NR_NO then
  828. reg:=ref.base
  829. else
  830. reg:=ref.index;
  831. current_asmdata.getjumplabel(lab);
  832. if reg=NR_R0 then
  833. a_reg_dealloc(list,reg);
  834. a_cmp_const_reg_label(list,OS_ADDR,OC_A,size-1,reg,lab);
  835. a_call_name(list,'FPC_INVALIDPOINTER',false);
  836. a_label(list,lab);
  837. end;
  838. procedure tcgppcgen.g_external_wrapper(list: TAsmList; pd: TProcDef; const externalname: string);
  839. var
  840. href : treference;
  841. begin
  842. if not(target_info.system in ([system_powerpc64_linux]+systems_aix)) then begin
  843. inherited;
  844. exit;
  845. end;
  846. { for ppc64/linux and aix emit correct code which sets up a stack frame
  847. and then calls the external method normally to ensure that the GOT/TOC
  848. will be loaded correctly if required.
  849. The resulting code sequence looks as follows:
  850. mflr r0
  851. stw/d r0, 16(r1)
  852. stw/du r1, -112(r1)
  853. bl <external_method>
  854. nop
  855. addi r1, r1, 112
  856. lwz/d r0, 16(r1)
  857. mtlr r0
  858. blr
  859. }
  860. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  861. if target_info.abi=abi_powerpc_sysv then
  862. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, 8)
  863. else
  864. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_AIX, 8);
  865. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_R0,href);
  866. reference_reset_base(href, NR_STACK_POINTER_REG, -MINIMUM_STACKFRAME_SIZE, 8);
  867. list.concat(taicpu.op_reg_ref({$ifdef cpu64bitaddr}A_STDU{$else}A_STWU{$endif}, NR_STACK_POINTER_REG, href));
  868. a_call_name(list,externalname,false);
  869. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, MINIMUM_STACKFRAME_SIZE));
  870. if target_info.abi=abi_powerpc_sysv then
  871. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, 8)
  872. else
  873. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_AIX, 8);
  874. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  875. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  876. list.concat(taicpu.op_none(A_BLR));
  877. end;
  878. function tcgppcgen.fixref(list: TAsmList; var ref: treference): boolean;
  879. var
  880. tmpreg: tregister;
  881. begin
  882. result := false;
  883. { Avoid recursion. }
  884. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  885. exit;
  886. {$IFDEF EXTDEBUG}
  887. list.concat(tai_comment.create(strpnew('fixref0 ' + ref2string(ref))));
  888. {$ENDIF EXTDEBUG}
  889. if (target_info.system in [system_powerpc_darwin,system_powerpc64_darwin]) and
  890. assigned(ref.symbol) and
  891. not assigned(ref.relsymbol) and
  892. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) or
  893. (cs_create_pic in current_settings.moduleswitches))then
  894. begin
  895. if (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) or
  896. ((target_info.system=system_powerpc64_darwin) and
  897. (ref.symbol.bind=AB_GLOBAL)) then
  898. begin
  899. tmpreg := g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  900. ref.symbol:=nil;
  901. end
  902. else
  903. begin
  904. include(current_procinfo.flags,pi_needs_got);
  905. tmpreg := getaddressregister(list);
  906. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,tmpreg);
  907. if assigned(ref.relsymbol) then
  908. internalerror(2007093501);
  909. ref.relsymbol := current_procinfo.CurrGOTLabel;
  910. end;
  911. if (ref.base = NR_NO) then
  912. ref.base := tmpreg
  913. else if (ref.index = NR_NO) then
  914. ref.index := tmpreg
  915. else
  916. begin
  917. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  918. ref.base := tmpreg;
  919. end;
  920. end;
  921. { if we have to create PIC, add the symbol to the TOC/GOT }
  922. if (((target_info.system = system_powerpc64_linux) and
  923. (cs_create_pic in current_settings.moduleswitches)) or
  924. (target_info.system in systems_aix)) and
  925. (assigned(ref.symbol) and
  926. not assigned(ref.relsymbol)) then
  927. begin
  928. tmpreg := load_got_symbol(list, ref.symbol.name, asmsym2indsymflags(ref.symbol));
  929. if (ref.base = NR_NO) then
  930. ref.base := tmpreg
  931. else if (ref.index = NR_NO) then
  932. ref.index := tmpreg
  933. else begin
  934. a_op_reg_reg_reg(list, OP_ADD, OS_ADDR, ref.base, tmpreg, tmpreg);
  935. ref.base := tmpreg;
  936. end;
  937. ref.symbol := nil;
  938. {$IFDEF EXTDEBUG}
  939. list.concat(tai_comment.create(strpnew('fixref-pic ' + ref2string(ref))));
  940. {$ENDIF EXTDEBUG}
  941. end;
  942. if (ref.base = NR_NO) then
  943. begin
  944. ref.base := ref.index;
  945. ref.index := NR_NO;
  946. end;
  947. if (ref.base <> NR_NO) then
  948. begin
  949. if (ref.index <> NR_NO) and
  950. ((ref.offset <> 0) or assigned(ref.symbol)) then
  951. begin
  952. result := true;
  953. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  954. list.concat(taicpu.op_reg_reg_reg(
  955. A_ADD,tmpreg,ref.base,ref.index));
  956. ref.index := NR_NO;
  957. ref.base := tmpreg;
  958. end
  959. end;
  960. if (ref.index <> NR_NO) and
  961. (assigned(ref.symbol) or
  962. (ref.offset <> 0)) then
  963. internalerror(200208102);
  964. {$IFDEF EXTDEBUG}
  965. list.concat(tai_comment.create(strpnew('fixref1 ' + ref2string(ref))));
  966. {$ENDIF EXTDEBUG}
  967. end;
  968. procedure tcgppcgen.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  969. ref: treference);
  970. var
  971. tmpreg: tregister;
  972. {$ifdef cpu64bitaddr}
  973. tmpreg2: tregister;
  974. {$endif cpu64bitaddr}
  975. tmpref: treference;
  976. largeOffset: Boolean;
  977. begin
  978. tmpreg := NR_NO;
  979. largeOffset:= hasLargeOffset(ref);
  980. if target_info.system in ([system_powerpc_macos]+systems_aix) then
  981. begin
  982. if assigned(ref.symbol) and
  983. (ref.refaddr<>addr_pic_no_got) then
  984. begin {Load symbol's value}
  985. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  986. reference_reset(tmpref,sizeof(pint));
  987. tmpref.symbol := ref.symbol;
  988. tmpref.base := NR_RTOC;
  989. tmpref.refaddr := addr_pic_no_got;
  990. if macos_direct_globals then
  991. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  992. else
  993. {$ifdef cpu64bitaddr}
  994. list.concat(taicpu.op_reg_ref(A_LD,tmpreg,tmpref));
  995. {$else cpu64bitaddr}
  996. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  997. {$endif cpu64bitaddr}
  998. end;
  999. if largeOffset then
  1000. begin {Add hi part of offset}
  1001. reference_reset(tmpref,ref.alignment);
  1002. {$ifdef cpu64bitaddr}
  1003. if (ref.offset < low(longint)) or
  1004. (ref.offset > high(longint)) then
  1005. begin
  1006. { load upper 32 bits of the offset, adjusted for adding
  1007. the lower 32 bits later }
  1008. tmpreg2:=getintregister(list,OS_ADDR);
  1009. a_load_const_reg(list,OS_ADDR,(ref.offset and $ffffffff00000000) + ord(longint(ref.offset)<0),tmpreg2);
  1010. if tmpreg=NR_NO then
  1011. tmpreg:=tmpreg2
  1012. else
  1013. a_op_reg_reg(list,OP_ADD,OS_ADDR,tmpreg2,tmpreg);
  1014. ref.offset:=longint(ref.offset);
  1015. end;
  1016. {$endif cpu64bitaddr}
  1017. {Compensate when lo part is negative}
  1018. tmpref.offset := Smallint(ref.offset >> 16) + ord(Smallint(ref.offset) < 0);
  1019. if (tmpreg <> NR_NO) then
  1020. list.concat(taicpu.op_reg_reg_const(A_ADDIS,tmpreg, tmpreg,tmpref.offset))
  1021. else
  1022. begin
  1023. tmpreg := getintregister(list,OS_ADDR);
  1024. list.concat(taicpu.op_reg_const(A_LIS,tmpreg,tmpref.offset));
  1025. end;
  1026. end;
  1027. if (tmpreg <> NR_NO) then
  1028. begin
  1029. {Add content of base register}
  1030. if ref.base <> NR_NO then
  1031. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1032. ref.base,tmpreg));
  1033. {Make ref ready to be used by op}
  1034. ref.symbol:= nil;
  1035. ref.base:= tmpreg;
  1036. if largeOffset then
  1037. ref.offset := Smallint(ref.offset);
  1038. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1039. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1040. end
  1041. else
  1042. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1043. end
  1044. else {if target_info.system <> system_powerpc_macos}
  1045. begin
  1046. if assigned(ref.symbol) or
  1047. largeOffset then
  1048. begin
  1049. // TODO: offsets > 32 bit
  1050. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1051. reference_reset(tmpref,ref.alignment);
  1052. tmpref.symbol := ref.symbol;
  1053. tmpref.relsymbol := ref.relsymbol;
  1054. tmpref.offset := ref.offset;
  1055. tmpref.refaddr := addr_higha;
  1056. if ref.base <> NR_NO then
  1057. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1058. ref.base,tmpref))
  1059. else
  1060. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1061. ref.base := tmpreg;
  1062. ref.refaddr := addr_low;
  1063. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1064. end
  1065. else
  1066. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1067. end;
  1068. end;
  1069. { TPPCAsmData }
  1070. procedure TPPCAsmData.GetNextSmallTocEntry(out tocnr, entrynr: longint);
  1071. begin
  1072. if fcurrenttocentries>(high(word) div sizeof(pint)) then
  1073. begin
  1074. fcurrenttocentries:=0;
  1075. inc(ftocsections);
  1076. end;
  1077. tocnr:=ftocsections;
  1078. entrynr:=fcurrenttocentries;
  1079. inc(fcurrenttocentries);
  1080. end;
  1081. begin
  1082. casmdata:=TPPCAsmData;
  1083. end.