cgcpu.pas 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,node,cg64f32,cginfo;
  25. type
  26. tcgppc = class(tcg)
  27. { passing parameters, per default the parameter is pushed }
  28. { nr gives the number of the parameter (enumerated from }
  29. { left to right), this allows to move the parameter to }
  30. { register, if the cpu supports register calling }
  31. { conventions }
  32. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  33. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  34. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  35. procedure a_call_name(list : taasmoutput;const s : string);override;
  36. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  37. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  38. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  39. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  40. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  41. size: tcgsize; a: aword; src, dst: tregister); override;
  42. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  43. size: tcgsize; src1, src2, dst: tregister); override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  46. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  47. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  48. procedure a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  49. { fpu move instructions }
  50. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  51. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  52. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  53. { comparison operations }
  54. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  55. l : tasmlabel);override;
  56. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  57. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  58. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  59. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  60. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);override;
  61. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  62. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  63. procedure g_restore_frame_pointer(list : taasmoutput);override;
  64. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  65. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  66. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  67. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  68. { that's the case, we can use rlwinm to do an AND operation }
  69. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  70. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  71. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  72. procedure g_save_all_registers(list : taasmoutput);override;
  73. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  74. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  75. private
  76. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  77. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  78. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  79. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  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: taasmoutput; 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:taasmoutput;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: taasmoutput; op: tasmop;
  96. c: tasmcondflag; crval: longint; l: tasmlabel);
  97. end;
  98. tcg64fppc = class(tcg64f32)
  99. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  100. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  101. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  102. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  103. end;
  104. const
  105. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  106. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  107. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  108. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  109. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  110. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  111. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  112. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  113. implementation
  114. uses
  115. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  116. { parameter passing... Still needs extra support from the processor }
  117. { independent code generator }
  118. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  119. var
  120. ref: treference;
  121. begin
  122. case locpara.loc of
  123. LOC_REGISTER,LOC_CREGISTER:
  124. a_load_const_reg(list,size,a,locpara.register);
  125. LOC_REFERENCE:
  126. begin
  127. reference_reset(ref);
  128. ref.base:=locpara.reference.index;
  129. ref.offset:=locpara.reference.offset;
  130. a_load_const_ref(list,size,a,ref);
  131. end;
  132. else
  133. internalerror(2002081101);
  134. end;
  135. if locpara.sp_fixup<>0 then
  136. internalerror(2002081102);
  137. end;
  138. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  139. var
  140. ref: treference;
  141. tmpreg: tregister;
  142. begin
  143. case locpara.loc of
  144. LOC_REGISTER,LOC_CREGISTER:
  145. a_load_ref_reg(list,size,r,locpara.register);
  146. LOC_REFERENCE:
  147. begin
  148. reference_reset(ref);
  149. ref.base:=locpara.reference.index;
  150. ref.offset:=locpara.reference.offset;
  151. tmpreg := get_scratch_reg_int(list,size);
  152. a_load_ref_reg(list,size,r,tmpreg);
  153. a_load_reg_ref(list,size,tmpreg,ref);
  154. free_scratch_reg(list,tmpreg);
  155. end;
  156. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  157. case size of
  158. OS_32:
  159. a_loadfpu_ref_reg(list,OS_F32,r,locpara.register);
  160. OS_64:
  161. a_loadfpu_ref_reg(list,OS_F64,r,locpara.register);
  162. else
  163. internalerror(2002072801);
  164. end;
  165. else
  166. internalerror(2002081103);
  167. end;
  168. if locpara.sp_fixup<>0 then
  169. internalerror(2002081104);
  170. end;
  171. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  172. var
  173. ref: treference;
  174. tmpreg: tregister;
  175. begin
  176. case locpara.loc of
  177. LOC_REGISTER,LOC_CREGISTER:
  178. a_loadaddr_ref_reg(list,r,locpara.register);
  179. LOC_REFERENCE:
  180. begin
  181. reference_reset(ref);
  182. ref.base := locpara.reference.index;
  183. ref.offset := locpara.reference.offset;
  184. tmpreg := get_scratch_reg_address(list);
  185. a_loadaddr_ref_reg(list,r,tmpreg);
  186. a_load_reg_ref(list,OS_ADDR,tmpreg,ref);
  187. free_scratch_reg(list,tmpreg);
  188. end;
  189. else
  190. internalerror(2002080701);
  191. end;
  192. end;
  193. { calling a procedure by name }
  194. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  195. var
  196. href : treference;
  197. begin
  198. {MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  199. if it is a cross-TOC call. If so, it also replaces the NOP
  200. with some restore code.}
  201. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  202. if target_info.system=system_powerpc_macos then
  203. list.concat(taicpu.op_none(A_NOP));
  204. include(current_procinfo.flags,pi_do_call);
  205. end;
  206. { calling a procedure by address }
  207. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  208. var
  209. tmpreg : tregister;
  210. tmpref : treference;
  211. begin
  212. if target_info.system=system_powerpc_macos then
  213. begin
  214. {Generate instruction to load the procedure address from
  215. the transition vector.}
  216. //TODO: Support cross-TOC calls.
  217. tmpreg := get_scratch_reg_int(list,OS_INT);
  218. reference_reset(tmpref);
  219. tmpref.offset := 0;
  220. //tmpref.symaddr := refs_full;
  221. tmpref.base:= reg;
  222. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  223. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  224. free_scratch_reg(list,tmpreg);
  225. end
  226. else
  227. list.concat(taicpu.op_reg(A_MTCTR,reg));
  228. list.concat(taicpu.op_none(A_BCTRL));
  229. //if target_info.system=system_powerpc_macos then
  230. // //NOP is not needed here.
  231. // list.concat(taicpu.op_none(A_NOP));
  232. include(current_procinfo.flags,pi_do_call);
  233. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  234. end;
  235. { calling a procedure by address }
  236. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  237. var
  238. tmpreg : tregister;
  239. tmpref : treference;
  240. begin
  241. tmpreg := get_scratch_reg_int(list,OS_ADDR);
  242. a_load_ref_reg(list,OS_ADDR,ref,tmpreg);
  243. if target_info.system=system_powerpc_macos then
  244. begin
  245. {Generate instruction to load the procedure address from
  246. the transition vector.}
  247. //TODO: Support cross-TOC calls.
  248. reference_reset(tmpref);
  249. tmpref.offset := 0;
  250. //tmpref.symaddr := refs_full;
  251. tmpref.base:= tmpreg;
  252. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  253. end;
  254. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  255. free_scratch_reg(list,tmpreg);
  256. list.concat(taicpu.op_none(A_BCTRL));
  257. //if target_info.system=system_powerpc_macos then
  258. // //NOP is not needed here.
  259. // list.concat(taicpu.op_none(A_NOP));
  260. include(current_procinfo.flags,pi_do_call);
  261. //list.concat(tai_comment.create(strpnew('***** a_call_ref')));
  262. end;
  263. {********************** load instructions ********************}
  264. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  265. begin
  266. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  267. internalerror(2002090902);
  268. if (longint(a) >= low(smallint)) and
  269. (longint(a) <= high(smallint)) then
  270. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  271. else if ((a and $ffff) <> 0) then
  272. begin
  273. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  274. if ((a shr 16) <> 0) or
  275. (smallint(a and $ffff) < 0) then
  276. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  277. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  278. end
  279. else
  280. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  281. end;
  282. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  283. const
  284. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  285. { indexed? updating?}
  286. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  287. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  288. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  289. var
  290. op: TAsmOp;
  291. ref2: TReference;
  292. freereg: boolean;
  293. begin
  294. ref2 := ref;
  295. freereg := fixref(list,ref2);
  296. if size in [OS_S8..OS_S16] then
  297. { storing is the same for signed and unsigned values }
  298. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  299. { 64 bit stuff should be handled separately }
  300. if size in [OS_64,OS_S64] then
  301. internalerror(200109236);
  302. op := storeinstr[tcgsize2unsigned[size],ref2.index.number<>NR_NO,false];
  303. a_load_store(list,op,reg,ref2);
  304. if freereg then
  305. cg.free_scratch_reg(list,ref2.base);
  306. End;
  307. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  308. const
  309. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  310. { indexed? updating?}
  311. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  312. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  313. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  314. { 64bit stuff should be handled separately }
  315. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  316. { there's no load-byte-with-sign-extend :( }
  317. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  318. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  319. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  320. var
  321. op: tasmop;
  322. tmpreg: tregister;
  323. ref2, tmpref: treference;
  324. freereg: boolean;
  325. begin
  326. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  327. internalerror(2002090902);
  328. ref2 := ref;
  329. freereg := fixref(list,ref2);
  330. op := loadinstr[size,ref2.index.number<>NR_NO,false];
  331. a_load_store(list,op,reg,ref2);
  332. if freereg then
  333. free_scratch_reg(list,ref2.base);
  334. { sign extend shortint if necessary, since there is no }
  335. { load instruction that does that automatically (JM) }
  336. if size = OS_S8 then
  337. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  338. end;
  339. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  340. begin
  341. if (reg1.enum<>R_INTREGISTER) or (reg1.number = 0) then
  342. internalerror(200303101);
  343. if (reg2.enum<>R_INTREGISTER) or (reg2.number = 0) then
  344. internalerror(200303102);
  345. if (reg1.number<>reg2.number) or
  346. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  347. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  348. (tosize <> fromsize) and
  349. not(fromsize in [OS_32,OS_S32])) then
  350. begin
  351. case fromsize of
  352. OS_8:
  353. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  354. reg2,reg1,0,31-8+1,31));
  355. OS_S8:
  356. list.concat(taicpu.op_reg_reg(A_EXTSB,reg2,reg1));
  357. OS_16:
  358. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  359. reg2,reg1,0,31-16+1,31));
  360. OS_S16:
  361. list.concat(taicpu.op_reg_reg(A_EXTSH,reg2,reg1));
  362. OS_32,OS_S32:
  363. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  364. else internalerror(2002090901);
  365. end;
  366. end;
  367. end;
  368. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  369. begin
  370. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  371. end;
  372. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  373. const
  374. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  375. { indexed? updating?}
  376. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  377. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  378. var
  379. op: tasmop;
  380. ref2: treference;
  381. freereg: boolean;
  382. begin
  383. { several functions call this procedure with OS_32 or OS_64 }
  384. { so this makes life easier (FK) }
  385. case size of
  386. OS_32,OS_F32:
  387. size:=OS_F32;
  388. OS_64,OS_F64,OS_C64:
  389. size:=OS_F64;
  390. else
  391. internalerror(200201121);
  392. end;
  393. ref2 := ref;
  394. freereg := fixref(list,ref2);
  395. op := fpuloadinstr[size,ref2.index.number <> NR_NO,false];
  396. a_load_store(list,op,reg,ref2);
  397. if freereg then
  398. cg.free_scratch_reg(list,ref2.base);
  399. end;
  400. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  401. const
  402. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  403. { indexed? updating?}
  404. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  405. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  406. var
  407. op: tasmop;
  408. ref2: treference;
  409. freereg: boolean;
  410. begin
  411. if not(size in [OS_F32,OS_F64]) then
  412. internalerror(200201122);
  413. ref2 := ref;
  414. freereg := fixref(list,ref2);
  415. op := fpustoreinstr[size,ref2.index.number <> NR_NO,false];
  416. a_load_store(list,op,reg,ref2);
  417. if freereg then
  418. cg.free_scratch_reg(list,ref2.base);
  419. end;
  420. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  421. var
  422. scratch_register: TRegister;
  423. begin
  424. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  425. end;
  426. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  427. begin
  428. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  429. end;
  430. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  431. size: tcgsize; a: aword; src, dst: tregister);
  432. var
  433. l1,l2: longint;
  434. oplo, ophi: tasmop;
  435. scratchreg: tregister;
  436. useReg, gotrlwi: boolean;
  437. procedure do_lo_hi;
  438. begin
  439. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  440. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  441. end;
  442. begin
  443. if src.enum<>R_INTREGISTER then
  444. internalerror(200303102);
  445. if op = OP_SUB then
  446. begin
  447. {$ifopt q+}
  448. {$q-}
  449. {$define overflowon}
  450. {$endif}
  451. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  452. {$ifdef overflowon}
  453. {$q+}
  454. {$undef overflowon}
  455. {$endif}
  456. exit;
  457. end;
  458. ophi := TOpCG2AsmOpConstHi[op];
  459. oplo := TOpCG2AsmOpConstLo[op];
  460. gotrlwi := get_rlwi_const(a,l1,l2);
  461. if (op in [OP_AND,OP_OR,OP_XOR]) then
  462. begin
  463. if (a = 0) then
  464. begin
  465. if op = OP_AND then
  466. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  467. exit;
  468. end
  469. else if (a = high(aword)) then
  470. begin
  471. case op of
  472. OP_OR:
  473. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  474. OP_XOR:
  475. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  476. end;
  477. exit;
  478. end
  479. else if (a <= high(word)) and
  480. ((op <> OP_AND) or
  481. not gotrlwi) then
  482. begin
  483. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  484. exit;
  485. end;
  486. { all basic constant instructions also have a shifted form that }
  487. { works only on the highest 16bits, so if lo(a) is 0, we can }
  488. { use that one }
  489. if (word(a) = 0) and
  490. (not(op = OP_AND) or
  491. not gotrlwi) then
  492. begin
  493. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  494. exit;
  495. end;
  496. end
  497. else if (op = OP_ADD) then
  498. if a = 0 then
  499. exit
  500. else if (longint(a) >= low(smallint)) and
  501. (longint(a) <= high(smallint)) then
  502. begin
  503. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  504. exit;
  505. end;
  506. { otherwise, the instructions we can generate depend on the }
  507. { operation }
  508. useReg := false;
  509. case op of
  510. OP_DIV,OP_IDIV:
  511. if (a = 0) then
  512. internalerror(200208103)
  513. else if (a = 1) then
  514. begin
  515. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  516. exit
  517. end
  518. else if ispowerof2(a,l1) then
  519. begin
  520. case op of
  521. OP_DIV:
  522. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  523. OP_IDIV:
  524. begin
  525. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  526. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  527. end;
  528. end;
  529. exit;
  530. end
  531. else
  532. usereg := true;
  533. OP_IMUL, OP_MUL:
  534. if (a = 0) then
  535. begin
  536. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  537. exit
  538. end
  539. else if (a = 1) then
  540. begin
  541. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  542. exit
  543. end
  544. else if ispowerof2(a,l1) then
  545. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  546. else if (longint(a) >= low(smallint)) and
  547. (longint(a) <= high(smallint)) then
  548. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  549. else
  550. usereg := true;
  551. OP_ADD:
  552. begin
  553. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  554. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  555. smallint((a shr 16) + ord(smallint(a) < 0))));
  556. end;
  557. OP_OR:
  558. { try to use rlwimi }
  559. if gotrlwi and
  560. (src.number = dst.number) then
  561. begin
  562. scratchreg := get_scratch_reg_int(list,OS_INT);
  563. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  564. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  565. scratchreg,0,l1,l2));
  566. free_scratch_reg(list,scratchreg);
  567. end
  568. else
  569. do_lo_hi;
  570. OP_AND:
  571. { try to use rlwinm }
  572. if gotrlwi then
  573. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  574. src,0,l1,l2))
  575. else
  576. useReg := true;
  577. OP_XOR:
  578. do_lo_hi;
  579. OP_SHL,OP_SHR,OP_SAR:
  580. begin
  581. if (a and 31) <> 0 Then
  582. list.concat(taicpu.op_reg_reg_const(
  583. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  584. if (a shr 5) <> 0 then
  585. internalError(68991);
  586. end
  587. else
  588. internalerror(200109091);
  589. end;
  590. { if all else failed, load the constant in a register and then }
  591. { perform the operation }
  592. if useReg then
  593. begin
  594. scratchreg := get_scratch_reg_int(list,OS_INT);
  595. a_load_const_reg(list,OS_32,a,scratchreg);
  596. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  597. free_scratch_reg(list,scratchreg);
  598. end;
  599. end;
  600. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  601. size: tcgsize; src1, src2, dst: tregister);
  602. const
  603. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  604. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  605. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  606. begin
  607. case op of
  608. OP_NEG,OP_NOT:
  609. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  610. else
  611. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  612. end;
  613. end;
  614. {*************** compare instructructions ****************}
  615. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  616. l : tasmlabel);
  617. var
  618. p: taicpu;
  619. scratch_register: TRegister;
  620. signed: boolean;
  621. r:Tregister;
  622. begin
  623. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  624. { in the following case, we generate more efficient code when }
  625. { signed is true }
  626. if (cmp_op in [OC_EQ,OC_NE]) and
  627. (a > $ffff) then
  628. signed := true;
  629. r.enum:=R_CR0;
  630. if signed then
  631. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  632. list.concat(taicpu.op_reg_reg_const(A_CMPWI,r,reg,longint(a)))
  633. else
  634. begin
  635. scratch_register := get_scratch_reg_int(list,OS_INT);
  636. a_load_const_reg(list,OS_32,a,scratch_register);
  637. list.concat(taicpu.op_reg_reg_reg(A_CMPW,r,reg,scratch_register));
  638. free_scratch_reg(list,scratch_register);
  639. end
  640. else
  641. if (a <= $ffff) then
  642. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,r,reg,a))
  643. else
  644. begin
  645. scratch_register := get_scratch_reg_int(list,OS_32);
  646. a_load_const_reg(list,OS_32,a,scratch_register);
  647. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,r,reg,scratch_register));
  648. free_scratch_reg(list,scratch_register);
  649. end;
  650. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  651. end;
  652. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  653. reg1,reg2 : tregister;l : tasmlabel);
  654. var
  655. p: taicpu;
  656. op: tasmop;
  657. r:Tregister;
  658. begin
  659. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  660. op := A_CMPW
  661. else op := A_CMPLW;
  662. r.enum:=R_CR0;
  663. list.concat(taicpu.op_reg_reg_reg(op,r,reg2,reg1));
  664. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  665. end;
  666. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  667. begin
  668. {$warning FIX ME}
  669. end;
  670. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  671. begin
  672. {$warning FIX ME}
  673. end;
  674. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  675. begin
  676. {$warning FIX ME}
  677. end;
  678. procedure tcgppc.g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);
  679. begin
  680. {$warning FIX ME}
  681. end;
  682. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  683. begin
  684. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  685. end;
  686. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  687. begin
  688. a_jmp(list,A_B,C_None,0,l);
  689. end;
  690. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  691. var
  692. c: tasmcond;
  693. r:Tregister;
  694. begin
  695. c := flags_to_cond(f);
  696. r.enum:=R_CR0;
  697. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(r.enum),l);
  698. end;
  699. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  700. var
  701. testbit: byte;
  702. bitvalue: boolean;
  703. begin
  704. { get the bit to extract from the conditional register + its }
  705. { requested value (0 or 1) }
  706. testbit := ((ord(f.cr)-ord(R_CR0)) * 4);
  707. case f.flag of
  708. F_EQ,F_NE:
  709. begin
  710. inc(testbit,2);
  711. bitvalue := f.flag = F_EQ;
  712. end;
  713. F_LT,F_GE:
  714. begin
  715. bitvalue := f.flag = F_LT;
  716. end;
  717. F_GT,F_LE:
  718. begin
  719. inc(testbit);
  720. bitvalue := f.flag = F_GT;
  721. end;
  722. else
  723. internalerror(200112261);
  724. end;
  725. { load the conditional register in the destination reg }
  726. list.concat(taicpu.op_reg(A_MFCR,reg));
  727. { we will move the bit that has to be tested to bit 0 by rotating }
  728. { left }
  729. testbit := (testbit + 1) and 31;
  730. { extract bit }
  731. list.concat(taicpu.op_reg_reg_const_const_const(
  732. A_RLWINM,reg,reg,testbit,31,31));
  733. { if we need the inverse, xor with 1 }
  734. if not bitvalue then
  735. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  736. end;
  737. (*
  738. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  739. var
  740. testbit: byte;
  741. bitvalue: boolean;
  742. begin
  743. { get the bit to extract from the conditional register + its }
  744. { requested value (0 or 1) }
  745. case f.simple of
  746. false:
  747. begin
  748. { we don't generate this in the compiler }
  749. internalerror(200109062);
  750. end;
  751. true:
  752. case f.cond of
  753. C_None:
  754. internalerror(200109063);
  755. C_LT..C_NU:
  756. begin
  757. testbit := (ord(f.cr) - ord(R_CR0))*4;
  758. inc(testbit,AsmCondFlag2BI[f.cond]);
  759. bitvalue := AsmCondFlagTF[f.cond];
  760. end;
  761. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  762. begin
  763. testbit := f.crbit
  764. bitvalue := AsmCondFlagTF[f.cond];
  765. end;
  766. else
  767. internalerror(200109064);
  768. end;
  769. end;
  770. { load the conditional register in the destination reg }
  771. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  772. { we will move the bit that has to be tested to bit 31 -> rotate }
  773. { left by bitpos+1 (remember, this is big-endian!) }
  774. if bitpos <> 31 then
  775. inc(bitpos)
  776. else
  777. bitpos := 0;
  778. { extract bit }
  779. list.concat(taicpu.op_reg_reg_const_const_const(
  780. A_RLWINM,reg,reg,bitpos,31,31));
  781. { if we need the inverse, xor with 1 }
  782. if not bitvalue then
  783. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  784. end;
  785. *)
  786. { *********** entry/exit code and address loading ************ }
  787. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  788. begin
  789. case target_info.system of
  790. system_powerpc_macos:
  791. g_stackframe_entry_mac(list,localsize);
  792. system_powerpc_linux:
  793. g_stackframe_entry_sysv(list,localsize)
  794. else
  795. internalerror(2204001);
  796. end;
  797. end;
  798. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  799. begin
  800. case target_info.system of
  801. system_powerpc_macos:
  802. g_return_from_proc_mac(list,parasize);
  803. system_powerpc_linux:
  804. g_return_from_proc_sysv(list,parasize)
  805. else
  806. internalerror(2204001);
  807. end;
  808. end;
  809. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  810. { generated the entry code of a procedure/function. Note: localsize is the }
  811. { sum of the size necessary for local variables and the maximum possible }
  812. { combined size of ALL the parameters of a procedure called by the current }
  813. { one }
  814. var regcounter,firstregfpu,firstreggpr: TRegister;
  815. href : treference;
  816. usesfpr,usesgpr,gotgot : boolean;
  817. parastart : aword;
  818. offset : aword;
  819. r,r2,rsp:Tregister;
  820. regcounter2: Tsuperregister;
  821. begin
  822. { we do our own localsize calculation }
  823. localsize:=0;
  824. { CR and LR only have to be saved in case they are modified by the current }
  825. { procedure, but currently this isn't checked, so save them always }
  826. { following is the entry code as described in "Altivec Programming }
  827. { Interface Manual", bar the saving of AltiVec registers }
  828. rsp.enum:=R_INTREGISTER;
  829. rsp.number:=NR_STACK_POINTER_REG;
  830. a_reg_alloc(list,rsp);
  831. r.enum:=R_INTREGISTER;
  832. r.number:=NR_R0;
  833. a_reg_alloc(list,r);
  834. if current_procdef.parast.symtablelevel>1 then
  835. begin
  836. r.enum:=R_INTREGISTER;
  837. r.number:=NR_R11;
  838. a_reg_alloc(list,r);
  839. end;
  840. { allocate registers containing reg parameters }
  841. r.enum := R_INTREGISTER;
  842. for regcounter2 := RS_R3 to RS_R10 do
  843. begin
  844. r.number:=regcounter2 shl 8;
  845. a_reg_alloc(list,r);
  846. end;
  847. usesfpr:=false;
  848. if not (po_assembler in current_procdef.procoptions) then
  849. for regcounter.enum:=R_F14 to R_F31 do
  850. if regcounter.enum in rg.usedbyproc then
  851. begin
  852. usesfpr:= true;
  853. firstregfpu:=regcounter;
  854. break;
  855. end;
  856. usesgpr:=false;
  857. if not (po_assembler in current_procdef.procoptions) then
  858. for regcounter2:=RS_R14 to RS_R31 do
  859. begin
  860. if regcounter2 in rg.usedintbyproc then
  861. begin
  862. usesgpr:=true;
  863. firstreggpr.enum := R_INTREGISTER;
  864. firstreggpr.number := regcounter2 shl 8;
  865. break;
  866. end;
  867. end;
  868. { save link register? }
  869. if not (po_assembler in current_procdef.procoptions) then
  870. if (pi_do_call in current_procinfo.flags) then
  871. begin
  872. { save return address... }
  873. r.enum:=R_INTREGISTER;
  874. r.number:=NR_R0;
  875. list.concat(taicpu.op_reg(A_MFLR,r));
  876. { ... in caller's rframe }
  877. reference_reset_base(href,rsp,4);
  878. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  879. a_reg_dealloc(list,r);
  880. end;
  881. if usesfpr or usesgpr then
  882. begin
  883. r.enum:=R_INTREGISTER;
  884. r.number:=NR_R12;
  885. a_reg_alloc(list,r);
  886. { save end of fpr save area }
  887. list.concat(taicpu.op_reg_reg(A_MR,r,rsp));
  888. end;
  889. { calculate the size of the locals }
  890. if usesgpr then
  891. inc(localsize,((NR_R31-firstreggpr.number) shr 8+1)*4);
  892. if usesfpr then
  893. inc(localsize,(ord(R_F31)-ord(firstregfpu.enum)+1)*8);
  894. { align to 16 bytes }
  895. localsize:=align(localsize,16);
  896. inc(localsize,tg.lasttemp);
  897. localsize:=align(localsize,16);
  898. tppcprocinfo(current_procinfo).localsize:=localsize;
  899. if (localsize <> 0) then
  900. begin
  901. r.enum:=R_INTREGISTER;
  902. r.number:=NR_STACK_POINTER_REG;
  903. reference_reset_base(href,r,-localsize);
  904. a_load_store(list,A_STWU,r,href);
  905. end;
  906. { no GOT pointer loaded yet }
  907. gotgot:=false;
  908. if usesfpr then
  909. begin
  910. { save floating-point registers
  911. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  912. begin
  913. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  914. gotgot:=true;
  915. end
  916. else
  917. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  918. }
  919. for regcounter.enum:=firstregfpu.enum to R_F31 do
  920. if regcounter.enum in rg.usedbyproc then
  921. begin
  922. { reference_reset_base(href,R_1,-localsize);
  923. a_load_store(list,A_STWU,R_1,href);
  924. }
  925. end;
  926. { compute end of gpr save area }
  927. r.enum:=R_INTREGISTER;
  928. r.number:=NR_R12;
  929. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,-(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  930. end;
  931. { save gprs and fetch GOT pointer }
  932. if usesgpr then
  933. begin
  934. {
  935. if cs_create_pic in aktmoduleswitches then
  936. begin
  937. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  938. gotgot:=true;
  939. end
  940. else
  941. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  942. }
  943. r.enum:=R_INTREGISTER;
  944. r.number:=NR_R12;
  945. reference_reset_base(href,r,-((NR_R31-firstreggpr.number) shr 8+1)*4);
  946. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  947. end;
  948. r.enum:=R_INTREGISTER;
  949. r.number:=NR_R12;
  950. if usesfpr or usesgpr then
  951. a_reg_dealloc(list,r);
  952. { PIC code support, }
  953. if cs_create_pic in aktmoduleswitches then
  954. begin
  955. { if we didn't get the GOT pointer till now, we've to calculate it now }
  956. if not(gotgot) then
  957. begin
  958. {!!!!!!!!!!!!!}
  959. end;
  960. r.enum:=R_INTREGISTER;
  961. r.number:=NR_R31;
  962. r2.enum:=R_LR;
  963. a_reg_alloc(list,r);
  964. { place GOT ptr in r31 }
  965. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  966. end;
  967. { save the CR if necessary ( !!! always done currently ) }
  968. { still need to find out where this has to be done for SystemV
  969. a_reg_alloc(list,R_0);
  970. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  971. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  972. new_reference(STACK_POINTER_REG,LA_CR)));
  973. a_reg_dealloc(list,R_0); }
  974. { now comes the AltiVec context save, not yet implemented !!! }
  975. { if we're in a nested procedure, we've to save R11 }
  976. if current_procdef.parast.symtablelevel>2 then
  977. begin
  978. r.enum:=R_INTREGISTER;
  979. r.number:=NR_R11;
  980. reference_reset_base(href,rsp,current_procinfo.framepointer_offset);
  981. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  982. end;
  983. end;
  984. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  985. var
  986. regcounter,firstregfpu,firstreggpr: TRegister;
  987. href : treference;
  988. usesfpr,usesgpr,genret : boolean;
  989. r,r2:Tregister;
  990. regcounter2:Tsuperregister;
  991. begin
  992. { release parameter registers }
  993. r.enum := R_INTREGISTER;
  994. for regcounter2 := RS_R3 to RS_R10 do
  995. begin
  996. r.number:=regcounter2 shl 8;
  997. a_reg_dealloc(list,r);
  998. end;
  999. { AltiVec context restore, not yet implemented !!! }
  1000. usesfpr:=false;
  1001. if not (po_assembler in current_procdef.procoptions) then
  1002. for regcounter.enum:=R_F14 to R_F31 do
  1003. if regcounter.enum in rg.usedbyproc then
  1004. begin
  1005. usesfpr:=true;
  1006. firstregfpu:=regcounter;
  1007. break;
  1008. end;
  1009. usesgpr:=false;
  1010. if not (po_assembler in current_procdef.procoptions) then
  1011. for regcounter2:=RS_R14 to RS_R30 do
  1012. begin
  1013. if regcounter2 in rg.usedintbyproc then
  1014. begin
  1015. usesgpr:=true;
  1016. firstreggpr.enum:=R_INTREGISTER;
  1017. firstreggpr.number:=regcounter2 shl 8;
  1018. break;
  1019. end;
  1020. end;
  1021. { no return (blr) generated yet }
  1022. genret:=true;
  1023. if usesgpr then
  1024. begin
  1025. { address of gpr save area to r11 }
  1026. r.enum:=R_INTREGISTER;
  1027. r.number:=NR_STACK_POINTER_REG;
  1028. r2.enum:=R_INTREGISTER;
  1029. r2.number:=NR_R12;
  1030. if usesfpr then
  1031. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tppcprocinfo(current_procinfo).localsize-(ord(R_F31)-ord(firstregfpu.enum)+1)*8,r,r2)
  1032. else
  1033. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tppcprocinfo(current_procinfo).localsize,r,r2);
  1034. { restore gprs }
  1035. { at least for now we use LMW }
  1036. {
  1037. a_call_name(objectlibrary.newasmsymbol('_restgpr_14');
  1038. }
  1039. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr.number)) shr 8+1)*4);
  1040. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1041. end;
  1042. { restore fprs and return }
  1043. if usesfpr then
  1044. begin
  1045. { address of fpr save area to r11 }
  1046. r.enum:=R_INTREGISTER;
  1047. r.number:=NR_R12;
  1048. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1049. {
  1050. if (pi_do_call in current_procinfo.flags) then
  1051. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1052. '_x')
  1053. else
  1054. { leaf node => lr haven't to be restored }
  1055. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1056. '_l');
  1057. genret:=false;
  1058. }
  1059. end;
  1060. { if we didn't generate the return code, we've to do it now }
  1061. if genret then
  1062. begin
  1063. { adjust r1 }
  1064. r.enum:=R_INTREGISTER;
  1065. r.number:=NR_R1;
  1066. a_op_const_reg(list,OP_ADD,tppcprocinfo(current_procinfo).localsize,r);
  1067. { load link register? }
  1068. if not (po_assembler in current_procdef.procoptions) then
  1069. if (pi_do_call in current_procinfo.flags) then
  1070. begin
  1071. r.enum:=R_INTREGISTER;
  1072. r.number:=NR_STACK_POINTER_REG;
  1073. reference_reset_base(href,r,4);
  1074. r.enum:=R_INTREGISTER;
  1075. r.number:=NR_R0;
  1076. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1077. list.concat(taicpu.op_reg(A_MTLR,r));
  1078. end;
  1079. list.concat(taicpu.op_none(A_BLR));
  1080. end;
  1081. end;
  1082. function save_regs(list : taasmoutput):longint;
  1083. {Generates code which saves used non-volatile registers in
  1084. the save area right below the address the stackpointer point to.
  1085. Returns the actual used save area size.}
  1086. var regcounter,firstregfpu,firstreggpr: TRegister;
  1087. usesfpr,usesgpr: boolean;
  1088. href : treference;
  1089. offset: integer;
  1090. r,r2:Tregister;
  1091. regcounter2: Tsuperregister;
  1092. begin
  1093. usesfpr:=false;
  1094. if not (po_assembler in current_procdef.procoptions) then
  1095. for regcounter.enum:=R_F14 to R_F31 do
  1096. if regcounter.enum in rg.usedbyproc then
  1097. begin
  1098. usesfpr:=true;
  1099. firstregfpu:=regcounter;
  1100. break;
  1101. end;
  1102. usesgpr:=false;
  1103. if not (po_assembler in current_procdef.procoptions) then
  1104. for regcounter2:=RS_R13 to RS_R31 do
  1105. begin
  1106. if regcounter2 in rg.usedintbyproc then
  1107. begin
  1108. usesgpr:=true;
  1109. firstreggpr.enum:=R_INTREGISTER;
  1110. firstreggpr.number:=regcounter2 shl 8;
  1111. break;
  1112. end;
  1113. end;
  1114. offset:= 0;
  1115. { save floating-point registers }
  1116. if usesfpr then
  1117. for regcounter.enum := firstregfpu.enum to R_F31 do
  1118. begin
  1119. offset:= offset - 8;
  1120. r.enum:=R_INTREGISTER;
  1121. r.number:=NR_STACK_POINTER_REG;
  1122. reference_reset_base(href, r, offset);
  1123. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1124. end;
  1125. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1126. { save gprs in gpr save area }
  1127. if usesgpr then
  1128. if firstreggpr.enum < R_30 then
  1129. begin
  1130. offset:= offset - 4 * (ord(R_31) - ord(firstreggpr.enum) + 1);
  1131. r.enum:=R_INTREGISTER;
  1132. r.number:=NR_STACK_POINTER_REG;
  1133. reference_reset_base(href,r,offset);
  1134. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1135. {STMW stores multiple registers}
  1136. end
  1137. else
  1138. begin
  1139. r.enum:=R_INTREGISTER;
  1140. r.number:=NR_STACK_POINTER_REG;
  1141. r2 := firstreggpr;
  1142. convert_register_to_enum(firstreggpr);
  1143. for regcounter.enum := firstreggpr.enum to R_31 do
  1144. begin
  1145. offset:= offset - 4;
  1146. reference_reset_base(href, r, offset);
  1147. list.concat(taicpu.op_reg_ref(A_STW, r2, href));
  1148. inc(r2.number,NR_R1-NR_R0);
  1149. end;
  1150. end;
  1151. { now comes the AltiVec context save, not yet implemented !!! }
  1152. save_regs:= -offset;
  1153. end;
  1154. procedure restore_regs(list : taasmoutput);
  1155. {Generates code which restores used non-volatile registers from
  1156. the save area right below the address the stackpointer point to.}
  1157. var regcounter,firstregfpu,firstreggpr: TRegister;
  1158. usesfpr,usesgpr: boolean;
  1159. href : treference;
  1160. offset: integer;
  1161. r,r2:Tregister;
  1162. regcounter2: Tsuperregister;
  1163. begin
  1164. usesfpr:=false;
  1165. if not (po_assembler in current_procdef.procoptions) then
  1166. for regcounter.enum:=R_F14 to R_F31 do
  1167. if regcounter.enum in rg.usedbyproc then
  1168. begin
  1169. usesfpr:=true;
  1170. firstregfpu:=regcounter;
  1171. break;
  1172. end;
  1173. usesgpr:=false;
  1174. if not (po_assembler in current_procdef.procoptions) then
  1175. for regcounter2:=RS_R13 to RS_R31 do
  1176. begin
  1177. if regcounter2 in rg.usedintbyproc then
  1178. begin
  1179. usesgpr:=true;
  1180. firstreggpr.enum:=R_INTREGISTER;
  1181. firstreggpr.number:=regcounter2 shl 8;
  1182. break;
  1183. end;
  1184. inc(r.number,NR_R1-NR_R0);
  1185. end;
  1186. offset:= 0;
  1187. { restore fp registers }
  1188. if usesfpr then
  1189. for regcounter.enum := firstregfpu.enum to R_F31 do
  1190. begin
  1191. offset:= offset - 8;
  1192. r.enum:=R_INTREGISTER;
  1193. r.number:=NR_STACK_POINTER_REG;
  1194. reference_reset_base(href, r, offset);
  1195. list.concat(taicpu.op_reg_ref(A_LFD, regcounter, href));
  1196. end;
  1197. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1198. { restore gprs }
  1199. if usesgpr then
  1200. if firstreggpr.enum < R_30 then
  1201. begin
  1202. offset:= offset - 4 * (ord(R_31) - ord(firstreggpr.enum) + 1);
  1203. r.enum:=R_INTREGISTER;
  1204. r.number:=NR_STACK_POINTER_REG;
  1205. reference_reset_base(href,r,offset); //-220
  1206. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1207. {LMW loads multiple registers}
  1208. end
  1209. else
  1210. begin
  1211. r.enum:=R_INTREGISTER;
  1212. r.number:=NR_STACK_POINTER_REG;
  1213. r2 := firstreggpr;
  1214. convert_register_to_enum(firstreggpr);
  1215. for regcounter.enum := firstreggpr.enum to R_31 do
  1216. begin
  1217. offset:= offset - 4;
  1218. reference_reset_base(href, r, offset);
  1219. list.concat(taicpu.op_reg_ref(A_LWZ, r2, href));
  1220. inc(r2.number,NR_R1-NR_R0);
  1221. end;
  1222. end;
  1223. { now comes the AltiVec context restore, not yet implemented !!! }
  1224. end;
  1225. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1226. { generated the entry code of a procedure/function. Note: localsize is the }
  1227. { sum of the size necessary for local variables and the maximum possible }
  1228. { combined size of ALL the parameters of a procedure called by the current }
  1229. { one }
  1230. const
  1231. macosLinkageAreaSize = 24;
  1232. var regcounter: TRegister;
  1233. href : treference;
  1234. registerSaveAreaSize : longint;
  1235. r,r2,rsp:Tregister;
  1236. regcounter2: Tsuperregister;
  1237. begin
  1238. if (localsize mod 8) <> 0 then internalerror(58991);
  1239. { CR and LR only have to be saved in case they are modified by the current }
  1240. { procedure, but currently this isn't checked, so save them always }
  1241. { following is the entry code as described in "Altivec Programming }
  1242. { Interface Manual", bar the saving of AltiVec registers }
  1243. r.enum:=R_INTREGISTER;
  1244. r.number:=NR_R0;
  1245. rsp.enum:=R_INTREGISTER;
  1246. rsp.number:=NR_STACK_POINTER_REG;
  1247. a_reg_alloc(list,rsp);
  1248. a_reg_alloc(list,r);
  1249. { allocate registers containing reg parameters }
  1250. r.enum := R_INTREGISTER;
  1251. for regcounter2 := RS_R3 to RS_R10 do
  1252. begin
  1253. r.number:=regcounter2 shl 8;
  1254. a_reg_alloc(list,r);
  1255. end;
  1256. {TODO: Allocate fp and altivec parameter registers also}
  1257. { save return address in callers frame}
  1258. r2.enum:=R_LR;
  1259. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1260. { ... in caller's frame }
  1261. reference_reset_base(href,rsp,8);
  1262. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1263. a_reg_dealloc(list,r);
  1264. { save non-volatile registers in callers frame}
  1265. registerSaveAreaSize:= save_regs(list);
  1266. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1267. a_reg_alloc(list,r);
  1268. r2.enum:=R_CR;
  1269. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1270. reference_reset_base(href,rsp,LA_CR);
  1271. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1272. a_reg_dealloc(list,r);
  1273. (*
  1274. { save pointer to incoming arguments }
  1275. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1276. *)
  1277. (*
  1278. a_reg_alloc(list,R_12);
  1279. { 0 or 8 based on SP alignment }
  1280. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1281. R_12,STACK_POINTER_REG,0,28,28));
  1282. { add in stack length }
  1283. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1284. -localsize));
  1285. { establish new alignment }
  1286. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1287. a_reg_dealloc(list,R_12);
  1288. *)
  1289. { allocate stack frame }
  1290. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1291. inc(localsize,tg.lasttemp);
  1292. localsize:=align(localsize,16);
  1293. tppcprocinfo(current_procinfo).localsize:=localsize;
  1294. if (localsize <> 0) then
  1295. begin
  1296. r.enum:=R_INTREGISTER;
  1297. r.number:=NR_STACK_POINTER_REG;
  1298. reference_reset_base(href,r,-localsize);
  1299. a_load_store(list,A_STWU,r,href);
  1300. { this also stores the old stack pointer in the new stack frame }
  1301. end;
  1302. end;
  1303. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1304. var
  1305. regcounter: TRegister;
  1306. href : treference;
  1307. r,r2,rsp:Tregister;
  1308. regcounter2: Tsuperregister;
  1309. begin
  1310. { release parameter registers }
  1311. r.enum := R_INTREGISTER;
  1312. for regcounter2 := RS_R3 to RS_R10 do
  1313. begin
  1314. r.number := regcounter2 shl 8;
  1315. a_reg_dealloc(list,r);
  1316. end;
  1317. {TODO: Release fp and altivec parameter registers also}
  1318. r.enum:=R_INTREGISTER;
  1319. r.number:=NR_R0;
  1320. rsp.enum:=R_INTREGISTER;
  1321. rsp.number:=NR_STACK_POINTER_REG;
  1322. a_reg_alloc(list,r);
  1323. { restore stack pointer }
  1324. reference_reset_base(href,rsp,LA_SP);
  1325. list.concat(taicpu.op_reg_ref(A_LWZ,rsp,href));
  1326. (*
  1327. list.concat(taicpu.op_reg_reg_const(A_ORI,rsp,R_31,0));
  1328. *)
  1329. { restore the CR if necessary from callers frame
  1330. ( !!! always done currently ) }
  1331. reference_reset_base(href,rsp,LA_CR);
  1332. r.enum:=R_INTREGISTER;
  1333. r.number:=NR_R0;
  1334. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1335. r2.enum:=R_CR;
  1336. list.concat(taicpu.op_reg_reg(A_MTSPR,r,r2));
  1337. a_reg_dealloc(list,r);
  1338. (*
  1339. { restore return address from callers frame }
  1340. reference_reset_base(href,STACK_POINTER_REG,8);
  1341. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1342. *)
  1343. { restore non-volatile registers from callers frame }
  1344. restore_regs(list);
  1345. (*
  1346. { return to caller }
  1347. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1348. list.concat(taicpu.op_none(A_BLR));
  1349. *)
  1350. { restore return address from callers frame }
  1351. r.enum:=R_INTREGISTER;
  1352. r.number:=NR_R0;
  1353. r2.enum:=R_LR;
  1354. reference_reset_base(href,rsp,8);
  1355. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1356. { return to caller }
  1357. list.concat(taicpu.op_reg_reg(A_MTSPR,r,r2));
  1358. list.concat(taicpu.op_none(A_BLR));
  1359. end;
  1360. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1361. begin
  1362. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1363. end;
  1364. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1365. var
  1366. ref2, tmpref: treference;
  1367. freereg: boolean;
  1368. r2,tmpreg:Tregister;
  1369. begin
  1370. ref2 := ref;
  1371. freereg := fixref(list,ref2);
  1372. if assigned(ref2.symbol) then
  1373. begin
  1374. if target_info.system = system_powerpc_macos then
  1375. begin
  1376. if ref2.base.number <> NR_NO then
  1377. internalerror(2002103102); //TODO: Implement this if needed
  1378. if macos_direct_globals then
  1379. begin
  1380. reference_reset(tmpref);
  1381. tmpref.offset := ref2.offset;
  1382. tmpref.symbol := ref2.symbol;
  1383. tmpref.symaddr := refs_full;
  1384. tmpref.base.number := NR_NO;
  1385. r2.enum:=R_INTREGISTER;
  1386. r2.number:=NR_RTOC;
  1387. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r2,tmpref));
  1388. end
  1389. else
  1390. begin
  1391. reference_reset(tmpref);
  1392. tmpref.symbol := ref2.symbol;
  1393. tmpref.offset := 0; //ref2.offset;
  1394. tmpref.symaddr := refs_full;
  1395. tmpref.base.enum := R_INTREGISTER;
  1396. tmpref.base.number := NR_RTOC;
  1397. if ref2.offset = 0 then
  1398. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref))
  1399. else
  1400. begin
  1401. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1402. reference_reset(tmpref);
  1403. tmpref.offset := ref2.offset;
  1404. tmpref.symaddr := refs_full;
  1405. tmpref.base:= r;
  1406. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1407. (*
  1408. tmpreg := get_scratch_reg_address(list);
  1409. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1410. reference_reset(tmpref);
  1411. tmpref.offset := ref2.offset;
  1412. tmpref.symaddr := refs_full;
  1413. tmpref.base:= tmpreg;
  1414. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1415. free_scratch_reg(list,tmpreg);
  1416. *)
  1417. end;
  1418. end;
  1419. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1420. end
  1421. else
  1422. begin
  1423. { add the symbol's value to the base of the reference, and if the }
  1424. { reference doesn't have a base, create one }
  1425. reference_reset(tmpref);
  1426. tmpref.offset := ref2.offset;
  1427. tmpref.symbol := ref2.symbol;
  1428. tmpref.symaddr := refs_ha;
  1429. if ref2.base .number<> NR_NO then
  1430. begin
  1431. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1432. ref2.base,tmpref));
  1433. if freereg then
  1434. begin
  1435. cg.free_scratch_reg(list,ref2.base);
  1436. freereg := false;
  1437. end;
  1438. end
  1439. else
  1440. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1441. tmpref.base.number := NR_NO;
  1442. tmpref.symaddr := refs_l;
  1443. { can be folded with one of the next instructions by the }
  1444. { optimizer probably }
  1445. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1446. end
  1447. end
  1448. else if ref2.offset <> 0 Then
  1449. if ref2.base.number <> NR_NO then
  1450. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1451. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1452. { occurs, so now only ref.offset has to be loaded }
  1453. else
  1454. a_load_const_reg(list,OS_32,ref2.offset,r)
  1455. else if ref.index.number <> NR_NO Then
  1456. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1457. else if (ref2.base.number <> NR_NO) and
  1458. (r.number <> ref2.base.number) then
  1459. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1460. if freereg then
  1461. cg.free_scratch_reg(list,ref2.base);
  1462. end;
  1463. { ************* concatcopy ************ }
  1464. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1465. var
  1466. countreg: TRegister;
  1467. src, dst: TReference;
  1468. lab: tasmlabel;
  1469. count, count2: aword;
  1470. orgsrc, orgdst: boolean;
  1471. r:Tregister;
  1472. begin
  1473. {$ifdef extdebug}
  1474. if len > high(longint) then
  1475. internalerror(2002072704);
  1476. {$endif extdebug}
  1477. { make sure short loads are handled as optimally as possible }
  1478. if not loadref then
  1479. if (len <= 8) and
  1480. (byte(len) in [1,2,4,8]) then
  1481. begin
  1482. if len < 8 then
  1483. begin
  1484. a_load_ref_ref(list,int_cgsize(len),source,dest);
  1485. if delsource then
  1486. reference_release(list,source);
  1487. end
  1488. else
  1489. begin
  1490. r.enum:=R_F0;
  1491. a_reg_alloc(list,r);
  1492. a_loadfpu_ref_reg(list,OS_F64,source,r);
  1493. if delsource then
  1494. reference_release(list,source);
  1495. a_loadfpu_reg_ref(list,OS_F64,r,dest);
  1496. a_reg_dealloc(list,r);
  1497. end;
  1498. exit;
  1499. end;
  1500. reference_reset(src);
  1501. reference_reset(dst);
  1502. { load the address of source into src.base }
  1503. if loadref then
  1504. begin
  1505. src.base := get_scratch_reg_address(list);
  1506. a_load_ref_reg(list,OS_32,source,src.base);
  1507. orgsrc := false;
  1508. end
  1509. else if not issimpleref(source) or
  1510. ((source.index.number <> NR_NO) and
  1511. ((source.offset + longint(len)) > high(smallint))) then
  1512. begin
  1513. src.base := get_scratch_reg_address(list);
  1514. a_loadaddr_ref_reg(list,source,src.base);
  1515. orgsrc := false;
  1516. end
  1517. else
  1518. begin
  1519. src := source;
  1520. orgsrc := true;
  1521. end;
  1522. if not orgsrc and delsource then
  1523. reference_release(list,source);
  1524. { load the address of dest into dst.base }
  1525. if not issimpleref(dest) or
  1526. ((dest.index.number <> NR_NO) and
  1527. ((dest.offset + longint(len)) > high(smallint))) then
  1528. begin
  1529. dst.base := get_scratch_reg_address(list);
  1530. a_loadaddr_ref_reg(list,dest,dst.base);
  1531. orgdst := false;
  1532. end
  1533. else
  1534. begin
  1535. dst := dest;
  1536. orgdst := true;
  1537. end;
  1538. count := len div 8;
  1539. if count > 4 then
  1540. { generate a loop }
  1541. begin
  1542. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1543. { have to be set to 8. I put an Inc there so debugging may be }
  1544. { easier (should offset be different from zero here, it will be }
  1545. { easy to notice in the generated assembler }
  1546. inc(dst.offset,8);
  1547. inc(src.offset,8);
  1548. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1549. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1550. countreg := get_scratch_reg_int(list,OS_INT);
  1551. a_load_const_reg(list,OS_32,count,countreg);
  1552. { explicitely allocate R_0 since it can be used safely here }
  1553. { (for holding date that's being copied) }
  1554. r.enum:=R_F0;
  1555. a_reg_alloc(list,r);
  1556. objectlibrary.getlabel(lab);
  1557. a_label(list, lab);
  1558. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1559. r.enum:=R_F0;
  1560. list.concat(taicpu.op_reg_ref(A_LFDU,r,src));
  1561. list.concat(taicpu.op_reg_ref(A_STFDU,r,dst));
  1562. a_jmp(list,A_BC,C_NE,0,lab);
  1563. free_scratch_reg(list,countreg);
  1564. a_reg_dealloc(list,r);
  1565. len := len mod 8;
  1566. end;
  1567. count := len div 8;
  1568. if count > 0 then
  1569. { unrolled loop }
  1570. begin
  1571. r.enum:=R_F0;
  1572. a_reg_alloc(list,r);
  1573. for count2 := 1 to count do
  1574. begin
  1575. a_loadfpu_ref_reg(list,OS_F64,src,r);
  1576. a_loadfpu_reg_ref(list,OS_F64,r,dst);
  1577. inc(src.offset,8);
  1578. inc(dst.offset,8);
  1579. end;
  1580. a_reg_dealloc(list,r);
  1581. len := len mod 8;
  1582. end;
  1583. if (len and 4) <> 0 then
  1584. begin
  1585. r.enum:=R_INTREGISTER;
  1586. r.number:=NR_R0;
  1587. a_reg_alloc(list,r);
  1588. a_load_ref_reg(list,OS_32,src,r);
  1589. a_load_reg_ref(list,OS_32,r,dst);
  1590. inc(src.offset,4);
  1591. inc(dst.offset,4);
  1592. a_reg_dealloc(list,r);
  1593. end;
  1594. { copy the leftovers }
  1595. if (len and 2) <> 0 then
  1596. begin
  1597. r.enum:=R_INTREGISTER;
  1598. r.number:=NR_R0;
  1599. a_reg_alloc(list,r);
  1600. a_load_ref_reg(list,OS_16,src,r);
  1601. a_load_reg_ref(list,OS_16,r,dst);
  1602. inc(src.offset,2);
  1603. inc(dst.offset,2);
  1604. a_reg_dealloc(list,r);
  1605. end;
  1606. if (len and 1) <> 0 then
  1607. begin
  1608. r.enum:=R_INTREGISTER;
  1609. r.number:=NR_R0;
  1610. a_reg_alloc(list,r);
  1611. a_load_ref_reg(list,OS_8,src,r);
  1612. a_load_reg_ref(list,OS_8,r,dst);
  1613. a_reg_dealloc(list,r);
  1614. end;
  1615. if orgsrc then
  1616. begin
  1617. if delsource then
  1618. reference_release(list,source);
  1619. end
  1620. else
  1621. free_scratch_reg(list,src.base);
  1622. if not orgdst then
  1623. free_scratch_reg(list,dst.base);
  1624. end;
  1625. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1626. var
  1627. lenref : treference;
  1628. power,len : longint;
  1629. {$ifndef __NOWINPECOFF__}
  1630. again,ok : tasmlabel;
  1631. {$endif}
  1632. r,r2,rsp:Tregister;
  1633. begin
  1634. {$warning !!!! FIX ME !!!!}
  1635. {!!!!
  1636. lenref:=ref;
  1637. inc(lenref.offset,4);
  1638. { get stack space }
  1639. r.enum:=R_INTREGISTER;
  1640. r.number:=NR_EDI;
  1641. rsp.enum:=R_INTREGISTER;
  1642. rsp.number:=NR_ESP;
  1643. r2.enum:=R_INTREGISTER;
  1644. rg.getexplicitregisterint(list,NR_EDI);
  1645. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1646. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1647. if (elesize<>1) then
  1648. begin
  1649. if ispowerof2(elesize, power) then
  1650. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1651. else
  1652. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1653. end;
  1654. {$ifndef __NOWINPECOFF__}
  1655. { windows guards only a few pages for stack growing, }
  1656. { so we have to access every page first }
  1657. if target_info.system=system_i386_win32 then
  1658. begin
  1659. objectlibrary.getlabel(again);
  1660. objectlibrary.getlabel(ok);
  1661. a_label(list,again);
  1662. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1663. a_jmp_cond(list,OC_B,ok);
  1664. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1665. r2.number:=NR_EAX;
  1666. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1667. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1668. a_jmp_always(list,again);
  1669. a_label(list,ok);
  1670. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1671. rg.ungetregisterint(list,r);
  1672. { now reload EDI }
  1673. rg.getexplicitregisterint(list,NR_EDI);
  1674. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1675. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1676. if (elesize<>1) then
  1677. begin
  1678. if ispowerof2(elesize, power) then
  1679. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1680. else
  1681. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1682. end;
  1683. end
  1684. else
  1685. {$endif __NOWINPECOFF__}
  1686. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1687. { align stack on 4 bytes }
  1688. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1689. { load destination }
  1690. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1691. { don't destroy the registers! }
  1692. r2.number:=NR_ECX;
  1693. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1694. r2.number:=NR_ESI;
  1695. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1696. { load count }
  1697. r2.number:=NR_ECX;
  1698. a_load_ref_reg(list,OS_INT,lenref,r2);
  1699. { load source }
  1700. r2.number:=NR_ESI;
  1701. a_load_ref_reg(list,OS_INT,ref,r2);
  1702. { scheduled .... }
  1703. r2.number:=NR_ECX;
  1704. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1705. { calculate size }
  1706. len:=elesize;
  1707. opsize:=S_B;
  1708. if (len and 3)=0 then
  1709. begin
  1710. opsize:=S_L;
  1711. len:=len shr 2;
  1712. end
  1713. else
  1714. if (len and 1)=0 then
  1715. begin
  1716. opsize:=S_W;
  1717. len:=len shr 1;
  1718. end;
  1719. if ispowerof2(len, power) then
  1720. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1721. else
  1722. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1723. list.concat(Taicpu.op_none(A_REP,S_NO));
  1724. case opsize of
  1725. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1726. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1727. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1728. end;
  1729. rg.ungetregisterint(list,r);
  1730. r2.number:=NR_ESI;
  1731. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1732. r2.number:=NR_ECX;
  1733. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1734. { patch the new address }
  1735. a_load_reg_ref(list,OS_INT,rsp,ref);
  1736. !!!!}
  1737. end;
  1738. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  1739. var
  1740. hl : tasmlabel;
  1741. r:Tregister;
  1742. begin
  1743. if not(cs_check_overflow in aktlocalswitches) then
  1744. exit;
  1745. objectlibrary.getlabel(hl);
  1746. if not ((p.resulttype.def.deftype=pointerdef) or
  1747. ((p.resulttype.def.deftype=orddef) and
  1748. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1749. bool8bit,bool16bit,bool32bit]))) then
  1750. begin
  1751. r.enum:=R_CR7;
  1752. list.concat(taicpu.op_reg(A_MCRXR,r));
  1753. a_jmp(list,A_BC,C_OV,7,hl)
  1754. end
  1755. else
  1756. a_jmp_cond(list,OC_AE,hl);
  1757. a_call_name(list,'FPC_OVERFLOW');
  1758. a_label(list,hl);
  1759. end;
  1760. {***************** This is private property, keep out! :) *****************}
  1761. function tcgppc.issimpleref(const ref: treference): boolean;
  1762. begin
  1763. if (ref.base.number = NR_NO) and
  1764. (ref.index.number <> NR_NO) then
  1765. internalerror(200208101);
  1766. result :=
  1767. not(assigned(ref.symbol)) and
  1768. (((ref.index.number = NR_NO) and
  1769. (ref.offset >= low(smallint)) and
  1770. (ref.offset <= high(smallint))) or
  1771. ((ref.index.number <> NR_NO) and
  1772. (ref.offset = 0)));
  1773. end;
  1774. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1775. var
  1776. tmpreg: tregister;
  1777. begin
  1778. result := false;
  1779. if (ref.base.number = NR_NO) then
  1780. ref.base := ref.index;
  1781. if (ref.base.number <> NR_NO) then
  1782. begin
  1783. if (ref.index.number <> NR_NO) and
  1784. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1785. begin
  1786. result := true;
  1787. tmpreg := cg.get_scratch_reg_int(list,OS_INT);
  1788. if not assigned(ref.symbol) and
  1789. (cardinal(ref.offset-low(smallint)) <=
  1790. high(smallint)-low(smallint)) then
  1791. begin
  1792. list.concat(taicpu.op_reg_reg_const(
  1793. A_ADDI,tmpreg,ref.base,ref.offset));
  1794. ref.offset := 0;
  1795. end
  1796. else
  1797. begin
  1798. list.concat(taicpu.op_reg_reg_reg(
  1799. A_ADD,tmpreg,ref.base,ref.index));
  1800. ref.index.number := NR_NO;
  1801. end;
  1802. ref.base := tmpreg;
  1803. end
  1804. end
  1805. else
  1806. if ref.index.number <> NR_NO then
  1807. internalerror(200208102);
  1808. end;
  1809. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1810. { that's the case, we can use rlwinm to do an AND operation }
  1811. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1812. var
  1813. temp : longint;
  1814. testbit : aword;
  1815. compare: boolean;
  1816. begin
  1817. get_rlwi_const := false;
  1818. if (a = 0) or (a = $ffffffff) then
  1819. exit;
  1820. { start with the lowest bit }
  1821. testbit := 1;
  1822. { check its value }
  1823. compare := boolean(a and testbit);
  1824. { find out how long the run of bits with this value is }
  1825. { (it's impossible that all bits are 1 or 0, because in that case }
  1826. { this function wouldn't have been called) }
  1827. l1 := 31;
  1828. while (((a and testbit) <> 0) = compare) do
  1829. begin
  1830. testbit := testbit shl 1;
  1831. dec(l1);
  1832. end;
  1833. { check the length of the run of bits that comes next }
  1834. compare := not compare;
  1835. l2 := l1;
  1836. while (((a and testbit) <> 0) = compare) and
  1837. (l2 >= 0) do
  1838. begin
  1839. testbit := testbit shl 1;
  1840. dec(l2);
  1841. end;
  1842. { and finally the check whether the rest of the bits all have the }
  1843. { same value }
  1844. compare := not compare;
  1845. temp := l2;
  1846. if temp >= 0 then
  1847. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1848. exit;
  1849. { we have done "not(not(compare))", so compare is back to its }
  1850. { initial value. If the lowest bit was 0, a is of the form }
  1851. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1852. { because l2 now contains the position of the last zero of the }
  1853. { first run instead of that of the first 1) so switch l1 and l2 }
  1854. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1855. if not compare then
  1856. begin
  1857. temp := l1;
  1858. l1 := l2+1;
  1859. l2 := temp;
  1860. end
  1861. else
  1862. { otherwise, l1 currently contains the position of the last }
  1863. { zero instead of that of the first 1 of the second run -> +1 }
  1864. inc(l1);
  1865. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1866. l1 := l1 and 31;
  1867. l2 := l2 and 31;
  1868. get_rlwi_const := true;
  1869. end;
  1870. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1871. ref: treference);
  1872. var
  1873. tmpreg: tregister;
  1874. tmpref: treference;
  1875. r : Tregister;
  1876. begin
  1877. tmpreg.number := NR_NO;
  1878. if assigned(ref.symbol) or
  1879. (cardinal(ref.offset-low(smallint)) >
  1880. high(smallint)-low(smallint)) then
  1881. begin
  1882. if target_info.system = system_powerpc_macos then
  1883. begin
  1884. if ref.base.number <> NR_NO then
  1885. begin
  1886. if macos_direct_globals then
  1887. begin
  1888. {Generates
  1889. add tempreg, ref.base, RTOC
  1890. op reg, symbolplusoffset, tempreg
  1891. which is eqvivalent to the more comprehensive
  1892. addi tempreg, RTOC, symbolplusoffset
  1893. add tempreg, ref.base, tempreg
  1894. op reg, tempreg
  1895. but which saves one instruction.}
  1896. tmpreg := get_scratch_reg_address(list);
  1897. reference_reset(tmpref);
  1898. tmpref.symbol := ref.symbol;
  1899. tmpref.offset := ref.offset;
  1900. tmpref.symaddr := refs_full;
  1901. tmpref.base:= tmpreg;
  1902. r.enum:=R_INTREGISTER;
  1903. r.number:=NR_RTOC;
  1904. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1905. ref.base,r));
  1906. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  1907. end
  1908. else
  1909. begin
  1910. tmpreg := get_scratch_reg_address(list);
  1911. reference_reset(tmpref);
  1912. tmpref.symbol := ref.symbol;
  1913. tmpref.offset := ref.offset;
  1914. tmpref.symaddr := refs_full;
  1915. tmpref.base.enum:= R_INTREGISTER;
  1916. tmpref.base.number:= NR_RTOC;
  1917. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1918. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1919. ref.base,tmpreg));
  1920. reference_reset(tmpref);
  1921. tmpref.offset := 0;
  1922. tmpref.symaddr := refs_full;
  1923. tmpref.base:= tmpreg;
  1924. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  1925. end;
  1926. //list.concat(tai_comment.create(strpnew('**** a_load_store 1')));
  1927. end
  1928. else
  1929. begin
  1930. if macos_direct_globals then
  1931. begin
  1932. reference_reset(tmpref);
  1933. tmpref.symbol := ref.symbol;
  1934. tmpref.offset := ref.offset;
  1935. tmpref.symaddr := refs_full;
  1936. tmpref.base.enum:= R_INTREGISTER;
  1937. tmpref.base.number:= NR_RTOC;
  1938. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  1939. end
  1940. else
  1941. begin
  1942. tmpreg := get_scratch_reg_address(list);
  1943. reference_reset(tmpref);
  1944. tmpref.symbol := ref.symbol;
  1945. tmpref.offset := ref.offset;
  1946. tmpref.symaddr := refs_full;
  1947. tmpref.base.enum:= R_INTREGISTER;
  1948. tmpref.base.number:= NR_RTOC;
  1949. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1950. reference_reset(tmpref);
  1951. tmpref.offset := 0;
  1952. tmpref.symaddr := refs_full;
  1953. tmpref.base:= tmpreg;
  1954. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  1955. end;
  1956. //list.concat(tai_comment.create(strpnew('*** a_load_store 2')));
  1957. end;
  1958. end
  1959. else
  1960. begin
  1961. tmpreg := get_scratch_reg_address(list);
  1962. reference_reset(tmpref);
  1963. tmpref.symbol := ref.symbol;
  1964. tmpref.offset := ref.offset;
  1965. tmpref.symaddr := refs_ha;
  1966. if ref.base.number <> NR_NO then
  1967. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1968. ref.base,tmpref))
  1969. else
  1970. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1971. ref.base := tmpreg;
  1972. ref.symaddr := refs_l;
  1973. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1974. end
  1975. end
  1976. else
  1977. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1978. if (tmpreg.number <> NR_NO) then
  1979. free_scratch_reg(list,tmpreg);
  1980. end;
  1981. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1982. crval: longint; l: tasmlabel);
  1983. var
  1984. p: taicpu;
  1985. begin
  1986. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  1987. if op <> A_B then
  1988. create_cond_norm(c,crval,p.condition);
  1989. p.is_jmp := true;
  1990. list.concat(p)
  1991. end;
  1992. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1993. begin
  1994. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1995. end;
  1996. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1997. begin
  1998. a_op64_const_reg_reg(list,op,value,reg,reg);
  1999. end;
  2000. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2001. begin
  2002. case op of
  2003. OP_AND,OP_OR,OP_XOR:
  2004. begin
  2005. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2006. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2007. end;
  2008. OP_ADD:
  2009. begin
  2010. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2011. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2012. end;
  2013. OP_SUB:
  2014. begin
  2015. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2016. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2017. end;
  2018. else
  2019. internalerror(2002072801);
  2020. end;
  2021. end;
  2022. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2023. const
  2024. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2025. (A_SUBIC,A_SUBC,A_ADDME));
  2026. var
  2027. tmpreg: tregister;
  2028. tmpreg64: tregister64;
  2029. newop: TOpCG;
  2030. issub: boolean;
  2031. begin
  2032. case op of
  2033. OP_AND,OP_OR,OP_XOR:
  2034. begin
  2035. cg.a_op_const_reg_reg(list,op,OS_32,cardinal(value),regsrc.reglo,regdst.reglo);
  2036. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  2037. regdst.reghi);
  2038. end;
  2039. OP_ADD, OP_SUB:
  2040. begin
  2041. if (int64(value) < 0) then
  2042. begin
  2043. if op = OP_ADD then
  2044. op := OP_SUB
  2045. else
  2046. op := OP_ADD;
  2047. int64(value) := -int64(value);
  2048. end;
  2049. if (longint(value) <> 0) then
  2050. begin
  2051. issub := op = OP_SUB;
  2052. if (int64(value) > 0) and
  2053. (int64(value)-ord(issub) <= 32767) then
  2054. begin
  2055. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2056. regdst.reglo,regsrc.reglo,longint(value)));
  2057. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2058. regdst.reghi,regsrc.reghi));
  2059. end
  2060. else if ((value shr 32) = 0) then
  2061. begin
  2062. tmpreg := cg.get_scratch_reg_int(list,OS_32);
  2063. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2064. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2065. regdst.reglo,regsrc.reglo,tmpreg));
  2066. cg.free_scratch_reg(list,tmpreg);
  2067. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2068. regdst.reghi,regsrc.reghi));
  2069. end
  2070. else
  2071. begin
  2072. tmpreg64.reglo := cg.get_scratch_reg_int(list,OS_INT);
  2073. tmpreg64.reghi := cg.get_scratch_reg_int(list,OS_INT);
  2074. a_load64_const_reg(list,value,tmpreg64);
  2075. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2076. cg.free_scratch_reg(list,tmpreg64.reghi);
  2077. cg.free_scratch_reg(list,tmpreg64.reglo);
  2078. end
  2079. end
  2080. else
  2081. begin
  2082. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2083. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  2084. regdst.reghi);
  2085. end;
  2086. end;
  2087. else
  2088. internalerror(2002072802);
  2089. end;
  2090. end;
  2091. begin
  2092. cg := tcgppc.create;
  2093. cg64 :=tcg64fppc.create;
  2094. end.
  2095. {
  2096. $Log$
  2097. Revision 1.86 2003-04-27 11:21:36 peter
  2098. * aktprocdef renamed to current_procdef
  2099. * procinfo renamed to current_procinfo
  2100. * procinfo will now be stored in current_module so it can be
  2101. cleaned up properly
  2102. * gen_main_procsym changed to create_main_proc and release_main_proc
  2103. to also generate a tprocinfo structure
  2104. * fixed unit implicit initfinal
  2105. Revision 1.85 2003/04/26 22:56:11 jonas
  2106. * fix to a_op64_const_reg_reg
  2107. Revision 1.84 2003/04/26 16:08:41 jonas
  2108. * fixed g_flags2reg
  2109. Revision 1.83 2003/04/26 15:25:29 florian
  2110. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2111. Revision 1.82 2003/04/25 20:55:34 florian
  2112. * stack frame calculations are now completly done using the code generator
  2113. routines instead of generating directly assembler so also large stack frames
  2114. are handle properly
  2115. Revision 1.81 2003/04/24 11:24:00 florian
  2116. * fixed several issues with nested procedures
  2117. Revision 1.80 2003/04/23 22:18:01 peter
  2118. * fixes to get rtl compiled
  2119. Revision 1.79 2003/04/23 12:35:35 florian
  2120. * fixed several issues with powerpc
  2121. + applied a patch from Jonas for nested function calls (PowerPC only)
  2122. * ...
  2123. Revision 1.78 2003/04/16 09:26:55 jonas
  2124. * assembler procedures now again get a stackframe if they have local
  2125. variables. No space is reserved for a function result however.
  2126. Also, the register parameters aren't automatically saved on the stack
  2127. anymore in assembler procedures.
  2128. Revision 1.77 2003/04/06 16:39:11 jonas
  2129. * don't generate entry/exit code for assembler procedures
  2130. Revision 1.76 2003/03/22 18:01:13 jonas
  2131. * fixed linux entry/exit code generation
  2132. Revision 1.75 2003/03/19 14:26:26 jonas
  2133. * fixed R_TOC bugs introduced by new register allocator conversion
  2134. Revision 1.74 2003/03/13 22:57:45 olle
  2135. * change in a_loadaddr_ref_reg
  2136. Revision 1.73 2003/03/12 22:43:38 jonas
  2137. * more powerpc and generic fixes related to the new register allocator
  2138. Revision 1.72 2003/03/11 21:46:24 jonas
  2139. * lots of new regallocator fixes, both in generic and ppc-specific code
  2140. (ppc compiler still can't compile the linux system unit though)
  2141. Revision 1.71 2003/02/19 22:00:16 daniel
  2142. * Code generator converted to new register notation
  2143. - Horribily outdated todo.txt removed
  2144. Revision 1.70 2003/01/13 17:17:50 olle
  2145. * changed global var access, TOC now contain pointers to globals
  2146. * fixed handling of function pointers
  2147. Revision 1.69 2003/01/09 22:00:53 florian
  2148. * fixed some PowerPC issues
  2149. Revision 1.68 2003/01/08 18:43:58 daniel
  2150. * Tregister changed into a record
  2151. Revision 1.67 2002/12/15 19:22:01 florian
  2152. * fixed some crashes and a rte 201
  2153. Revision 1.66 2002/11/28 10:55:16 olle
  2154. * macos: changing code gen for references to globals
  2155. Revision 1.65 2002/11/07 15:50:23 jonas
  2156. * fixed bctr(l) problems
  2157. Revision 1.64 2002/11/04 18:24:19 olle
  2158. * macos: globals are located in TOC and relative r2, instead of absolute
  2159. Revision 1.63 2002/10/28 22:24:28 olle
  2160. * macos entry/exit: only used registers are saved
  2161. - macos entry/exit: stackptr not saved in r31 anymore
  2162. * macos entry/exit: misc fixes
  2163. Revision 1.62 2002/10/19 23:51:48 olle
  2164. * macos stack frame size computing updated
  2165. + macos epilogue: control register now restored
  2166. * macos prologue and epilogue: fp reg now saved and restored
  2167. Revision 1.61 2002/10/19 12:50:36 olle
  2168. * reorganized prologue and epilogue routines
  2169. Revision 1.60 2002/10/02 21:49:51 florian
  2170. * all A_BL instructions replaced by calls to a_call_name
  2171. Revision 1.59 2002/10/02 13:24:58 jonas
  2172. * changed a_call_* so that no superfluous code is generated anymore
  2173. Revision 1.58 2002/09/17 18:54:06 jonas
  2174. * a_load_reg_reg() now has two size parameters: source and dest. This
  2175. allows some optimizations on architectures that don't encode the
  2176. register size in the register name.
  2177. Revision 1.57 2002/09/10 21:22:25 jonas
  2178. + added some internal errors
  2179. * fixed bug in sysv exit code
  2180. Revision 1.56 2002/09/08 20:11:56 jonas
  2181. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2182. Revision 1.55 2002/09/08 13:03:26 jonas
  2183. * several large offset-related fixes
  2184. Revision 1.54 2002/09/07 17:54:58 florian
  2185. * first part of PowerPC fixes
  2186. Revision 1.53 2002/09/07 15:25:14 peter
  2187. * old logs removed and tabs fixed
  2188. Revision 1.52 2002/09/02 10:14:51 jonas
  2189. + a_call_reg()
  2190. * small fix in a_call_ref()
  2191. Revision 1.51 2002/09/02 06:09:02 jonas
  2192. * fixed range error
  2193. Revision 1.50 2002/09/01 21:04:49 florian
  2194. * several powerpc related stuff fixed
  2195. Revision 1.49 2002/09/01 12:09:27 peter
  2196. + a_call_reg, a_call_loc added
  2197. * removed exprasmlist references
  2198. Revision 1.48 2002/08/31 21:38:02 jonas
  2199. * fixed a_call_ref (it should load ctr, not lr)
  2200. Revision 1.47 2002/08/31 21:30:45 florian
  2201. * fixed several problems caused by Jonas' commit :)
  2202. Revision 1.46 2002/08/31 19:25:50 jonas
  2203. + implemented a_call_ref()
  2204. Revision 1.45 2002/08/18 22:16:14 florian
  2205. + the ppc gas assembler writer adds now registers aliases
  2206. to the assembler file
  2207. Revision 1.44 2002/08/17 18:23:53 florian
  2208. * some assembler writer bugs fixed
  2209. Revision 1.43 2002/08/17 09:23:49 florian
  2210. * first part of procinfo rewrite
  2211. Revision 1.42 2002/08/16 14:24:59 carl
  2212. * issameref() to test if two references are the same (then emit no opcodes)
  2213. + ret_in_reg to replace ret_in_acc
  2214. (fix some register allocation bugs at the same time)
  2215. + save_std_register now has an extra parameter which is the
  2216. usedinproc registers
  2217. Revision 1.41 2002/08/15 08:13:54 carl
  2218. - a_load_sym_ofs_reg removed
  2219. * loadvmt now calls loadaddr_ref_reg instead
  2220. Revision 1.40 2002/08/11 14:32:32 peter
  2221. * renamed current_library to objectlibrary
  2222. Revision 1.39 2002/08/11 13:24:18 peter
  2223. * saving of asmsymbols in ppu supported
  2224. * asmsymbollist global is removed and moved into a new class
  2225. tasmlibrarydata that will hold the info of a .a file which
  2226. corresponds with a single module. Added librarydata to tmodule
  2227. to keep the library info stored for the module. In the future the
  2228. objectfiles will also be stored to the tasmlibrarydata class
  2229. * all getlabel/newasmsymbol and friends are moved to the new class
  2230. Revision 1.38 2002/08/11 11:39:31 jonas
  2231. + powerpc-specific genlinearlist
  2232. Revision 1.37 2002/08/10 17:15:31 jonas
  2233. * various fixes and optimizations
  2234. Revision 1.36 2002/08/06 20:55:23 florian
  2235. * first part of ppc calling conventions fix
  2236. Revision 1.35 2002/08/06 07:12:05 jonas
  2237. * fixed bug in g_flags2reg()
  2238. * and yet more constant operation fixes :)
  2239. Revision 1.34 2002/08/05 08:58:53 jonas
  2240. * fixed compilation problems
  2241. Revision 1.33 2002/08/04 12:57:55 jonas
  2242. * more misc. fixes, mostly constant-related
  2243. }