cgcpu.pas 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,symtype,symdef,
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcg)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : tcgpara);override;
  36. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  37. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : tcgpara);override;
  38. procedure a_call_name(list : TAsmList;const s : string);override;
  39. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  40. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  41. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  42. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  43. size: tcgsize; a: aint; src, dst: tregister); override;
  44. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  45. size: tcgsize; src1, src2, dst: tregister); override;
  46. { move instructions }
  47. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  48. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  49. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  50. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  51. { fpu move instructions }
  52. procedure a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister); override;
  53. procedure a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister); override;
  54. procedure a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference); override;
  55. { comparison operations }
  56. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  57. l : tasmlabel);override;
  58. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_name(list : TAsmList;const s : string); override;
  60. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  62. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  63. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  64. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  65. procedure g_save_standard_registers(list:TAsmList); override;
  66. procedure g_restore_standard_registers(list:TAsmList); override;
  67. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  68. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  69. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  70. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  71. { that's the case, we can use rlwinm to do an AND operation }
  72. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  73. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  74. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  75. private
  76. (* NOT IN USE: *)
  77. procedure g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  78. (* NOT IN USE: *)
  79. procedure g_return_from_proc_mac(list : TAsmList;parasize : aint);
  80. { clear out potential overflow bits from 8 or 16 bit operations }
  81. { the upper 24/16 bits of a register after an operation }
  82. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  83. { Make sure ref is a valid reference for the PowerPC and sets the }
  84. { base to the value of the index if (base = R_NO). }
  85. { Returns true if the reference contained a base, index and an }
  86. { offset or symbol, in which case the base will have been changed }
  87. { to a tempreg (which has to be freed by the caller) containing }
  88. { the sum of part of the original reference }
  89. function fixref(list: TAsmList; var ref: treference): boolean;
  90. { returns whether a reference can be used immediately in a powerpc }
  91. { instruction }
  92. function issimpleref(const ref: treference): boolean;
  93. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  94. procedure a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  95. ref: treference);
  96. { creates the correct branch instruction for a given combination }
  97. { of asmcondflags and destination addressing mode }
  98. procedure a_jmp(list: TAsmList; op: tasmop;
  99. c: tasmcondflag; crval: longint; l: tasmlabel);
  100. function save_regs(list : TAsmList):longint;
  101. procedure restore_regs(list : TAsmList);
  102. function get_darwin_call_stub(const s: string): tasmsymbol;
  103. end;
  104. tcg64fppc = class(tcg64f32)
  105. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  106. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  107. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  108. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  109. end;
  110. const
  111. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDI,A_ANDI_,A_DIVWU,
  112. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  113. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  114. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDIS,A_ANDIS_,
  115. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  116. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  117. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  118. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  119. implementation
  120. uses
  121. globals,verbose,systems,cutils,
  122. symconst,symsym,fmodule,
  123. rgobj,tgobj,cpupi,procinfo,paramgr;
  124. procedure tcgppc.init_register_allocators;
  125. begin
  126. inherited init_register_allocators;
  127. if target_info.system=system_powerpc_darwin then
  128. begin
  129. {
  130. if pi_needs_got in current_procinfo.flags then
  131. begin
  132. current_procinfo.got:=NR_R31;
  133. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  134. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  135. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  136. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  137. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  138. RS_R14,RS_R13],first_int_imreg,[]);
  139. end
  140. else}
  141. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  142. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  143. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  144. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  145. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  146. RS_R14,RS_R13],first_int_imreg,[]);
  147. end
  148. else
  149. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  150. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  151. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  152. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  153. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  154. RS_R14,RS_R13],first_int_imreg,[]);
  155. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  156. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  157. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  158. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  159. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  160. {$warning FIX ME}
  161. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  162. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  163. end;
  164. procedure tcgppc.done_register_allocators;
  165. begin
  166. rg[R_INTREGISTER].free;
  167. rg[R_FPUREGISTER].free;
  168. rg[R_MMREGISTER].free;
  169. inherited done_register_allocators;
  170. end;
  171. procedure tcgppc.a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : tcgpara);
  172. var
  173. ref: treference;
  174. begin
  175. paraloc.check_simple_location;
  176. case paraloc.location^.loc of
  177. LOC_REGISTER,LOC_CREGISTER:
  178. a_load_const_reg(list,size,a,paraloc.location^.register);
  179. LOC_REFERENCE:
  180. begin
  181. reference_reset(ref);
  182. ref.base:=paraloc.location^.reference.index;
  183. ref.offset:=paraloc.location^.reference.offset;
  184. a_load_const_ref(list,size,a,ref);
  185. end;
  186. else
  187. internalerror(2002081101);
  188. end;
  189. end;
  190. procedure tcgppc.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);
  191. var
  192. tmpref, ref: treference;
  193. location: pcgparalocation;
  194. sizeleft: aint;
  195. begin
  196. location := paraloc.location;
  197. tmpref := r;
  198. sizeleft := paraloc.intsize;
  199. while assigned(location) do
  200. begin
  201. case location^.loc of
  202. LOC_REGISTER,LOC_CREGISTER:
  203. begin
  204. {$ifndef cpu64bit}
  205. if (sizeleft <> 3) then
  206. begin
  207. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  208. end
  209. else
  210. begin
  211. a_load_ref_reg(list,OS_16,OS_16,tmpref,location^.register);
  212. a_reg_alloc(list,NR_R0);
  213. inc(tmpref.offset,2);
  214. a_load_ref_reg(list,OS_8,OS_8,tmpref,newreg(R_INTREGISTER,RS_R0,R_SUBNONE));
  215. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  216. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location^.register,newreg(R_INTREGISTER,RS_R0,R_SUBNONE),8,16,31-8));
  217. a_reg_dealloc(list,NR_R0);
  218. dec(tmpref.offset,2);
  219. end;
  220. {$else not cpu64bit}
  221. {$error add 64 bit support for non power of 2 loads in a_param_ref}
  222. {$endif not cpu64bit}
  223. end;
  224. LOC_REFERENCE:
  225. begin
  226. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  227. g_concatcopy(list,tmpref,ref,sizeleft);
  228. if assigned(location^.next) then
  229. internalerror(2005010710);
  230. end;
  231. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  232. case location^.size of
  233. OS_F32, OS_F64:
  234. a_loadfpu_ref_reg(list,location^.size,tmpref,location^.register);
  235. else
  236. internalerror(2002072801);
  237. end;
  238. LOC_VOID:
  239. begin
  240. // nothing to do
  241. end;
  242. else
  243. internalerror(2002081103);
  244. end;
  245. inc(tmpref.offset,tcgsize2size[location^.size]);
  246. dec(sizeleft,tcgsize2size[location^.size]);
  247. location := location^.next;
  248. end;
  249. end;
  250. procedure tcgppc.a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : tcgpara);
  251. var
  252. ref: treference;
  253. tmpreg: tregister;
  254. begin
  255. paraloc.check_simple_location;
  256. case paraloc.location^.loc of
  257. LOC_REGISTER,LOC_CREGISTER:
  258. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  259. LOC_REFERENCE:
  260. begin
  261. reference_reset(ref);
  262. ref.base := paraloc.location^.reference.index;
  263. ref.offset := paraloc.location^.reference.offset;
  264. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  265. a_loadaddr_ref_reg(list,r,tmpreg);
  266. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  267. end;
  268. else
  269. internalerror(2002080701);
  270. end;
  271. end;
  272. function tcgppc.get_darwin_call_stub(const s: string): tasmsymbol;
  273. var
  274. stubname: string;
  275. href: treference;
  276. l1: tasmsymbol;
  277. begin
  278. { function declared in the current unit? }
  279. { doesn't work correctly, because this will also return a hit if we }
  280. { previously took the address of an external procedure. It doesn't }
  281. { really matter, the linker will remove all unnecessary stubs. }
  282. { result := current_asmdata.getasmsymbol(s);
  283. if not(assigned(result)) then
  284. begin }
  285. stubname := 'L'+s+'$stub';
  286. result := current_asmdata.getasmsymbol(stubname);
  287. { end; }
  288. if assigned(result) then
  289. exit;
  290. if current_asmdata.asmlists[al_imports]=nil then
  291. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  292. current_asmdata.asmlists[al_imports].concat(Tai_section.create(sec_stub,'',0));
  293. current_asmdata.asmlists[al_imports].concat(Tai_align.Create(16));
  294. result := current_asmdata.RefAsmSymbol(stubname);
  295. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  296. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  297. l1 := current_asmdata.RefAsmSymbol('L'+s+'$lazy_ptr');
  298. reference_reset_symbol(href,l1,0);
  299. href.refaddr := addr_hi;
  300. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  301. href.refaddr := addr_lo;
  302. href.base := NR_R11;
  303. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  304. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MTCTR,NR_R12));
  305. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_BCTR));
  306. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_lazy_symbol_pointer,''));
  307. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  308. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  309. current_asmdata.asmlists[al_imports].concat(tai_const.createname(strpnew('dyld_stub_binding_helper'),0));
  310. end;
  311. { calling a procedure by name }
  312. procedure tcgppc.a_call_name(list : TAsmList;const s : string);
  313. begin
  314. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  315. if it is a cross-TOC call. If so, it also replaces the NOP
  316. with some restore code.}
  317. if (target_info.system <> system_powerpc_darwin) then
  318. begin
  319. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  320. if target_info.system=system_powerpc_macos then
  321. list.concat(taicpu.op_none(A_NOP));
  322. end
  323. else
  324. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  325. {
  326. the compiler does not properly set this flag anymore in pass 1, and
  327. for now we only need it after pass 2 (I hope) (JM)
  328. if not(pi_do_call in current_procinfo.flags) then
  329. internalerror(2003060703);
  330. }
  331. include(current_procinfo.flags,pi_do_call);
  332. end;
  333. { calling a procedure by address }
  334. procedure tcgppc.a_call_reg(list : TAsmList;reg: tregister);
  335. var
  336. tmpreg : tregister;
  337. tmpref : treference;
  338. begin
  339. if target_info.system=system_powerpc_macos then
  340. begin
  341. {Generate instruction to load the procedure address from
  342. the transition vector.}
  343. //TODO: Support cross-TOC calls.
  344. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  345. reference_reset(tmpref);
  346. tmpref.offset := 0;
  347. //tmpref.symaddr := refs_full;
  348. tmpref.base:= reg;
  349. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  350. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  351. end
  352. else
  353. list.concat(taicpu.op_reg(A_MTCTR,reg));
  354. list.concat(taicpu.op_none(A_BCTRL));
  355. //if target_info.system=system_powerpc_macos then
  356. // //NOP is not needed here.
  357. // list.concat(taicpu.op_none(A_NOP));
  358. include(current_procinfo.flags,pi_do_call);
  359. {
  360. if not(pi_do_call in current_procinfo.flags) then
  361. internalerror(2003060704);
  362. }
  363. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  364. end;
  365. {********************** load instructions ********************}
  366. procedure tcgppc.a_load_const_reg(list : TAsmList; size: TCGSize; a : aint; reg : TRegister);
  367. begin
  368. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  369. internalerror(2002090902);
  370. if (a >= low(smallint)) and
  371. (a <= high(smallint)) then
  372. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  373. else if ((a and $ffff) <> 0) then
  374. begin
  375. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  376. if ((a shr 16) <> 0) or
  377. (smallint(a and $ffff) < 0) then
  378. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  379. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  380. end
  381. else
  382. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  383. end;
  384. procedure tcgppc.a_load_reg_ref(list : TAsmList; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  385. const
  386. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  387. { indexed? updating?}
  388. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  389. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  390. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  391. var
  392. op: TAsmOp;
  393. ref2: TReference;
  394. begin
  395. ref2 := ref;
  396. fixref(list,ref2);
  397. if tosize in [OS_S8..OS_S16] then
  398. { storing is the same for signed and unsigned values }
  399. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  400. { 64 bit stuff should be handled separately }
  401. if tosize in [OS_64,OS_S64] then
  402. internalerror(200109236);
  403. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  404. a_load_store(list,op,reg,ref2);
  405. End;
  406. procedure tcgppc.a_load_ref_reg(list : TAsmList; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  407. const
  408. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  409. { indexed? updating?}
  410. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  411. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  412. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  413. { 64bit stuff should be handled separately }
  414. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  415. { 128bit stuff too }
  416. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  417. { there's no load-byte-with-sign-extend :( }
  418. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  419. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  420. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  421. var
  422. op: tasmop;
  423. ref2: treference;
  424. begin
  425. { TODO: optimize/take into consideration fromsize/tosize. Will }
  426. { probably only matter for OS_S8 loads though }
  427. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  428. internalerror(2002090902);
  429. ref2 := ref;
  430. fixref(list,ref2);
  431. { the caller is expected to have adjusted the reference already }
  432. { in this case }
  433. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  434. fromsize := tosize;
  435. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  436. a_load_store(list,op,reg,ref2);
  437. { sign extend shortint if necessary, since there is no }
  438. { load instruction that does that automatically (JM) }
  439. if fromsize = OS_S8 then
  440. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  441. end;
  442. procedure tcgppc.a_load_reg_reg(list : TAsmList;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  443. var
  444. instr: taicpu;
  445. begin
  446. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  447. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  448. (fromsize <> tosize)) or
  449. { needs to mask out the sign in the top 16 bits }
  450. ((fromsize = OS_S8) and
  451. (tosize = OS_16)) then
  452. case tosize of
  453. OS_8:
  454. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  455. reg2,reg1,0,31-8+1,31);
  456. OS_S8:
  457. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  458. OS_16:
  459. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  460. reg2,reg1,0,31-16+1,31);
  461. OS_S16:
  462. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  463. OS_32,OS_S32:
  464. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  465. else internalerror(2002090901);
  466. end
  467. else
  468. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  469. list.concat(instr);
  470. rg[R_INTREGISTER].add_move_instruction(instr);
  471. end;
  472. procedure tcgppc.a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister);
  473. var
  474. instr: taicpu;
  475. begin
  476. instr := taicpu.op_reg_reg(A_FMR,reg2,reg1);
  477. list.concat(instr);
  478. rg[R_FPUREGISTER].add_move_instruction(instr);
  479. end;
  480. procedure tcgppc.a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister);
  481. const
  482. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  483. { indexed? updating?}
  484. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  485. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  486. var
  487. op: tasmop;
  488. ref2: treference;
  489. begin
  490. { several functions call this procedure with OS_32 or OS_64 }
  491. { so this makes life easier (FK) }
  492. case size of
  493. OS_32,OS_F32:
  494. size:=OS_F32;
  495. OS_64,OS_F64,OS_C64:
  496. size:=OS_F64;
  497. else
  498. internalerror(200201121);
  499. end;
  500. ref2 := ref;
  501. fixref(list,ref2);
  502. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  503. a_load_store(list,op,reg,ref2);
  504. end;
  505. procedure tcgppc.a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference);
  506. const
  507. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  508. { indexed? updating?}
  509. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  510. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  511. var
  512. op: tasmop;
  513. ref2: treference;
  514. begin
  515. if not(size in [OS_F32,OS_F64]) then
  516. internalerror(200201122);
  517. ref2 := ref;
  518. fixref(list,ref2);
  519. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  520. a_load_store(list,op,reg,ref2);
  521. end;
  522. procedure tcgppc.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  523. begin
  524. a_op_const_reg_reg(list,op,size,a,reg,reg);
  525. end;
  526. procedure tcgppc.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  527. begin
  528. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  529. end;
  530. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  531. const
  532. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  533. begin
  534. if (op in overflowops) and
  535. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  536. a_load_reg_reg(list,OS_32,size,dst,dst);
  537. end;
  538. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  539. size: tcgsize; a: aint; src, dst: tregister);
  540. var
  541. l1,l2: longint;
  542. oplo, ophi: tasmop;
  543. scratchreg: tregister;
  544. useReg, gotrlwi: boolean;
  545. procedure do_lo_hi;
  546. begin
  547. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  548. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  549. end;
  550. begin
  551. if (op = OP_MOVE) then
  552. internalerror(2006031401);
  553. if op = OP_SUB then
  554. begin
  555. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  556. exit;
  557. end;
  558. ophi := TOpCG2AsmOpConstHi[op];
  559. oplo := TOpCG2AsmOpConstLo[op];
  560. gotrlwi := get_rlwi_const(a,l1,l2);
  561. if (op in [OP_AND,OP_OR,OP_XOR]) then
  562. begin
  563. if (a = 0) then
  564. begin
  565. if op = OP_AND then
  566. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  567. else
  568. a_load_reg_reg(list,size,size,src,dst);
  569. exit;
  570. end
  571. else if (a = -1) then
  572. begin
  573. case op of
  574. OP_OR:
  575. case size of
  576. OS_8, OS_S8:
  577. list.concat(taicpu.op_reg_const(A_LI,dst,255));
  578. OS_16, OS_S16:
  579. a_load_const_reg(list,OS_16,65535,dst);
  580. else
  581. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  582. end;
  583. OP_XOR:
  584. case size of
  585. OS_8, OS_S8:
  586. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,255));
  587. OS_16, OS_S16:
  588. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,65535));
  589. else
  590. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  591. end;
  592. OP_AND:
  593. a_load_reg_reg(list,size,size,src,dst);
  594. end;
  595. exit;
  596. end
  597. else if (aword(a) <= high(word)) and
  598. ((op <> OP_AND) or
  599. not gotrlwi) then
  600. begin
  601. if ((size = OS_8) and
  602. (byte(a) <> a)) or
  603. ((size = OS_S8) and
  604. (shortint(a) <> a)) then
  605. internalerror(200604142);
  606. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  607. { and/or/xor -> cannot overflow in high 16 bits }
  608. exit;
  609. end;
  610. { all basic constant instructions also have a shifted form that }
  611. { works only on the highest 16bits, so if lo(a) is 0, we can }
  612. { use that one }
  613. if (word(a) = 0) and
  614. (not(op = OP_AND) or
  615. not gotrlwi) then
  616. begin
  617. if (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  618. internalerror(200604141);
  619. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  620. exit;
  621. end;
  622. end
  623. else if (op = OP_ADD) then
  624. if a = 0 then
  625. begin
  626. a_load_reg_reg(list,size,size,src,dst);
  627. exit
  628. end
  629. else if (a >= low(smallint)) and
  630. (a <= high(smallint)) then
  631. begin
  632. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  633. maybeadjustresult(list,op,size,dst);
  634. exit;
  635. end;
  636. { otherwise, the instructions we can generate depend on the }
  637. { operation }
  638. useReg := false;
  639. case op of
  640. OP_DIV,OP_IDIV:
  641. if (a = 0) then
  642. internalerror(200208103)
  643. else if (a = 1) then
  644. begin
  645. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  646. exit
  647. end
  648. else if ispowerof2(a,l1) then
  649. begin
  650. case op of
  651. OP_DIV:
  652. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  653. OP_IDIV:
  654. begin
  655. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  656. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  657. end;
  658. end;
  659. exit;
  660. end
  661. else
  662. usereg := true;
  663. OP_IMUL, OP_MUL:
  664. if (a = 0) then
  665. begin
  666. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  667. exit
  668. end
  669. else if (a = 1) then
  670. begin
  671. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  672. exit
  673. end
  674. else if ispowerof2(a,l1) then
  675. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  676. else if (longint(a) >= low(smallint)) and
  677. (longint(a) <= high(smallint)) then
  678. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  679. else
  680. usereg := true;
  681. OP_ADD:
  682. begin
  683. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  684. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  685. smallint((a shr 16) + ord(smallint(a) < 0))));
  686. end;
  687. OP_OR:
  688. { try to use rlwimi }
  689. if gotrlwi and
  690. (src = dst) then
  691. begin
  692. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  693. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  694. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  695. scratchreg,0,l1,l2));
  696. end
  697. else
  698. do_lo_hi;
  699. OP_AND:
  700. { try to use rlwinm }
  701. if gotrlwi then
  702. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  703. src,0,l1,l2))
  704. else
  705. useReg := true;
  706. OP_XOR:
  707. do_lo_hi;
  708. OP_SHL,OP_SHR,OP_SAR:
  709. begin
  710. if (a and 31) <> 0 Then
  711. list.concat(taicpu.op_reg_reg_const(
  712. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  713. else
  714. a_load_reg_reg(list,size,size,src,dst);
  715. if (a shr 5) <> 0 then
  716. internalError(68991);
  717. end
  718. else
  719. internalerror(200109091);
  720. end;
  721. { if all else failed, load the constant in a register and then }
  722. { perform the operation }
  723. if useReg then
  724. begin
  725. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  726. a_load_const_reg(list,OS_32,a,scratchreg);
  727. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  728. end;
  729. maybeadjustresult(list,op,size,dst);
  730. end;
  731. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  732. size: tcgsize; src1, src2, dst: tregister);
  733. const
  734. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  735. (A_NONE,A_MR,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  736. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  737. begin
  738. if (op = OP_MOVE) then
  739. internalerror(2006031402);
  740. case op of
  741. OP_NEG,OP_NOT:
  742. begin
  743. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  744. if (op = OP_NOT) and
  745. not(size in [OS_32,OS_S32]) then
  746. { zero/sign extend result again }
  747. a_load_reg_reg(list,OS_32,size,dst,dst);
  748. end;
  749. else
  750. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  751. end;
  752. maybeadjustresult(list,op,size,dst);
  753. end;
  754. {*************** compare instructructions ****************}
  755. procedure tcgppc.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  756. l : tasmlabel);
  757. var
  758. scratch_register: TRegister;
  759. signed: boolean;
  760. begin
  761. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE,OC_EQ,OC_NE];
  762. { in the following case, we generate more efficient code when }
  763. { signed is false }
  764. if (cmp_op in [OC_EQ,OC_NE]) and
  765. (aword(a) >= $8000) and
  766. (aword(a) <= $ffff) then
  767. signed := false;
  768. if signed then
  769. if (a >= low(smallint)) and (a <= high(smallint)) Then
  770. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  771. else
  772. begin
  773. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  774. a_load_const_reg(list,OS_32,a,scratch_register);
  775. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  776. end
  777. else
  778. if (aword(a) <= $ffff) then
  779. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  780. else
  781. begin
  782. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  783. a_load_const_reg(list,OS_32,a,scratch_register);
  784. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  785. end;
  786. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  787. end;
  788. procedure tcgppc.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  789. reg1,reg2 : tregister;l : tasmlabel);
  790. var
  791. op: tasmop;
  792. begin
  793. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  794. op := A_CMPW
  795. else
  796. op := A_CMPLW;
  797. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  798. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  799. end;
  800. procedure tcgppc.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  801. begin
  802. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  803. end;
  804. procedure tcgppc.a_jmp_name(list : TAsmList;const s : string);
  805. var
  806. p : taicpu;
  807. begin
  808. if (target_info.system = system_powerpc_darwin) then
  809. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  810. else
  811. p := taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  812. p.is_jmp := true;
  813. list.concat(p)
  814. end;
  815. procedure tcgppc.a_jmp_always(list : TAsmList;l: tasmlabel);
  816. begin
  817. a_jmp(list,A_B,C_None,0,l);
  818. end;
  819. procedure tcgppc.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  820. var
  821. c: tasmcond;
  822. begin
  823. c := flags_to_cond(f);
  824. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  825. end;
  826. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  827. var
  828. testbit: byte;
  829. bitvalue: boolean;
  830. begin
  831. { get the bit to extract from the conditional register + its }
  832. { requested value (0 or 1) }
  833. testbit := ((f.cr-RS_CR0) * 4);
  834. case f.flag of
  835. F_EQ,F_NE:
  836. begin
  837. inc(testbit,2);
  838. bitvalue := f.flag = F_EQ;
  839. end;
  840. F_LT,F_GE:
  841. begin
  842. bitvalue := f.flag = F_LT;
  843. end;
  844. F_GT,F_LE:
  845. begin
  846. inc(testbit);
  847. bitvalue := f.flag = F_GT;
  848. end;
  849. else
  850. internalerror(200112261);
  851. end;
  852. { load the conditional register in the destination reg }
  853. list.concat(taicpu.op_reg(A_MFCR,reg));
  854. { we will move the bit that has to be tested to bit 0 by rotating }
  855. { left }
  856. testbit := (testbit + 1) and 31;
  857. { extract bit }
  858. list.concat(taicpu.op_reg_reg_const_const_const(
  859. A_RLWINM,reg,reg,testbit,31,31));
  860. { if we need the inverse, xor with 1 }
  861. if not bitvalue then
  862. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  863. end;
  864. (*
  865. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  866. var
  867. testbit: byte;
  868. bitvalue: boolean;
  869. begin
  870. { get the bit to extract from the conditional register + its }
  871. { requested value (0 or 1) }
  872. case f.simple of
  873. false:
  874. begin
  875. { we don't generate this in the compiler }
  876. internalerror(200109062);
  877. end;
  878. true:
  879. case f.cond of
  880. C_None:
  881. internalerror(200109063);
  882. C_LT..C_NU:
  883. begin
  884. testbit := (ord(f.cr) - ord(R_CR0))*4;
  885. inc(testbit,AsmCondFlag2BI[f.cond]);
  886. bitvalue := AsmCondFlagTF[f.cond];
  887. end;
  888. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  889. begin
  890. testbit := f.crbit
  891. bitvalue := AsmCondFlagTF[f.cond];
  892. end;
  893. else
  894. internalerror(200109064);
  895. end;
  896. end;
  897. { load the conditional register in the destination reg }
  898. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  899. { we will move the bit that has to be tested to bit 31 -> rotate }
  900. { left by bitpos+1 (remember, this is big-endian!) }
  901. if bitpos <> 31 then
  902. inc(bitpos)
  903. else
  904. bitpos := 0;
  905. { extract bit }
  906. list.concat(taicpu.op_reg_reg_const_const_const(
  907. A_RLWINM,reg,reg,bitpos,31,31));
  908. { if we need the inverse, xor with 1 }
  909. if not bitvalue then
  910. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  911. end;
  912. *)
  913. { *********** entry/exit code and address loading ************ }
  914. procedure tcgppc.g_save_standard_registers(list:TAsmList);
  915. begin
  916. { this work is done in g_proc_entry }
  917. end;
  918. procedure tcgppc.g_restore_standard_registers(list:TAsmList);
  919. begin
  920. { this work is done in g_proc_exit }
  921. end;
  922. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  923. { generated the entry code of a procedure/function. Note: localsize is the }
  924. { sum of the size necessary for local variables and the maximum possible }
  925. { combined size of ALL the parameters of a procedure called by the current }
  926. { one. }
  927. { This procedure may be called before, as well as after g_return_from_proc }
  928. { is called. NOTE registers are not to be allocated through the register }
  929. { allocator here, because the register colouring has already occured !! }
  930. var regcounter,firstregfpu,firstregint: TSuperRegister;
  931. href : treference;
  932. usesfpr,usesgpr,gotgot : boolean;
  933. cond : tasmcond;
  934. instr : taicpu;
  935. begin
  936. { CR and LR only have to be saved in case they are modified by the current }
  937. { procedure, but currently this isn't checked, so save them always }
  938. { following is the entry code as described in "Altivec Programming }
  939. { Interface Manual", bar the saving of AltiVec registers }
  940. a_reg_alloc(list,NR_STACK_POINTER_REG);
  941. usesgpr := false;
  942. usesfpr := false;
  943. if not(po_assembler in current_procinfo.procdef.procoptions) then
  944. begin
  945. { save link register? }
  946. if (pi_do_call in current_procinfo.flags) or
  947. ([cs_lineinfo,cs_debuginfo] * aktmoduleswitches <> []) then
  948. begin
  949. a_reg_alloc(list,NR_R0);
  950. { save return address... }
  951. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  952. { ... in caller's frame }
  953. case target_info.abi of
  954. abi_powerpc_aix:
  955. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  956. abi_powerpc_sysv:
  957. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  958. end;
  959. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  960. a_reg_dealloc(list,NR_R0);
  961. end;
  962. (*
  963. { save the CR if necessary in callers frame. }
  964. if target_info.abi = abi_powerpc_aix then
  965. if false then { Not needed at the moment. }
  966. begin
  967. a_reg_alloc(list,NR_R0);
  968. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  969. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  970. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  971. a_reg_dealloc(list,NR_R0);
  972. end;
  973. *)
  974. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  975. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  976. usesgpr := firstregint <> 32;
  977. usesfpr := firstregfpu <> 32;
  978. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  979. begin
  980. a_reg_alloc(list,NR_R12);
  981. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  982. end;
  983. end;
  984. { no GOT pointer loaded yet }
  985. gotgot:=false;
  986. if usesfpr then
  987. begin
  988. { save floating-point registers
  989. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  990. begin
  991. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g'));
  992. gotgot:=true;
  993. end
  994. else
  995. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)));
  996. }
  997. reference_reset_base(href,NR_R1,-8);
  998. for regcounter:=firstregfpu to RS_F31 do
  999. begin
  1000. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  1001. dec(href.offset,8);
  1002. end;
  1003. { compute start of gpr save area }
  1004. inc(href.offset,4);
  1005. end
  1006. else
  1007. { compute start of gpr save area }
  1008. reference_reset_base(href,NR_R1,-4);
  1009. { save gprs and fetch GOT pointer }
  1010. if usesgpr then
  1011. begin
  1012. {
  1013. if cs_create_pic in aktmoduleswitches then
  1014. begin
  1015. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g'));
  1016. gotgot:=true;
  1017. end
  1018. else
  1019. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)))
  1020. }
  1021. if (firstregint <= RS_R22) or
  1022. ((cs_opt_size in aktoptimizerswitches) and
  1023. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1024. (firstregint <= RS_R29)) then
  1025. begin
  1026. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1027. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1028. end
  1029. else
  1030. for regcounter:=firstregint to RS_R31 do
  1031. begin
  1032. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  1033. dec(href.offset,4);
  1034. end;
  1035. end;
  1036. { done in ncgutil because it may only be released after the parameters }
  1037. { have been moved to their final resting place }
  1038. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  1039. { a_reg_dealloc(list,NR_R12); }
  1040. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1041. (*
  1042. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1043. case target_info.system of
  1044. system_powerpc_darwin:
  1045. begin
  1046. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1047. fillchar(cond,sizeof(cond),0);
  1048. cond.simple:=false;
  1049. cond.bo:=20;
  1050. cond.bi:=31;
  1051. instr:=taicpu.op_sym(A_BCL,current_procinfo.CurrGOTLabel);
  1052. instr.setcondition(cond);
  1053. list.concat(instr);
  1054. a_label(list,current_procinfo.CurrGOTLabel);
  1055. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1056. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1057. end;
  1058. else
  1059. begin
  1060. a_reg_alloc(list,NR_R31);
  1061. { place GOT ptr in r31 }
  1062. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1063. end;
  1064. end;
  1065. *)
  1066. if (not nostackframe) and
  1067. (localsize <> 0) then
  1068. begin
  1069. if (localsize <= high(smallint)) then
  1070. begin
  1071. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1072. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1073. end
  1074. else
  1075. begin
  1076. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1077. { can't use getregisterint here, the register colouring }
  1078. { is already done when we get here }
  1079. href.index := NR_R11;
  1080. a_reg_alloc(list,href.index);
  1081. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1082. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1083. a_reg_dealloc(list,href.index);
  1084. end;
  1085. end;
  1086. { save the CR if necessary ( !!! never done currently ) }
  1087. { still need to find out where this has to be done for SystemV
  1088. a_reg_alloc(list,R_0);
  1089. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1090. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1091. new_reference(STACK_POINTER_REG,LA_CR)));
  1092. a_reg_dealloc(list,R_0);
  1093. }
  1094. { now comes the AltiVec context save, not yet implemented !!! }
  1095. end;
  1096. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1097. { This procedure may be called before, as well as after g_stackframe_entry }
  1098. { is called. NOTE registers are not to be allocated through the register }
  1099. { allocator here, because the register colouring has already occured !! }
  1100. var
  1101. regcounter,firstregfpu,firstregint: TsuperRegister;
  1102. href : treference;
  1103. usesfpr,usesgpr,genret : boolean;
  1104. localsize: aint;
  1105. begin
  1106. { AltiVec context restore, not yet implemented !!! }
  1107. usesfpr:=false;
  1108. usesgpr:=false;
  1109. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1110. begin
  1111. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  1112. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  1113. usesgpr := firstregint <> 32;
  1114. usesfpr := firstregfpu <> 32;
  1115. end;
  1116. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1117. { adjust r1 }
  1118. { (register allocator is no longer valid at this time and an add of 0 }
  1119. { is translated into a move, which is then registered with the register }
  1120. { allocator, causing a crash }
  1121. if (not nostackframe) and
  1122. (localsize <> 0) then
  1123. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1124. { no return (blr) generated yet }
  1125. genret:=true;
  1126. if usesfpr then
  1127. begin
  1128. reference_reset_base(href,NR_R1,-8);
  1129. for regcounter := firstregfpu to RS_F31 do
  1130. begin
  1131. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1132. dec(href.offset,8);
  1133. end;
  1134. inc(href.offset,4);
  1135. end
  1136. else
  1137. reference_reset_base(href,NR_R1,-4);
  1138. if (usesgpr) then
  1139. begin
  1140. if (firstregint <= RS_R22) or
  1141. ((cs_opt_size in aktoptimizerswitches) and
  1142. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1143. (firstregint <= RS_R29)) then
  1144. begin
  1145. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1146. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1147. end
  1148. else
  1149. for regcounter:=firstregint to RS_R31 do
  1150. begin
  1151. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  1152. dec(href.offset,4);
  1153. end;
  1154. end;
  1155. (*
  1156. { restore fprs and return }
  1157. if usesfpr then
  1158. begin
  1159. { address of fpr save area to r11 }
  1160. r:=NR_R12;
  1161. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1162. {
  1163. if (pi_do_call in current_procinfo.flags) then
  1164. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  1165. else
  1166. { leaf node => lr haven't to be restored }
  1167. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  1168. genret:=false;
  1169. }
  1170. end;
  1171. *)
  1172. { if we didn't generate the return code, we've to do it now }
  1173. if genret then
  1174. begin
  1175. { load link register? }
  1176. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1177. begin
  1178. if (pi_do_call in current_procinfo.flags) then
  1179. begin
  1180. case target_info.abi of
  1181. abi_powerpc_aix:
  1182. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1183. abi_powerpc_sysv:
  1184. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1185. end;
  1186. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1187. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1188. end;
  1189. (*
  1190. { restore the CR if necessary from callers frame}
  1191. if target_info.abi = abi_powerpc_aix then
  1192. if false then { Not needed at the moment. }
  1193. begin
  1194. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1195. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1196. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1197. a_reg_dealloc(list,NR_R0);
  1198. end;
  1199. *)
  1200. end;
  1201. list.concat(taicpu.op_none(A_BLR));
  1202. end;
  1203. end;
  1204. function tcgppc.save_regs(list : TAsmList):longint;
  1205. {Generates code which saves used non-volatile registers in
  1206. the save area right below the address the stackpointer point to.
  1207. Returns the actual used save area size.}
  1208. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1209. usesfpr,usesgpr: boolean;
  1210. href : treference;
  1211. offset: aint;
  1212. regcounter2, firstfpureg: Tsuperregister;
  1213. begin
  1214. usesfpr:=false;
  1215. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1216. begin
  1217. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1218. case target_info.abi of
  1219. abi_powerpc_aix:
  1220. firstfpureg := RS_F14;
  1221. abi_powerpc_sysv:
  1222. firstfpureg := RS_F9;
  1223. else
  1224. internalerror(2003122903);
  1225. end;
  1226. for regcounter:=firstfpureg to RS_F31 do
  1227. begin
  1228. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1229. begin
  1230. usesfpr:=true;
  1231. firstregfpu:=regcounter;
  1232. break;
  1233. end;
  1234. end;
  1235. end;
  1236. usesgpr:=false;
  1237. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1238. for regcounter2:=RS_R13 to RS_R31 do
  1239. begin
  1240. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1241. begin
  1242. usesgpr:=true;
  1243. firstreggpr:=regcounter2;
  1244. break;
  1245. end;
  1246. end;
  1247. offset:= 0;
  1248. { save floating-point registers }
  1249. if usesfpr then
  1250. for regcounter := firstregfpu to RS_F31 do
  1251. begin
  1252. offset:= offset - 8;
  1253. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1254. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1255. end;
  1256. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1257. { save gprs in gpr save area }
  1258. if usesgpr then
  1259. if firstreggpr < RS_R30 then
  1260. begin
  1261. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1262. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1263. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1264. {STMW stores multiple registers}
  1265. end
  1266. else
  1267. begin
  1268. for regcounter := firstreggpr to RS_R31 do
  1269. begin
  1270. offset:= offset - 4;
  1271. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1272. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1273. end;
  1274. end;
  1275. { now comes the AltiVec context save, not yet implemented !!! }
  1276. save_regs:= -offset;
  1277. end;
  1278. procedure tcgppc.restore_regs(list : TAsmList);
  1279. {Generates code which restores used non-volatile registers from
  1280. the save area right below the address the stackpointer point to.}
  1281. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1282. usesfpr,usesgpr: boolean;
  1283. href : treference;
  1284. offset: integer;
  1285. regcounter2, firstfpureg: Tsuperregister;
  1286. begin
  1287. usesfpr:=false;
  1288. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1289. begin
  1290. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1291. case target_info.abi of
  1292. abi_powerpc_aix:
  1293. firstfpureg := RS_F14;
  1294. abi_powerpc_sysv:
  1295. firstfpureg := RS_F9;
  1296. else
  1297. internalerror(2003122903);
  1298. end;
  1299. for regcounter:=firstfpureg to RS_F31 do
  1300. begin
  1301. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1302. begin
  1303. usesfpr:=true;
  1304. firstregfpu:=regcounter;
  1305. break;
  1306. end;
  1307. end;
  1308. end;
  1309. usesgpr:=false;
  1310. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1311. for regcounter2:=RS_R13 to RS_R31 do
  1312. begin
  1313. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1314. begin
  1315. usesgpr:=true;
  1316. firstreggpr:=regcounter2;
  1317. break;
  1318. end;
  1319. end;
  1320. offset:= 0;
  1321. { restore fp registers }
  1322. if usesfpr then
  1323. for regcounter := firstregfpu to RS_F31 do
  1324. begin
  1325. offset:= offset - 8;
  1326. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1327. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1328. end;
  1329. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1330. { restore gprs }
  1331. if usesgpr then
  1332. if firstreggpr < RS_R30 then
  1333. begin
  1334. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1335. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1336. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1337. {LMW loads multiple registers}
  1338. end
  1339. else
  1340. begin
  1341. for regcounter := firstreggpr to RS_R31 do
  1342. begin
  1343. offset:= offset - 4;
  1344. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1345. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1346. end;
  1347. end;
  1348. { now comes the AltiVec context restore, not yet implemented !!! }
  1349. end;
  1350. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1351. (* NOT IN USE *)
  1352. { generated the entry code of a procedure/function. Note: localsize is the }
  1353. { sum of the size necessary for local variables and the maximum possible }
  1354. { combined size of ALL the parameters of a procedure called by the current }
  1355. { one }
  1356. const
  1357. macosLinkageAreaSize = 24;
  1358. var
  1359. href : treference;
  1360. registerSaveAreaSize : longint;
  1361. begin
  1362. if (localsize mod 8) <> 0 then
  1363. internalerror(58991);
  1364. { CR and LR only have to be saved in case they are modified by the current }
  1365. { procedure, but currently this isn't checked, so save them always }
  1366. { following is the entry code as described in "Altivec Programming }
  1367. { Interface Manual", bar the saving of AltiVec registers }
  1368. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1369. a_reg_alloc(list,NR_R0);
  1370. { save return address in callers frame}
  1371. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1372. { ... in caller's frame }
  1373. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1374. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1375. a_reg_dealloc(list,NR_R0);
  1376. { save non-volatile registers in callers frame}
  1377. registerSaveAreaSize:= save_regs(list);
  1378. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1379. a_reg_alloc(list,NR_R0);
  1380. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1381. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1382. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1383. a_reg_dealloc(list,NR_R0);
  1384. (*
  1385. { save pointer to incoming arguments }
  1386. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1387. *)
  1388. (*
  1389. a_reg_alloc(list,R_12);
  1390. { 0 or 8 based on SP alignment }
  1391. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1392. R_12,STACK_POINTER_REG,0,28,28));
  1393. { add in stack length }
  1394. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1395. -localsize));
  1396. { establish new alignment }
  1397. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1398. a_reg_dealloc(list,R_12);
  1399. *)
  1400. { allocate stack frame }
  1401. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1402. inc(localsize,tg.lasttemp);
  1403. localsize:=align(localsize,16);
  1404. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1405. if (localsize <> 0) then
  1406. begin
  1407. if (localsize <= high(smallint)) then
  1408. begin
  1409. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1410. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1411. end
  1412. else
  1413. begin
  1414. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1415. href.index := NR_R11;
  1416. a_reg_alloc(list,href.index);
  1417. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1418. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1419. a_reg_dealloc(list,href.index);
  1420. end;
  1421. end;
  1422. end;
  1423. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : aint);
  1424. (* NOT IN USE *)
  1425. var
  1426. href : treference;
  1427. begin
  1428. a_reg_alloc(list,NR_R0);
  1429. { restore stack pointer }
  1430. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1431. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1432. (*
  1433. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1434. *)
  1435. { restore the CR if necessary from callers frame
  1436. ( !!! always done currently ) }
  1437. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1438. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1439. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1440. a_reg_dealloc(list,NR_R0);
  1441. (*
  1442. { restore return address from callers frame }
  1443. reference_reset_base(href,STACK_POINTER_REG,8);
  1444. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1445. *)
  1446. { restore non-volatile registers from callers frame }
  1447. restore_regs(list);
  1448. (*
  1449. { return to caller }
  1450. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1451. list.concat(taicpu.op_none(A_BLR));
  1452. *)
  1453. { restore return address from callers frame }
  1454. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1455. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1456. { return to caller }
  1457. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1458. list.concat(taicpu.op_none(A_BLR));
  1459. end;
  1460. procedure tcgppc.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1461. var
  1462. ref2, tmpref: treference;
  1463. begin
  1464. ref2 := ref;
  1465. fixref(list,ref2);
  1466. if assigned(ref2.symbol) then
  1467. begin
  1468. if target_info.system = system_powerpc_macos then
  1469. begin
  1470. if macos_direct_globals then
  1471. begin
  1472. reference_reset(tmpref);
  1473. tmpref.offset := ref2.offset;
  1474. tmpref.symbol := ref2.symbol;
  1475. tmpref.base := NR_NO;
  1476. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1477. end
  1478. else
  1479. begin
  1480. reference_reset(tmpref);
  1481. tmpref.symbol := ref2.symbol;
  1482. tmpref.offset := 0;
  1483. tmpref.base := NR_RTOC;
  1484. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1485. if ref2.offset <> 0 then
  1486. begin
  1487. reference_reset(tmpref);
  1488. tmpref.offset := ref2.offset;
  1489. tmpref.base:= r;
  1490. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1491. end;
  1492. end;
  1493. if ref2.base <> NR_NO then
  1494. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1495. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1496. end
  1497. else
  1498. begin
  1499. { add the symbol's value to the base of the reference, and if the }
  1500. { reference doesn't have a base, create one }
  1501. reference_reset(tmpref);
  1502. tmpref.offset := ref2.offset;
  1503. tmpref.symbol := ref2.symbol;
  1504. tmpref.relsymbol := ref2.relsymbol;
  1505. tmpref.refaddr := addr_hi;
  1506. if ref2.base<> NR_NO then
  1507. begin
  1508. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1509. ref2.base,tmpref));
  1510. end
  1511. else
  1512. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1513. tmpref.base := NR_NO;
  1514. tmpref.refaddr := addr_lo;
  1515. { can be folded with one of the next instructions by the }
  1516. { optimizer probably }
  1517. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1518. end
  1519. end
  1520. else if ref2.offset <> 0 Then
  1521. if ref2.base <> NR_NO then
  1522. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1523. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1524. { occurs, so now only ref.offset has to be loaded }
  1525. else
  1526. a_load_const_reg(list,OS_32,ref2.offset,r)
  1527. else if ref2.index <> NR_NO Then
  1528. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1529. else if (ref2.base <> NR_NO) and
  1530. (r <> ref2.base) then
  1531. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1532. else
  1533. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1534. end;
  1535. { ************* concatcopy ************ }
  1536. {$ifndef ppc603}
  1537. const
  1538. maxmoveunit = 8;
  1539. {$else ppc603}
  1540. const
  1541. maxmoveunit = 4;
  1542. {$endif ppc603}
  1543. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1544. var
  1545. countreg: TRegister;
  1546. src, dst: TReference;
  1547. lab: tasmlabel;
  1548. count, count2: aint;
  1549. size: tcgsize;
  1550. copyreg: tregister;
  1551. begin
  1552. {$ifdef extdebug}
  1553. if len > high(longint) then
  1554. internalerror(2002072704);
  1555. {$endif extdebug}
  1556. if (references_equal(source,dest)) then
  1557. exit;
  1558. { make sure short loads are handled as optimally as possible }
  1559. if (len <= maxmoveunit) and
  1560. (byte(len) in [1,2,4,8]) then
  1561. begin
  1562. if len < 8 then
  1563. begin
  1564. size := int_cgsize(len);
  1565. a_load_ref_ref(list,size,size,source,dest);
  1566. end
  1567. else
  1568. begin
  1569. copyreg := getfpuregister(list,OS_F64);
  1570. a_loadfpu_ref_reg(list,OS_F64,source,copyreg);
  1571. a_loadfpu_reg_ref(list,OS_F64,copyreg,dest);
  1572. end;
  1573. exit;
  1574. end;
  1575. count := len div maxmoveunit;
  1576. reference_reset(src);
  1577. reference_reset(dst);
  1578. { load the address of source into src.base }
  1579. if (count > 4) or
  1580. not issimpleref(source) or
  1581. ((source.index <> NR_NO) and
  1582. ((source.offset + longint(len)) > high(smallint))) then
  1583. begin
  1584. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1585. a_loadaddr_ref_reg(list,source,src.base);
  1586. end
  1587. else
  1588. begin
  1589. src := source;
  1590. end;
  1591. { load the address of dest into dst.base }
  1592. if (count > 4) or
  1593. not issimpleref(dest) or
  1594. ((dest.index <> NR_NO) and
  1595. ((dest.offset + longint(len)) > high(smallint))) then
  1596. begin
  1597. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1598. a_loadaddr_ref_reg(list,dest,dst.base);
  1599. end
  1600. else
  1601. begin
  1602. dst := dest;
  1603. end;
  1604. {$ifndef ppc603}
  1605. if count > 4 then
  1606. { generate a loop }
  1607. begin
  1608. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1609. { have to be set to 8. I put an Inc there so debugging may be }
  1610. { easier (should offset be different from zero here, it will be }
  1611. { easy to notice in the generated assembler }
  1612. inc(dst.offset,8);
  1613. inc(src.offset,8);
  1614. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1615. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1616. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1617. a_load_const_reg(list,OS_32,count,countreg);
  1618. copyreg := getfpuregister(list,OS_F64);
  1619. a_reg_sync(list,copyreg);
  1620. current_asmdata.getjumplabel(lab);
  1621. a_label(list, lab);
  1622. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1623. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1624. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1625. a_jmp(list,A_BC,C_NE,0,lab);
  1626. a_reg_sync(list,copyreg);
  1627. len := len mod 8;
  1628. end;
  1629. count := len div 8;
  1630. if count > 0 then
  1631. { unrolled loop }
  1632. begin
  1633. copyreg := getfpuregister(list,OS_F64);
  1634. for count2 := 1 to count do
  1635. begin
  1636. a_loadfpu_ref_reg(list,OS_F64,src,copyreg);
  1637. a_loadfpu_reg_ref(list,OS_F64,copyreg,dst);
  1638. inc(src.offset,8);
  1639. inc(dst.offset,8);
  1640. end;
  1641. len := len mod 8;
  1642. end;
  1643. if (len and 4) <> 0 then
  1644. begin
  1645. a_reg_alloc(list,NR_R0);
  1646. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1647. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1648. inc(src.offset,4);
  1649. inc(dst.offset,4);
  1650. a_reg_dealloc(list,NR_R0);
  1651. end;
  1652. {$else not ppc603}
  1653. if count > 4 then
  1654. { generate a loop }
  1655. begin
  1656. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1657. { have to be set to 4. I put an Inc there so debugging may be }
  1658. { easier (should offset be different from zero here, it will be }
  1659. { easy to notice in the generated assembler }
  1660. inc(dst.offset,4);
  1661. inc(src.offset,4);
  1662. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1663. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1664. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1665. a_load_const_reg(list,OS_32,count,countreg);
  1666. { explicitely allocate R_0 since it can be used safely here }
  1667. { (for holding date that's being copied) }
  1668. a_reg_alloc(list,NR_R0);
  1669. current_asmdata.getjumplabel(lab);
  1670. a_label(list, lab);
  1671. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1672. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1673. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1674. a_jmp(list,A_BC,C_NE,0,lab);
  1675. a_reg_dealloc(list,NR_R0);
  1676. len := len mod 4;
  1677. end;
  1678. count := len div 4;
  1679. if count > 0 then
  1680. { unrolled loop }
  1681. begin
  1682. a_reg_alloc(list,NR_R0);
  1683. for count2 := 1 to count do
  1684. begin
  1685. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1686. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1687. inc(src.offset,4);
  1688. inc(dst.offset,4);
  1689. end;
  1690. a_reg_dealloc(list,NR_R0);
  1691. len := len mod 4;
  1692. end;
  1693. {$endif not ppc603}
  1694. { copy the leftovers }
  1695. if (len and 2) <> 0 then
  1696. begin
  1697. a_reg_alloc(list,NR_R0);
  1698. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1699. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1700. inc(src.offset,2);
  1701. inc(dst.offset,2);
  1702. a_reg_dealloc(list,NR_R0);
  1703. end;
  1704. if (len and 1) <> 0 then
  1705. begin
  1706. a_reg_alloc(list,NR_R0);
  1707. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1708. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1709. a_reg_dealloc(list,NR_R0);
  1710. end;
  1711. end;
  1712. procedure tcgppc.g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);
  1713. var
  1714. hl : tasmlabel;
  1715. begin
  1716. if not(cs_check_overflow in aktlocalswitches) then
  1717. exit;
  1718. current_asmdata.getjumplabel(hl);
  1719. if not ((def.deftype=pointerdef) or
  1720. ((def.deftype=orddef) and
  1721. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1722. bool8bit,bool16bit,bool32bit]))) then
  1723. begin
  1724. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1725. a_jmp(list,A_BC,C_NO,7,hl)
  1726. end
  1727. else
  1728. a_jmp_cond(list,OC_AE,hl);
  1729. a_call_name(list,'FPC_OVERFLOW');
  1730. a_label(list,hl);
  1731. end;
  1732. procedure tcgppc.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1733. procedure loadvmttor11;
  1734. var
  1735. href : treference;
  1736. begin
  1737. reference_reset_base(href,NR_R3,0);
  1738. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  1739. end;
  1740. procedure op_onr11methodaddr;
  1741. var
  1742. href : treference;
  1743. begin
  1744. if (procdef.extnumber=$ffff) then
  1745. Internalerror(200006139);
  1746. { call/jmp vmtoffs(%eax) ; method offs }
  1747. reference_reset_base(href,NR_R11,procdef._class.vmtmethodoffset(procdef.extnumber));
  1748. if not((longint(href.offset) >= low(smallint)) and
  1749. (longint(href.offset) <= high(smallint))) then
  1750. begin
  1751. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  1752. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  1753. href.offset := smallint(href.offset and $ffff);
  1754. end;
  1755. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  1756. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  1757. list.concat(taicpu.op_none(A_BCTR));
  1758. end;
  1759. var
  1760. make_global : boolean;
  1761. begin
  1762. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1763. Internalerror(200006137);
  1764. if not assigned(procdef._class) or
  1765. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1766. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1767. Internalerror(200006138);
  1768. if procdef.owner.symtabletype<>objectsymtable then
  1769. Internalerror(200109191);
  1770. make_global:=false;
  1771. if (not current_module.is_unit) or
  1772. (cs_create_smart in aktmoduleswitches) or
  1773. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1774. make_global:=true;
  1775. if make_global then
  1776. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1777. else
  1778. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1779. { set param1 interface to self }
  1780. g_adjust_self_value(list,procdef,ioffset);
  1781. { case 4 }
  1782. if po_virtualmethod in procdef.procoptions then
  1783. begin
  1784. loadvmttor11;
  1785. op_onr11methodaddr;
  1786. end
  1787. { case 0 }
  1788. else
  1789. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1790. List.concat(Tai_symbol_end.Createname(labelname));
  1791. end;
  1792. {***************** This is private property, keep out! :) *****************}
  1793. function tcgppc.issimpleref(const ref: treference): boolean;
  1794. begin
  1795. if (ref.base = NR_NO) and
  1796. (ref.index <> NR_NO) then
  1797. internalerror(200208101);
  1798. result :=
  1799. not(assigned(ref.symbol)) and
  1800. (((ref.index = NR_NO) and
  1801. (ref.offset >= low(smallint)) and
  1802. (ref.offset <= high(smallint))) or
  1803. ((ref.index <> NR_NO) and
  1804. (ref.offset = 0)));
  1805. end;
  1806. function tcgppc.fixref(list: TAsmList; var ref: treference): boolean;
  1807. var
  1808. tmpreg: tregister;
  1809. begin
  1810. result := false;
  1811. if (target_info.system = system_powerpc_darwin) and
  1812. assigned(ref.symbol) and
  1813. (ref.symbol.bind = AB_EXTERNAL) then
  1814. begin
  1815. tmpreg := g_indirect_sym_load(list,ref.symbol.name);
  1816. if (ref.base = NR_NO) then
  1817. ref.base := tmpreg
  1818. else if (ref.index = NR_NO) then
  1819. ref.index := tmpreg
  1820. else
  1821. begin
  1822. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1823. ref.base := tmpreg;
  1824. end;
  1825. ref.symbol := nil;
  1826. end;
  1827. if (ref.base = NR_NO) then
  1828. begin
  1829. ref.base := ref.index;
  1830. ref.index := NR_NO;
  1831. end;
  1832. if (ref.base <> NR_NO) then
  1833. begin
  1834. if (ref.index <> NR_NO) and
  1835. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1836. begin
  1837. result := true;
  1838. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1839. list.concat(taicpu.op_reg_reg_reg(
  1840. A_ADD,tmpreg,ref.base,ref.index));
  1841. ref.index := NR_NO;
  1842. ref.base := tmpreg;
  1843. end
  1844. end
  1845. else
  1846. if ref.index <> NR_NO then
  1847. internalerror(200208102);
  1848. end;
  1849. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1850. { that's the case, we can use rlwinm to do an AND operation }
  1851. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1852. var
  1853. temp : longint;
  1854. testbit : aint;
  1855. compare: boolean;
  1856. begin
  1857. get_rlwi_const := false;
  1858. if (a = 0) or (a = -1) then
  1859. exit;
  1860. { start with the lowest bit }
  1861. testbit := 1;
  1862. { check its value }
  1863. compare := boolean(a and testbit);
  1864. { find out how long the run of bits with this value is }
  1865. { (it's impossible that all bits are 1 or 0, because in that case }
  1866. { this function wouldn't have been called) }
  1867. l1 := 31;
  1868. while (((a and testbit) <> 0) = compare) do
  1869. begin
  1870. testbit := testbit shl 1;
  1871. dec(l1);
  1872. end;
  1873. { check the length of the run of bits that comes next }
  1874. compare := not compare;
  1875. l2 := l1;
  1876. while (((a and testbit) <> 0) = compare) and
  1877. (l2 >= 0) do
  1878. begin
  1879. testbit := testbit shl 1;
  1880. dec(l2);
  1881. end;
  1882. { and finally the check whether the rest of the bits all have the }
  1883. { same value }
  1884. compare := not compare;
  1885. temp := l2;
  1886. if temp >= 0 then
  1887. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1888. exit;
  1889. { we have done "not(not(compare))", so compare is back to its }
  1890. { initial value. If the lowest bit was 0, a is of the form }
  1891. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1892. { because l2 now contains the position of the last zero of the }
  1893. { first run instead of that of the first 1) so switch l1 and l2 }
  1894. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1895. if not compare then
  1896. begin
  1897. temp := l1;
  1898. l1 := l2+1;
  1899. l2 := temp;
  1900. end
  1901. else
  1902. { otherwise, l1 currently contains the position of the last }
  1903. { zero instead of that of the first 1 of the second run -> +1 }
  1904. inc(l1);
  1905. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1906. l1 := l1 and 31;
  1907. l2 := l2 and 31;
  1908. get_rlwi_const := true;
  1909. end;
  1910. procedure tcgppc.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  1911. ref: treference);
  1912. var
  1913. tmpreg: tregister;
  1914. tmpref: treference;
  1915. largeOffset: Boolean;
  1916. begin
  1917. tmpreg := NR_NO;
  1918. if target_info.system = system_powerpc_macos then
  1919. begin
  1920. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1921. high(smallint)-low(smallint));
  1922. if assigned(ref.symbol) then
  1923. begin {Load symbol's value}
  1924. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1925. reference_reset(tmpref);
  1926. tmpref.symbol := ref.symbol;
  1927. tmpref.base := NR_RTOC;
  1928. if macos_direct_globals then
  1929. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1930. else
  1931. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1932. end;
  1933. if largeOffset then
  1934. begin {Add hi part of offset}
  1935. reference_reset(tmpref);
  1936. if Smallint(Lo(ref.offset)) < 0 then
  1937. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1938. else
  1939. tmpref.offset := Hi(ref.offset);
  1940. if (tmpreg <> NR_NO) then
  1941. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1942. else
  1943. begin
  1944. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1945. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1946. end;
  1947. end;
  1948. if (tmpreg <> NR_NO) then
  1949. begin
  1950. {Add content of base register}
  1951. if ref.base <> NR_NO then
  1952. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1953. ref.base,tmpreg));
  1954. {Make ref ready to be used by op}
  1955. ref.symbol:= nil;
  1956. ref.base:= tmpreg;
  1957. if largeOffset then
  1958. ref.offset := Smallint(Lo(ref.offset));
  1959. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1960. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1961. end
  1962. else
  1963. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1964. end
  1965. else {if target_info.system <> system_powerpc_macos}
  1966. begin
  1967. if assigned(ref.symbol) or
  1968. (cardinal(ref.offset-low(smallint)) >
  1969. high(smallint)-low(smallint)) then
  1970. begin
  1971. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1972. reference_reset(tmpref);
  1973. tmpref.symbol := ref.symbol;
  1974. tmpref.relsymbol := ref.relsymbol;
  1975. tmpref.offset := ref.offset;
  1976. tmpref.refaddr := addr_hi;
  1977. if ref.base <> NR_NO then
  1978. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1979. ref.base,tmpref))
  1980. else
  1981. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1982. ref.base := tmpreg;
  1983. ref.refaddr := addr_lo;
  1984. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1985. end
  1986. else
  1987. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1988. end;
  1989. end;
  1990. procedure tcgppc.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  1991. crval: longint; l: tasmlabel);
  1992. var
  1993. p: taicpu;
  1994. begin
  1995. p := taicpu.op_sym(op,l);
  1996. if op <> A_B then
  1997. create_cond_norm(c,crval,p.condition);
  1998. p.is_jmp := true;
  1999. list.concat(p)
  2000. end;
  2001. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  2002. begin
  2003. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  2004. end;
  2005. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2006. begin
  2007. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  2008. end;
  2009. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2010. begin
  2011. case op of
  2012. OP_AND,OP_OR,OP_XOR:
  2013. begin
  2014. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2015. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2016. end;
  2017. OP_ADD:
  2018. begin
  2019. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2020. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2021. end;
  2022. OP_SUB:
  2023. begin
  2024. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2025. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2026. end;
  2027. else
  2028. internalerror(2002072801);
  2029. end;
  2030. end;
  2031. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  2032. const
  2033. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2034. (A_SUBIC,A_SUBC,A_ADDME));
  2035. var
  2036. tmpreg: tregister;
  2037. tmpreg64: tregister64;
  2038. issub: boolean;
  2039. begin
  2040. case op of
  2041. OP_AND,OP_OR,OP_XOR:
  2042. begin
  2043. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  2044. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2045. regdst.reghi);
  2046. end;
  2047. OP_ADD, OP_SUB:
  2048. begin
  2049. if (value < 0) then
  2050. begin
  2051. if op = OP_ADD then
  2052. op := OP_SUB
  2053. else
  2054. op := OP_ADD;
  2055. value := -value;
  2056. end;
  2057. if (longint(value) <> 0) then
  2058. begin
  2059. issub := op = OP_SUB;
  2060. if (value > 0) and
  2061. (value-ord(issub) <= 32767) then
  2062. begin
  2063. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2064. regdst.reglo,regsrc.reglo,longint(value)));
  2065. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2066. regdst.reghi,regsrc.reghi));
  2067. end
  2068. else if ((value shr 32) = 0) then
  2069. begin
  2070. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2071. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2072. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2073. regdst.reglo,regsrc.reglo,tmpreg));
  2074. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2075. regdst.reghi,regsrc.reghi));
  2076. end
  2077. else
  2078. begin
  2079. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2080. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2081. a_load64_const_reg(list,value,tmpreg64);
  2082. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  2083. end
  2084. end
  2085. else
  2086. begin
  2087. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2088. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2089. regdst.reghi);
  2090. end;
  2091. end;
  2092. else
  2093. internalerror(2002072802);
  2094. end;
  2095. end;
  2096. begin
  2097. cg := tcgppc.create;
  2098. cg64 :=tcg64fppc.create;
  2099. end.