cgcpu.pas 86 KB

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