cgcpu.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. {
  2. Copyright (c) 1998-2002 by the FPC team
  3. This unit implements the code generator for the 680x0
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,cgobj,globtype,
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cpubase,cpuinfo,
  24. parabase,cpupara,
  25. node,symconst,symtype,symdef,
  26. cgutils,cg64f32;
  27. type
  28. tcg68k = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  37. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  38. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  39. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  40. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  41. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  42. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  43. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  44. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  45. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  46. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  47. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  48. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  49. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  50. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  51. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); override;
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister; l : tasmlabel);override;
  53. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel); override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  59. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  60. { generates overflow checking code for a node }
  61. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  62. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  63. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  64. procedure g_save_registers(list:TAsmList);override;
  65. procedure g_restore_registers(list:TAsmList);override;
  66. procedure g_adjust_self_value(list:TAsmList;procdef:tprocdef;ioffset:tcgint);override;
  67. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  68. { # Sign or zero extend the register to a full 32-bit value.
  69. The new value is left in the same register.
  70. }
  71. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  72. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  73. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  74. function fixref(list: TAsmList; var ref: treference): boolean;
  75. protected
  76. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  77. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  78. private
  79. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  80. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  81. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  82. end;
  83. tcg64f68k = class(tcg64f32)
  84. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  85. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  86. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  87. end;
  88. { This function returns true if the reference+offset is valid.
  89. Otherwise extra code must be generated to solve the reference.
  90. On the m68k, this verifies that the reference is valid
  91. (e.g : if index register is used, then the max displacement
  92. is 256 bytes, if only base is used, then max displacement
  93. is 32K
  94. }
  95. function isvalidrefoffset(const ref: treference): boolean;
  96. function isvalidreference(const ref: treference): boolean;
  97. procedure create_codegen;
  98. implementation
  99. uses
  100. globals,verbose,systems,cutils,
  101. symsym,symtable,defutil,paramgr,procinfo,
  102. rgobj,tgobj,rgcpu,fmodule;
  103. const
  104. { opcode table lookup }
  105. topcg2tasmop: Array[topcg] of tasmop =
  106. (
  107. A_NONE,
  108. A_MOVE,
  109. A_ADD,
  110. A_AND,
  111. A_DIVU,
  112. A_DIVS,
  113. A_MULS,
  114. A_MULU,
  115. A_NEG,
  116. A_NOT,
  117. A_OR,
  118. A_ASR,
  119. A_LSL,
  120. A_LSR,
  121. A_SUB,
  122. A_EOR,
  123. A_ROL,
  124. A_ROR
  125. );
  126. { opcode with extend bits table lookup, used by 64bit cg }
  127. topcg2tasmopx: Array[topcg] of tasmop =
  128. (
  129. A_NONE,
  130. A_NONE,
  131. A_ADDX,
  132. A_NONE,
  133. A_NONE,
  134. A_NONE,
  135. A_NONE,
  136. A_NONE,
  137. A_NEGX,
  138. A_NONE,
  139. A_NONE,
  140. A_NONE,
  141. A_NONE,
  142. A_NONE,
  143. A_SUBX,
  144. A_NONE,
  145. A_NONE,
  146. A_NONE
  147. );
  148. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  149. (
  150. C_NONE,
  151. C_EQ,
  152. C_GT,
  153. C_LT,
  154. C_GE,
  155. C_LE,
  156. C_NE,
  157. C_LS,
  158. C_CS,
  159. C_CC,
  160. C_HI
  161. );
  162. function isvalidreference(const ref: treference): boolean;
  163. begin
  164. isvalidreference:=isvalidrefoffset(ref) and
  165. { don't try to generate addressing with symbol and base reg and offset
  166. it might fail in linking stage if the symbol is more than 32k away (KB) }
  167. not (assigned(ref.symbol) and (ref.base <> NR_NO) and (ref.offset <> 0)) and
  168. { coldfire and 68000 cannot handle non-addressregs as bases }
  169. not ((current_settings.cputype in cpu_coldfire+[cpu_mc68000]) and
  170. not isaddressregister(ref.base));
  171. end;
  172. function isvalidrefoffset(const ref: treference): boolean;
  173. begin
  174. isvalidrefoffset := true;
  175. if ref.index <> NR_NO then
  176. begin
  177. // if ref.base <> NR_NO then
  178. // internalerror(2002081401);
  179. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  180. isvalidrefoffset := false
  181. end
  182. else
  183. begin
  184. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  185. isvalidrefoffset := false;
  186. end;
  187. end;
  188. {****************************************************************************}
  189. { TCG68K }
  190. {****************************************************************************}
  191. function use_push(const cgpara:tcgpara):boolean;
  192. begin
  193. result:=(not paramanager.use_fixed_stack) and
  194. assigned(cgpara.location) and
  195. (cgpara.location^.loc=LOC_REFERENCE) and
  196. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  197. end;
  198. procedure tcg68k.init_register_allocators;
  199. var
  200. reg: TSuperRegister;
  201. address_regs: array of TSuperRegister;
  202. begin
  203. inherited init_register_allocators;
  204. address_regs:=nil;
  205. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  206. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  207. first_int_imreg,[]);
  208. { set up the array of address registers to use }
  209. for reg:=RS_A0 to RS_A6 do
  210. begin
  211. { don't hardwire the frame pointer register, because it can vary between target OS }
  212. if assigned(current_procinfo) and (current_procinfo.framepointer = NR_FRAME_POINTER_REG)
  213. and (reg = RS_FRAME_POINTER_REG) then
  214. continue;
  215. setlength(address_regs,length(address_regs)+1);
  216. address_regs[length(address_regs)-1]:=reg;
  217. end;
  218. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  219. address_regs, first_addr_imreg, []);
  220. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  221. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  222. first_fpu_imreg,[]);
  223. end;
  224. procedure tcg68k.done_register_allocators;
  225. begin
  226. rg[R_INTREGISTER].free;
  227. rg[R_FPUREGISTER].free;
  228. rg[R_ADDRESSREGISTER].free;
  229. inherited done_register_allocators;
  230. end;
  231. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  232. var
  233. pushsize : tcgsize;
  234. ref : treference;
  235. begin
  236. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  237. { TODO: FIX ME! check_register_size()}
  238. // check_register_size(size,r);
  239. if use_push(cgpara) then
  240. begin
  241. cgpara.check_simple_location;
  242. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  243. pushsize:=cgpara.location^.size
  244. else
  245. pushsize:=int_cgsize(cgpara.alignment);
  246. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  247. ref.direction := dir_dec;
  248. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  249. end
  250. else
  251. inherited a_load_reg_cgpara(list,size,r,cgpara);
  252. end;
  253. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  254. var
  255. pushsize : tcgsize;
  256. ref : treference;
  257. begin
  258. if use_push(cgpara) then
  259. begin
  260. cgpara.check_simple_location;
  261. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  262. pushsize:=cgpara.location^.size
  263. else
  264. pushsize:=int_cgsize(cgpara.alignment);
  265. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  266. ref.direction := dir_dec;
  267. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[pushsize],a,ref));
  268. end
  269. else
  270. inherited a_load_const_cgpara(list,size,a,cgpara);
  271. end;
  272. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  273. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  274. var
  275. pushsize : tcgsize;
  276. tmpreg : tregister;
  277. href : treference;
  278. ref : treference;
  279. begin
  280. if not assigned(paraloc) then
  281. exit;
  282. { TODO: FIX ME!!! this also triggers location bug }
  283. {if (paraloc^.loc<>LOC_REFERENCE) or
  284. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  285. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  286. internalerror(200501162);}
  287. { Pushes are needed in reverse order, add the size of the
  288. current location to the offset where to load from. This
  289. prevents wrong calculations for the last location when
  290. the size is not a power of 2 }
  291. if assigned(paraloc^.next) then
  292. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  293. { Push the data starting at ofs }
  294. href:=r;
  295. inc(href.offset,ofs);
  296. fixref(list,href);
  297. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  298. pushsize:=paraloc^.size
  299. else
  300. pushsize:=int_cgsize(cgpara.alignment);
  301. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[pushsize]);
  302. ref.direction := dir_dec;
  303. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  304. begin
  305. tmpreg:=getintregister(list,pushsize);
  306. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  307. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],tmpreg,ref));
  308. end
  309. else
  310. list.concat(taicpu.op_ref_ref(A_MOVE,tcgsize2opsize[pushsize],href,ref));
  311. end;
  312. var
  313. len : tcgint;
  314. href : treference;
  315. begin
  316. { cgpara.size=OS_NO requires a copy on the stack }
  317. if use_push(cgpara) then
  318. begin
  319. { Record copy? }
  320. if (cgpara.size in [OS_NO,OS_F64]) or (size in [OS_NO,OS_F64]) then
  321. begin
  322. //list.concat(tai_comment.create(strpnew('a_load_ref_cgpara: g_concatcopy')));
  323. cgpara.check_simple_location;
  324. len:=align(cgpara.intsize,cgpara.alignment);
  325. g_stackpointer_alloc(list,len);
  326. reference_reset_base(href,NR_STACK_POINTER_REG,0,cgpara.alignment);
  327. g_concatcopy(list,r,href,len);
  328. end
  329. else
  330. begin
  331. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  332. internalerror(200501161);
  333. { We need to push the data in reverse order,
  334. therefor we use a recursive algorithm }
  335. pushdata(cgpara.location,0);
  336. end
  337. end
  338. else
  339. inherited a_load_ref_cgpara(list,size,r,cgpara);
  340. end;
  341. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  342. var
  343. tmpref : treference;
  344. begin
  345. { 68k always passes arguments on the stack }
  346. if use_push(cgpara) then
  347. begin
  348. //list.concat(tai_comment.create(strpnew('a_loadaddr_ref_cgpara: PEA')));
  349. cgpara.check_simple_location;
  350. tmpref:=r;
  351. fixref(list,tmpref);
  352. list.concat(taicpu.op_ref(A_PEA,S_NO,tmpref));
  353. end
  354. else
  355. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  356. end;
  357. function tcg68k.fixref(list: TAsmList; var ref: treference): boolean;
  358. var
  359. hreg,idxreg : tregister;
  360. href : treference;
  361. instr : taicpu;
  362. scale : aint;
  363. begin
  364. result:=false;
  365. { The MC68020+ has extended
  366. addressing capabilities with a 32-bit
  367. displacement.
  368. }
  369. { first ensure that base is an address register }
  370. if ((ref.base<>NR_NO) and (ref.index<>NR_NO)) and
  371. (not isaddressregister(ref.base) and isaddressregister(ref.index)) and
  372. (ref.scalefactor < 2) then
  373. begin
  374. { if we have both base and index registers, but base is data and index
  375. is address, we can just swap them, as FPC always uses long index.
  376. but we can only do this, if the index has no scalefactor }
  377. hreg:=ref.base;
  378. ref.base:=ref.index;
  379. ref.index:=hreg;
  380. //list.concat(tai_comment.create(strpnew('fixref: base and index swapped')));
  381. end;
  382. if (not assigned (ref.symbol) and (current_settings.cputype<>cpu_MC68000)) and
  383. (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  384. begin
  385. hreg:=getaddressregister(list);
  386. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  387. add_move_instruction(instr);
  388. list.concat(instr);
  389. fixref:=true;
  390. ref.base:=hreg;
  391. end;
  392. if (current_settings.cputype=cpu_MC68020) then
  393. exit;
  394. { ToDo: check which constraints of Coldfire also apply to MC68000 }
  395. case current_settings.cputype of
  396. cpu_MC68000:
  397. begin
  398. if (ref.base<>NR_NO) then
  399. begin
  400. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  401. begin
  402. hreg:=getaddressregister(list);
  403. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  404. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.index,hreg));
  405. ref.index:=NR_NO;
  406. ref.base:=hreg;
  407. end;
  408. { base + reg }
  409. if ref.index <> NR_NO then
  410. begin
  411. { base + reg + offset }
  412. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  413. begin
  414. hreg:=getaddressregister(list);
  415. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  416. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  417. fixref:=true;
  418. ref.offset:=0;
  419. ref.base:=hreg;
  420. exit;
  421. end;
  422. end
  423. else
  424. { base + offset }
  425. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  426. begin
  427. hreg:=getaddressregister(list);
  428. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  429. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  430. fixref:=true;
  431. ref.offset:=0;
  432. ref.base:=hreg;
  433. exit;
  434. end;
  435. if assigned(ref.symbol) then
  436. begin
  437. hreg:=getaddressregister(list);
  438. idxreg:=ref.base;
  439. ref.base:=NR_NO;
  440. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  441. reference_reset_base(ref,hreg,0,ref.alignment);
  442. fixref:=true;
  443. ref.index:=idxreg;
  444. end
  445. else if not isaddressregister(ref.base) then
  446. begin
  447. hreg:=getaddressregister(list);
  448. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  449. //add_move_instruction(instr);
  450. list.concat(instr);
  451. fixref:=true;
  452. ref.base:=hreg;
  453. end;
  454. end
  455. else
  456. { Note: symbol -> ref would be supported as long as ref does not
  457. contain a offset or index... (maybe something for the
  458. optimizer) }
  459. if Assigned(ref.symbol) and (ref.index<>NR_NO) then
  460. begin
  461. hreg:=cg.getaddressregister(list);
  462. idxreg:=ref.index;
  463. ref.index:=NR_NO;
  464. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  465. reference_reset_base(ref,hreg,0,ref.alignment);
  466. ref.index:=idxreg;
  467. fixref:=true;
  468. end;
  469. end;
  470. cpu_isa_a,
  471. cpu_isa_a_p,
  472. cpu_isa_b,
  473. cpu_isa_c:
  474. begin
  475. if (ref.base<>NR_NO) then
  476. begin
  477. if assigned(ref.symbol) then
  478. begin
  479. //list.concat(tai_comment.create(strpnew('fixref: symbol')));
  480. hreg:=cg.getaddressregister(list);
  481. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment);
  482. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  483. if ref.index<>NR_NO then
  484. begin
  485. { fold the symbol + offset into the base, not the base into the index,
  486. because that might screw up the scalefactor of the reference }
  487. //list.concat(tai_comment.create(strpnew('fixref: symbol + offset (index + base)')));
  488. idxreg:=getaddressregister(list);
  489. reference_reset_base(href,ref.base,0,ref.alignment);
  490. href.index:=hreg;
  491. hreg:=getaddressregister(list);
  492. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  493. ref.base:=hreg;
  494. end
  495. else
  496. ref.index:=hreg;
  497. ref.offset:=0;
  498. ref.symbol:=nil;
  499. fixref:=true;
  500. end
  501. else
  502. { base + reg }
  503. if ref.index <> NR_NO then
  504. begin
  505. { base + reg + offset }
  506. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  507. begin
  508. hreg:=getaddressregister(list);
  509. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  510. begin
  511. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  512. //add_move_instruction(instr);
  513. list.concat(instr);
  514. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  515. end
  516. else
  517. begin
  518. //list.concat(tai_comment.create(strpnew('fixref: base + reg + offset lea')));
  519. reference_reset_base(href,ref.base,ref.offset,ref.alignment);
  520. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  521. end;
  522. fixref:=true;
  523. ref.base:=hreg;
  524. ref.offset:=0;
  525. exit;
  526. end;
  527. end
  528. else
  529. { base + offset }
  530. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  531. begin
  532. hreg:=getaddressregister(list);
  533. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  534. //add_move_instruction(instr);
  535. list.concat(instr);
  536. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  537. fixref:=true;
  538. ref.offset:=0;
  539. ref.base:=hreg;
  540. exit;
  541. end;
  542. end
  543. else
  544. { Note: symbol -> ref would be supported as long as ref does not
  545. contain a offset or index... (maybe something for the
  546. optimizer) }
  547. if Assigned(ref.symbol) {and (ref.index<>NR_NO)} then
  548. begin
  549. hreg:=cg.getaddressregister(list);
  550. idxreg:=ref.index;
  551. scale:=ref.scalefactor;
  552. ref.index:=NR_NO;
  553. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  554. reference_reset_base(ref,hreg,0,ref.alignment);
  555. ref.index:=idxreg;
  556. ref.scalefactor:=scale;
  557. fixref:=true;
  558. end;
  559. end;
  560. end;
  561. end;
  562. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  563. var
  564. paraloc1,paraloc2,paraloc3 : tcgpara;
  565. pd : tprocdef;
  566. begin
  567. pd:=search_system_proc(name);
  568. paraloc1.init;
  569. paraloc2.init;
  570. paraloc3.init;
  571. paramanager.getintparaloc(pd,1,paraloc1);
  572. paramanager.getintparaloc(pd,2,paraloc2);
  573. paramanager.getintparaloc(pd,3,paraloc3);
  574. a_load_const_cgpara(list,OS_8,0,paraloc3);
  575. a_load_const_cgpara(list,size,a,paraloc2);
  576. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  577. paramanager.freecgpara(list,paraloc3);
  578. paramanager.freecgpara(list,paraloc2);
  579. paramanager.freecgpara(list,paraloc1);
  580. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  581. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  582. a_call_name(list,name,false);
  583. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  584. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  585. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  586. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  587. paraloc3.done;
  588. paraloc2.done;
  589. paraloc1.done;
  590. end;
  591. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  592. var
  593. paraloc1,paraloc2,paraloc3 : tcgpara;
  594. pd : tprocdef;
  595. begin
  596. pd:=search_system_proc(name);
  597. paraloc1.init;
  598. paraloc2.init;
  599. paraloc3.init;
  600. paramanager.getintparaloc(pd,1,paraloc1);
  601. paramanager.getintparaloc(pd,2,paraloc2);
  602. paramanager.getintparaloc(pd,3,paraloc3);
  603. a_load_const_cgpara(list,OS_8,0,paraloc3);
  604. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  605. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  606. paramanager.freecgpara(list,paraloc3);
  607. paramanager.freecgpara(list,paraloc2);
  608. paramanager.freecgpara(list,paraloc1);
  609. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  610. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  611. a_call_name(list,name,false);
  612. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  613. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  614. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  615. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  616. paraloc3.done;
  617. paraloc2.done;
  618. paraloc1.done;
  619. end;
  620. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  621. var
  622. sym: tasmsymbol;
  623. begin
  624. if not(weak) then
  625. sym:=current_asmdata.RefAsmSymbol(s)
  626. else
  627. sym:=current_asmdata.WeakRefAsmSymbol(s);
  628. list.concat(taicpu.op_sym(A_JSR,S_NO,sym));
  629. end;
  630. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  631. var
  632. tmpref : treference;
  633. tmpreg : tregister;
  634. instr : taicpu;
  635. begin
  636. if isaddressregister(reg) then
  637. begin
  638. { if we have an address register, we can jump to the address directly }
  639. reference_reset_base(tmpref,reg,0,4);
  640. end
  641. else
  642. begin
  643. { if we have a data register, we need to move it to an address register first }
  644. tmpreg:=getaddressregister(list);
  645. reference_reset_base(tmpref,tmpreg,0,4);
  646. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  647. add_move_instruction(instr);
  648. list.concat(instr);
  649. end;
  650. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  651. end;
  652. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  653. var
  654. opsize: topsize;
  655. begin
  656. opsize:=tcgsize2opsize[size];
  657. if isaddressregister(register) then
  658. begin
  659. { an m68k manual I have recommends SUB Ax,Ax to be used instead of CLR for address regs }
  660. if a = 0 then
  661. list.concat(taicpu.op_reg_reg(A_SUB,S_L,register,register))
  662. else
  663. { ISA B/C Coldfire has MOV3Q which can move -1 or 1..7 to any reg }
  664. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  665. ((longint(a) = -1) or ((longint(a) > 0) and (longint(a) < 8))) then
  666. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  667. else
  668. { We don't have to specify the size here, the assembler will decide the size of
  669. the operand it needs. If this ends up as a MOVEA.W, that will sign extend the
  670. value in the dest. reg to full 32 bits (specific to Ax regs only) }
  671. list.concat(taicpu.op_const_reg(A_MOVEA,S_NO,longint(a),register));
  672. end
  673. else
  674. if a = 0 then
  675. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  676. else
  677. begin
  678. { Prefer MOV3Q if applicable, it allows replacement spilling for register }
  679. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  680. ((longint(a)=-1) or ((longint(a)>0) and (longint(a)<8))) then
  681. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  682. else if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  683. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  684. else
  685. begin
  686. { ISA B/C Coldfire has sign extend/zero extend moves }
  687. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  688. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  689. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  690. begin
  691. if size in [OS_16, OS_8] then
  692. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  693. else
  694. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  695. end
  696. else
  697. begin
  698. { clear the register first, for unsigned and positive values, so
  699. we don't need to zero extend after }
  700. if (size in [OS_16,OS_8]) or
  701. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  702. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  703. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  704. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  705. if (size in [OS_S16,OS_S8]) and (a < 0) then
  706. sign_extend(list,size,register);
  707. end;
  708. end;
  709. end;
  710. end;
  711. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  712. var
  713. hreg : tregister;
  714. href : treference;
  715. begin
  716. a:=longint(a);
  717. href:=ref;
  718. fixref(list,href);
  719. if (a=0) and not (current_settings.cputype = cpu_mc68000) then
  720. list.concat(taicpu.op_ref(A_CLR,tcgsize2opsize[tosize],href))
  721. else if (tcgsize2opsize[tosize]=S_L) and
  722. (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  723. ((a=-1) or ((a>0) and (a<8))) then
  724. list.concat(taicpu.op_const_ref(A_MOV3Q,S_L,a,href))
  725. { for coldfire we need to go through a temporary register if we have a
  726. offset, index or symbol given }
  727. else if (current_settings.cputype in cpu_coldfire) and
  728. (
  729. (href.offset<>0) or
  730. { TODO : check whether we really need this second condition }
  731. (href.index<>NR_NO) or
  732. assigned(href.symbol)
  733. ) then
  734. begin
  735. hreg:=getintregister(list,tosize);
  736. a_load_const_reg(list,tosize,a,hreg);
  737. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  738. end
  739. else
  740. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  741. end;
  742. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  743. var
  744. href : treference;
  745. begin
  746. href := ref;
  747. fixref(list,href);
  748. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  749. a_load_reg_reg(list,fromsize,tosize,register,register);
  750. { move to destination reference }
  751. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],register,href));
  752. end;
  753. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  754. var
  755. aref: treference;
  756. bref: treference;
  757. tmpref : treference;
  758. dofix : boolean;
  759. hreg: TRegister;
  760. begin
  761. aref := sref;
  762. bref := dref;
  763. fixref(list,aref);
  764. fixref(list,bref);
  765. if TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize] then
  766. begin
  767. { if we need to change the size then always use a temporary
  768. register }
  769. hreg:=getintregister(list,fromsize);
  770. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  771. sign_extend(list,fromsize,tosize,hreg);
  772. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  773. exit;
  774. end;
  775. { Coldfire dislikes certain move combinations }
  776. if current_settings.cputype in cpu_coldfire then
  777. begin
  778. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  779. dofix:=false;
  780. if { (d16,Ax) and (d8,Ax,Xi) }
  781. (
  782. (aref.base<>NR_NO) and
  783. (
  784. (aref.index<>NR_NO) or
  785. (aref.offset<>0)
  786. )
  787. ) or
  788. { (xxx) }
  789. assigned(aref.symbol) then
  790. begin
  791. if aref.index<>NR_NO then
  792. begin
  793. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  794. (
  795. (bref.base<>NR_NO) and
  796. (
  797. (bref.index<>NR_NO) or
  798. (bref.offset<>0)
  799. )
  800. ) or
  801. { (xxx) }
  802. assigned(bref.symbol);
  803. end
  804. else
  805. { offset <> 0, but no index }
  806. begin
  807. dofix:={ (d8,Ax,Xi) }
  808. (
  809. (bref.base<>NR_NO) and
  810. (bref.index<>NR_NO)
  811. ) or
  812. { (xxx) }
  813. assigned(bref.symbol);
  814. end;
  815. end;
  816. if dofix then
  817. begin
  818. hreg:=getaddressregister(list);
  819. reference_reset_base(tmpref,hreg,0,0);
  820. list.concat(taicpu.op_ref_reg(A_LEA,S_L,aref,hreg));
  821. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],tmpref,bref));
  822. exit;
  823. end;
  824. end;
  825. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  826. end;
  827. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  828. var
  829. instr : taicpu;
  830. begin
  831. { move to destination register }
  832. if (reg1<>reg2) then
  833. begin
  834. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,reg2);
  835. add_move_instruction(instr);
  836. list.concat(instr);
  837. end;
  838. sign_extend(list, fromsize, reg2);
  839. end;
  840. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  841. var
  842. href : treference;
  843. size : tcgsize;
  844. begin
  845. href:=ref;
  846. fixref(list,href);
  847. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  848. size:=fromsize
  849. else
  850. size:=tosize;
  851. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[size],href,register));
  852. { extend the value in the register }
  853. sign_extend(list, size, register);
  854. end;
  855. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  856. var
  857. href : treference;
  858. hreg : tregister;
  859. begin
  860. href:=ref;
  861. fixref(list, href);
  862. if not isaddressregister(r) then
  863. begin
  864. hreg:=getaddressregister(list);
  865. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  866. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  867. end
  868. else
  869. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  870. end;
  871. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  872. var
  873. instr : taicpu;
  874. begin
  875. instr:=taicpu.op_reg_reg(A_FMOVE,S_FX,reg1,reg2);
  876. add_move_instruction(instr);
  877. list.concat(instr);
  878. end;
  879. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  880. var
  881. opsize : topsize;
  882. href : treference;
  883. begin
  884. opsize := tcgsize2opsize[fromsize];
  885. { extended is not supported, since it is not available on Coldfire }
  886. if opsize = S_FX then
  887. internalerror(20020729);
  888. href := ref;
  889. fixref(list,href);
  890. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  891. end;
  892. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  893. var
  894. opsize : topsize;
  895. href : treference;
  896. begin
  897. opsize := tcgsize2opsize[tosize];
  898. { extended is not supported, since it is not available on Coldfire }
  899. if opsize = S_FX then
  900. internalerror(20020729);
  901. href := ref;
  902. fixref(list,href);
  903. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg,href));
  904. end;
  905. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  906. begin
  907. if current_settings.fputype = fpu_soft then
  908. case cgpara.location^.loc of
  909. LOC_REFERENCE,LOC_CREFERENCE:
  910. begin
  911. case size of
  912. OS_F64:
  913. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  914. OS_F32:
  915. a_load_ref_cgpara(list,size,ref,cgpara);
  916. else
  917. internalerror(2013021201);
  918. end;
  919. end;
  920. else
  921. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  922. end
  923. else
  924. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  925. end;
  926. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  927. var
  928. scratch_reg : tregister;
  929. scratch_reg2: tregister;
  930. opcode : tasmop;
  931. begin
  932. optimize_op_const(size, op, a);
  933. opcode := topcg2tasmop[op];
  934. case op of
  935. OP_NONE :
  936. begin
  937. { Opcode is optimized away }
  938. end;
  939. OP_MOVE :
  940. begin
  941. { Optimized, replaced with a simple load }
  942. a_load_const_reg(list,size,a,reg);
  943. end;
  944. OP_ADD,
  945. OP_SUB:
  946. begin
  947. { add/sub works the same way, so have it unified here }
  948. if (a >= 1) and (a <= 8) then
  949. if (op = OP_ADD) then
  950. opcode:=A_ADDQ
  951. else
  952. opcode:=A_SUBQ;
  953. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  954. end;
  955. OP_AND,
  956. OP_OR,
  957. OP_XOR:
  958. begin
  959. scratch_reg := force_to_dataregister(list, size, reg);
  960. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  961. move_if_needed(list, size, scratch_reg, reg);
  962. end;
  963. OP_DIV,
  964. OP_IDIV:
  965. begin
  966. internalerror(20020816);
  967. end;
  968. OP_MUL,
  969. OP_IMUL:
  970. begin
  971. { NOTE: better have this as fast as possible on every CPU in all cases,
  972. because the compiler uses OP_IMUL for array indexing... (KB) }
  973. { ColdFire doesn't support MULS/MULU <imm>,dX }
  974. if current_settings.cputype in cpu_coldfire then
  975. begin
  976. { move const to a register first }
  977. scratch_reg := getintregister(list,OS_INT);
  978. a_load_const_reg(list, size, a, scratch_reg);
  979. { do the multiplication }
  980. scratch_reg2 := force_to_dataregister(list, size, reg);
  981. sign_extend(list, size, scratch_reg2);
  982. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  983. { move the value back to the original register }
  984. move_if_needed(list, size, scratch_reg2, reg);
  985. end
  986. else
  987. begin
  988. if current_settings.cputype = cpu_mc68020 then
  989. begin
  990. { do the multiplication }
  991. scratch_reg := force_to_dataregister(list, size, reg);
  992. sign_extend(list, size, scratch_reg);
  993. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  994. { move the value back to the original register }
  995. move_if_needed(list, size, scratch_reg, reg);
  996. end
  997. else
  998. { Fallback branch, plain 68000 for now }
  999. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1000. if op = OP_MUL then
  1001. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1002. else
  1003. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1004. end;
  1005. end;
  1006. OP_ROL,
  1007. OP_ROR,
  1008. OP_SAR,
  1009. OP_SHL,
  1010. OP_SHR :
  1011. begin
  1012. scratch_reg := force_to_dataregister(list, size, reg);
  1013. sign_extend(list, size, scratch_reg);
  1014. { some special cases which can generate smarter code
  1015. using the SWAP instruction }
  1016. if (a = 16) then
  1017. begin
  1018. if (op = OP_SHL) then
  1019. begin
  1020. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1021. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1022. end
  1023. else if (op = OP_SHR) then
  1024. begin
  1025. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1026. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1027. end
  1028. else if (op = OP_SAR) then
  1029. begin
  1030. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1031. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1032. end
  1033. else if (op = OP_ROR) or (op = OP_ROL) then
  1034. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1035. end
  1036. else if (a >= 1) and (a <= 8) then
  1037. begin
  1038. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1039. end
  1040. else if (a >= 9) and (a < 16) then
  1041. begin
  1042. { Use two ops instead of const -> reg + shift with reg, because
  1043. this way is the same in length and speed but has less register
  1044. pressure }
  1045. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1046. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1047. end
  1048. else
  1049. begin
  1050. { move const to a register first }
  1051. scratch_reg2 := getintregister(list,OS_INT);
  1052. a_load_const_reg(list, size, a, scratch_reg2);
  1053. { do the operation }
  1054. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1055. end;
  1056. { move the value back to the original register }
  1057. move_if_needed(list, size, scratch_reg, reg);
  1058. end;
  1059. else
  1060. internalerror(20020729);
  1061. end;
  1062. end;
  1063. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1064. var
  1065. opcode: tasmop;
  1066. opsize: topsize;
  1067. href : treference;
  1068. begin
  1069. optimize_op_const(size, op, a);
  1070. opcode := topcg2tasmop[op];
  1071. opsize := TCGSize2OpSize[size];
  1072. { on ColdFire all arithmetic operations are only possible on 32bit }
  1073. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1074. and not (op in [OP_NONE,OP_MOVE])) then
  1075. begin
  1076. inherited;
  1077. exit;
  1078. end;
  1079. case op of
  1080. OP_NONE :
  1081. begin
  1082. { opcode was optimized away }
  1083. end;
  1084. OP_MOVE :
  1085. begin
  1086. { Optimized, replaced with a simple load }
  1087. a_load_const_ref(list,size,a,ref);
  1088. end;
  1089. OP_ADD,
  1090. OP_SUB :
  1091. begin
  1092. href:=ref;
  1093. fixref(list,href);
  1094. { add/sub works the same way, so have it unified here }
  1095. if (a >= 1) and (a <= 8) then
  1096. begin
  1097. if (op = OP_ADD) then
  1098. opcode:=A_ADDQ
  1099. else
  1100. opcode:=A_SUBQ;
  1101. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1102. end
  1103. else
  1104. if not(current_settings.cputype in cpu_coldfire) then
  1105. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1106. else
  1107. { on ColdFire, ADDI/SUBI cannot act on memory
  1108. so we can only go through a register }
  1109. inherited;
  1110. end;
  1111. else begin
  1112. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1113. inherited;
  1114. end;
  1115. end;
  1116. end;
  1117. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1118. var
  1119. hreg1, hreg2: tregister;
  1120. opcode : tasmop;
  1121. opsize : topsize;
  1122. begin
  1123. opcode := topcg2tasmop[op];
  1124. if current_settings.cputype in cpu_coldfire then
  1125. opsize := S_L
  1126. else
  1127. opsize := TCGSize2OpSize[size];
  1128. case op of
  1129. OP_ADD,
  1130. OP_SUB:
  1131. begin
  1132. if current_settings.cputype in cpu_coldfire then
  1133. begin
  1134. { operation only allowed only a longword }
  1135. sign_extend(list, size, src);
  1136. sign_extend(list, size, dst);
  1137. end;
  1138. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1139. end;
  1140. OP_AND,OP_OR,
  1141. OP_SAR,OP_SHL,
  1142. OP_SHR,OP_XOR:
  1143. begin
  1144. { load to data registers }
  1145. hreg1 := force_to_dataregister(list, size, src);
  1146. hreg2 := force_to_dataregister(list, size, dst);
  1147. if current_settings.cputype in cpu_coldfire then
  1148. begin
  1149. { operation only allowed only a longword }
  1150. {!***************************************
  1151. in the case of shifts, the value to
  1152. shift by, should already be valid, so
  1153. no need to sign extend the value
  1154. !
  1155. }
  1156. if op in [OP_AND,OP_OR,OP_XOR] then
  1157. sign_extend(list, size, hreg1);
  1158. sign_extend(list, size, hreg2);
  1159. end;
  1160. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1161. { move back result into destination register }
  1162. move_if_needed(list, size, hreg2, dst);
  1163. end;
  1164. OP_DIV,
  1165. OP_IDIV :
  1166. begin
  1167. internalerror(20020816);
  1168. end;
  1169. OP_MUL,
  1170. OP_IMUL:
  1171. begin
  1172. if (current_settings.cputype <> cpu_mc68020) and
  1173. (not (current_settings.cputype in cpu_coldfire)) then
  1174. if op = OP_MUL then
  1175. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1176. else
  1177. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1178. else
  1179. begin
  1180. { 68020+ and ColdFire codepath, probably could be improved }
  1181. hreg1 := force_to_dataregister(list, size, src);
  1182. hreg2 := force_to_dataregister(list, size, dst);
  1183. sign_extend(list, size, hreg1);
  1184. sign_extend(list, size, hreg2);
  1185. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1186. { move back result into destination register }
  1187. move_if_needed(list, size, hreg2, dst);
  1188. end;
  1189. end;
  1190. OP_NEG,
  1191. OP_NOT :
  1192. begin
  1193. { if there are two operands, move the register,
  1194. since the operation will only be done on the result
  1195. register. }
  1196. if (src<>dst) then
  1197. a_load_reg_reg(list,size,size,src,dst);
  1198. hreg2 := force_to_dataregister(list, size, dst);
  1199. { coldfire only supports long version }
  1200. if current_settings.cputype in cpu_ColdFire then
  1201. sign_extend(list, size, hreg2);
  1202. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1203. { move back the result to the result register if needed }
  1204. move_if_needed(list, size, hreg2, dst);
  1205. end;
  1206. else
  1207. internalerror(20020729);
  1208. end;
  1209. end;
  1210. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1211. var
  1212. opcode : tasmop;
  1213. opsize : topsize;
  1214. href : treference;
  1215. begin
  1216. opcode := topcg2tasmop[op];
  1217. opsize := TCGSize2OpSize[size];
  1218. { on ColdFire all arithmetic operations are only possible on 32bit
  1219. and addressing modes are limited }
  1220. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1221. begin
  1222. inherited;
  1223. exit;
  1224. end;
  1225. case op of
  1226. OP_ADD,
  1227. OP_SUB :
  1228. begin
  1229. href:=ref;
  1230. fixref(list,href);
  1231. { add/sub works the same way, so have it unified here }
  1232. list.concat(taicpu.op_reg_ref(opcode, opsize, reg, href));
  1233. end;
  1234. else begin
  1235. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1236. inherited;
  1237. end;
  1238. end;
  1239. end;
  1240. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1241. l : tasmlabel);
  1242. var
  1243. hregister : tregister;
  1244. instr : taicpu;
  1245. need_temp_reg : boolean;
  1246. temp_size: topsize;
  1247. begin
  1248. need_temp_reg := false;
  1249. { plain 68000 doesn't support address registers for TST }
  1250. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1251. (a = 0) and isaddressregister(reg);
  1252. { ColdFire doesn't support address registers for CMPI }
  1253. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1254. and (a <> 0) and isaddressregister(reg));
  1255. if need_temp_reg then
  1256. begin
  1257. hregister := getintregister(list,OS_INT);
  1258. temp_size := TCGSize2OpSize[size];
  1259. if temp_size < S_W then
  1260. temp_size := S_W;
  1261. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1262. add_move_instruction(instr);
  1263. list.concat(instr);
  1264. reg := hregister;
  1265. { do sign extension if size had to be modified }
  1266. if temp_size <> TCGSize2OpSize[size] then
  1267. begin
  1268. sign_extend(list, size, reg);
  1269. size:=OS_INT;
  1270. end;
  1271. end;
  1272. if a = 0 then
  1273. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1274. else
  1275. begin
  1276. { ColdFire ISA A also needs S_L for CMPI }
  1277. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1278. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1279. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1280. default. (KB) }
  1281. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]} then
  1282. begin
  1283. sign_extend(list, size, reg);
  1284. size:=OS_INT;
  1285. end;
  1286. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1287. end;
  1288. { emit the actual jump to the label }
  1289. a_jmp_cond(list,cmp_op,l);
  1290. end;
  1291. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1292. var
  1293. tmpref: treference;
  1294. begin
  1295. { optimize for usage of TST here, so ref compares against zero, which is the
  1296. most common case by far in the RTL code at least (KB) }
  1297. if (a = 0) then
  1298. begin
  1299. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1300. tmpref:=ref;
  1301. fixref(list,tmpref);
  1302. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1303. a_jmp_cond(list,cmp_op,l);
  1304. end
  1305. else
  1306. begin
  1307. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1308. inherited;
  1309. end;
  1310. end;
  1311. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1312. begin
  1313. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c]) then
  1314. begin
  1315. sign_extend(list,size,reg1);
  1316. sign_extend(list,size,reg2);
  1317. size:=OS_INT;
  1318. end;
  1319. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1320. { emit the actual jump to the label }
  1321. a_jmp_cond(list,cmp_op,l);
  1322. end;
  1323. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1324. var
  1325. ai: taicpu;
  1326. begin
  1327. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1328. ai.is_jmp := true;
  1329. list.concat(ai);
  1330. end;
  1331. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1332. var
  1333. ai: taicpu;
  1334. begin
  1335. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1336. ai.is_jmp := true;
  1337. list.concat(ai);
  1338. end;
  1339. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1340. var
  1341. ai : taicpu;
  1342. begin
  1343. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1344. ai.SetCondition(flags_to_cond(f));
  1345. ai.is_jmp := true;
  1346. list.concat(ai);
  1347. end;
  1348. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1349. var
  1350. ai : taicpu;
  1351. hreg : tregister;
  1352. instr : taicpu;
  1353. begin
  1354. { move to a Dx register? }
  1355. if (isaddressregister(reg)) then
  1356. hreg:=getintregister(list,OS_INT)
  1357. else
  1358. hreg:=reg;
  1359. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1360. ai.SetCondition(flags_to_cond(f));
  1361. list.concat(ai);
  1362. { Scc stores a complete byte of 1s, but the compiler expects only one
  1363. bit set, so ensure this is the case }
  1364. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1365. if hreg<>reg then
  1366. begin
  1367. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1368. add_move_instruction(instr);
  1369. list.concat(instr);
  1370. end;
  1371. end;
  1372. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1373. var
  1374. helpsize : longint;
  1375. i : byte;
  1376. hregister : tregister;
  1377. iregister : tregister;
  1378. jregister : tregister;
  1379. hp1 : treference;
  1380. hp2 : treference;
  1381. hl : tasmlabel;
  1382. srcref,dstref : treference;
  1383. begin
  1384. hregister := getintregister(list,OS_INT);
  1385. { from 12 bytes movs is being used }
  1386. if ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1387. begin
  1388. srcref := source;
  1389. dstref := dest;
  1390. helpsize:=len div 4;
  1391. { move a dword x times }
  1392. for i:=1 to helpsize do
  1393. begin
  1394. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1395. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1396. inc(srcref.offset,4);
  1397. inc(dstref.offset,4);
  1398. dec(len,4);
  1399. end;
  1400. { move a word }
  1401. if len>1 then
  1402. begin
  1403. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1404. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1405. inc(srcref.offset,2);
  1406. inc(dstref.offset,2);
  1407. dec(len,2);
  1408. end;
  1409. { move a single byte }
  1410. if len>0 then
  1411. begin
  1412. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1413. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1414. end
  1415. end
  1416. else
  1417. begin
  1418. iregister:=getaddressregister(list);
  1419. jregister:=getaddressregister(list);
  1420. { reference for move (An)+,(An)+ }
  1421. reference_reset(hp1,source.alignment);
  1422. hp1.base := iregister; { source register }
  1423. hp1.direction := dir_inc;
  1424. reference_reset(hp2,dest.alignment);
  1425. hp2.base := jregister;
  1426. hp2.direction := dir_inc;
  1427. { iregister = source }
  1428. { jregister = destination }
  1429. a_loadaddr_ref_reg(list,source,iregister);
  1430. a_loadaddr_ref_reg(list,dest,jregister);
  1431. { double word move only on 68020+ machines }
  1432. { because of possible alignment problems }
  1433. { use fast loop mode }
  1434. if (current_settings.cputype=cpu_MC68020) then
  1435. begin
  1436. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1437. helpsize := len - len mod 4;
  1438. len := len mod 4;
  1439. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1440. current_asmdata.getjumplabel(hl);
  1441. a_label(list,hl);
  1442. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1443. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1444. if len > 1 then
  1445. begin
  1446. dec(len,2);
  1447. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1448. end;
  1449. if len = 1 then
  1450. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1451. end
  1452. else
  1453. begin
  1454. { Fast 68010 loop mode with no possible alignment problems }
  1455. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1456. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1457. current_asmdata.getjumplabel(hl);
  1458. a_label(list,hl);
  1459. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1460. if current_settings.cputype in cpu_coldfire then
  1461. begin
  1462. { Coldfire does not support DBRA }
  1463. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1464. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1465. end
  1466. else
  1467. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1468. end;
  1469. end;
  1470. end;
  1471. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1472. var
  1473. hl : tasmlabel;
  1474. ai : taicpu;
  1475. cond : TAsmCond;
  1476. begin
  1477. if not(cs_check_overflow in current_settings.localswitches) then
  1478. exit;
  1479. current_asmdata.getjumplabel(hl);
  1480. if not ((def.typ=pointerdef) or
  1481. ((def.typ=orddef) and
  1482. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1483. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1484. cond:=C_VC
  1485. else
  1486. cond:=C_CC;
  1487. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1488. ai.SetCondition(cond);
  1489. ai.is_jmp:=true;
  1490. list.concat(ai);
  1491. a_call_name(list,'FPC_OVERFLOW',false);
  1492. a_label(list,hl);
  1493. end;
  1494. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1495. begin
  1496. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1497. However, a LINK seems faster than two moves on everything from 68000
  1498. to '060, so the two move branch here was dropped. (KB) }
  1499. if not nostackframe then
  1500. begin
  1501. { size can't be negative }
  1502. if (localsize < 0) then
  1503. internalerror(2006122601);
  1504. if (localsize > high(smallint)) then
  1505. begin
  1506. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1507. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1508. end
  1509. else
  1510. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1511. end;
  1512. end;
  1513. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1514. var
  1515. r,hregister : TRegister;
  1516. ref : TReference;
  1517. ref2: TReference;
  1518. begin
  1519. if not nostackframe then
  1520. begin
  1521. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1522. { if parasize is less than zero here, we probably have a cdecl function.
  1523. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1524. 68k GCC uses two different methods to free the stack, depending if the target
  1525. architecture supports RTD or not, and one does callee side, the other does
  1526. caller side free, which looks like a PITA to support. We have to figure this
  1527. out later. More info welcomed. (KB) }
  1528. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1529. begin
  1530. if current_settings.cputype=cpu_mc68020 then
  1531. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1532. else
  1533. begin
  1534. { We must pull the PC Counter from the stack, before }
  1535. { restoring the stack pointer, otherwise the PC would }
  1536. { point to nowhere! }
  1537. { Instead of doing a slow copy of the return address while trying }
  1538. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1539. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1540. { return to the caller with the paras freed. (KB) }
  1541. hregister:=NR_A0;
  1542. cg.a_reg_alloc(list,hregister);
  1543. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1544. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1545. { instead of using a postincrement above (which also writes the }
  1546. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1547. { below then take that size into account as well, so SP reg is only }
  1548. { written once (KB) }
  1549. parasize:=parasize+4;
  1550. r:=NR_SP;
  1551. { can we do a quick addition ... }
  1552. if (parasize < 9) then
  1553. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1554. else { nope ... }
  1555. begin
  1556. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4);
  1557. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1558. end;
  1559. reference_reset_base(ref,hregister,0,4);
  1560. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1561. end;
  1562. end
  1563. else
  1564. list.concat(taicpu.op_none(A_RTS,S_NO));
  1565. end
  1566. else
  1567. begin
  1568. list.concat(taicpu.op_none(A_RTS,S_NO));
  1569. end;
  1570. { Routines with the poclearstack flag set use only a ret.
  1571. also routines with parasize=0 }
  1572. { TODO: figure out if these are still relevant to us (KB) }
  1573. (*
  1574. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1575. begin
  1576. { complex return values are removed from stack in C code PM }
  1577. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1578. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1579. else
  1580. list.concat(taicpu.op_none(A_RTS,S_NO));
  1581. end
  1582. else if (parasize=0) then
  1583. begin
  1584. list.concat(taicpu.op_none(A_RTS,S_NO));
  1585. end
  1586. else
  1587. *)
  1588. end;
  1589. procedure tcg68k.g_save_registers(list:TAsmList);
  1590. var
  1591. dataregs: tcpuregisterset;
  1592. addrregs: tcpuregisterset;
  1593. href : treference;
  1594. hreg : tregister;
  1595. size : longint;
  1596. r : integer;
  1597. begin
  1598. { The code generated by the section below, particularly the movem.l
  1599. instruction is known to cause an issue when compiled by some GNU
  1600. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1601. when you run into this problem, just call inherited here instead
  1602. to skip the movem.l generation. But better just use working GNU
  1603. AS version instead. (KB) }
  1604. dataregs:=[];
  1605. addrregs:=[];
  1606. { calculate temp. size }
  1607. size:=0;
  1608. hreg:=NR_NO;
  1609. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1610. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1611. begin
  1612. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1613. inc(size,sizeof(aint));
  1614. dataregs:=dataregs + [saved_standard_registers[r]];
  1615. end;
  1616. if uses_registers(R_ADDRESSREGISTER) then
  1617. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1618. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1619. begin
  1620. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1621. inc(size,sizeof(aint));
  1622. addrregs:=addrregs + [saved_address_registers[r]];
  1623. end;
  1624. { 68k has no MM registers }
  1625. if uses_registers(R_MMREGISTER) then
  1626. internalerror(2014030201);
  1627. if size>0 then
  1628. begin
  1629. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1630. include(current_procinfo.flags,pi_has_saved_regs);
  1631. { Copy registers to temp }
  1632. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1633. href:=current_procinfo.save_regs_ref;
  1634. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1635. begin
  1636. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1637. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1638. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1639. end;
  1640. if size = sizeof(aint) then
  1641. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1642. else
  1643. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,href));
  1644. end;
  1645. end;
  1646. procedure tcg68k.g_restore_registers(list:TAsmList);
  1647. var
  1648. dataregs: tcpuregisterset;
  1649. addrregs: tcpuregisterset;
  1650. href : treference;
  1651. r : integer;
  1652. hreg : tregister;
  1653. size : longint;
  1654. begin
  1655. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1656. dataregs:=[];
  1657. addrregs:=[];
  1658. if not(pi_has_saved_regs in current_procinfo.flags) then
  1659. exit;
  1660. { Copy registers from temp }
  1661. size:=0;
  1662. hreg:=NR_NO;
  1663. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1664. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1665. begin
  1666. inc(size,sizeof(aint));
  1667. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1668. { Allocate register so the optimizer does not remove the load }
  1669. a_reg_alloc(list,hreg);
  1670. dataregs:=dataregs + [saved_standard_registers[r]];
  1671. end;
  1672. if uses_registers(R_ADDRESSREGISTER) then
  1673. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1674. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1675. begin
  1676. inc(size,sizeof(aint));
  1677. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1678. { Allocate register so the optimizer does not remove the load }
  1679. a_reg_alloc(list,hreg);
  1680. addrregs:=addrregs + [saved_address_registers[r]];
  1681. end;
  1682. { 68k has no MM registers }
  1683. if uses_registers(R_MMREGISTER) then
  1684. internalerror(2014030202);
  1685. { Restore registers from temp }
  1686. href:=current_procinfo.save_regs_ref;
  1687. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1688. begin
  1689. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1690. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1691. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1692. end;
  1693. if size = sizeof(aint) then
  1694. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1695. else
  1696. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs));
  1697. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1698. end;
  1699. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1700. begin
  1701. case _newsize of
  1702. OS_S16, OS_16:
  1703. case _oldsize of
  1704. OS_S8:
  1705. begin { 8 -> 16 bit sign extend }
  1706. if (isaddressregister(reg)) then
  1707. internalerror(2014031201);
  1708. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1709. end;
  1710. OS_8: { 8 -> 16 bit zero extend }
  1711. begin
  1712. if (current_settings.cputype in cpu_coldfire) then
  1713. { ColdFire has no ANDI.W }
  1714. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1715. else
  1716. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1717. end;
  1718. end;
  1719. OS_S32, OS_32:
  1720. case _oldsize of
  1721. OS_S8:
  1722. begin { 8 -> 32 bit sign extend }
  1723. if (isaddressregister(reg)) then
  1724. internalerror(2014031202);
  1725. if (current_settings.cputype = cpu_MC68000) then
  1726. begin
  1727. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1728. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1729. end
  1730. else
  1731. begin
  1732. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1733. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1734. end;
  1735. end;
  1736. OS_8: { 8 -> 32 bit zero extend }
  1737. begin
  1738. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1739. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1740. end;
  1741. OS_S16: { 16 -> 32 bit sign extend }
  1742. begin
  1743. if (isaddressregister(reg)) then
  1744. internalerror(2014031203);
  1745. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1746. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1747. end;
  1748. OS_16:
  1749. begin
  1750. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1751. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1752. end;
  1753. end;
  1754. end; { otherwise the size is already correct }
  1755. end;
  1756. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1757. begin
  1758. sign_extend(list, _oldsize, OS_INT, reg);
  1759. end;
  1760. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1761. var
  1762. ai : taicpu;
  1763. begin
  1764. if cond=OC_None then
  1765. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1766. else
  1767. begin
  1768. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1769. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1770. end;
  1771. ai.is_jmp:=true;
  1772. list.concat(ai);
  1773. end;
  1774. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1775. operations on an address register. if the register is a dataregister anyway, it
  1776. just returns it untouched.}
  1777. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1778. var
  1779. scratch_reg: TRegister;
  1780. instr: Taicpu;
  1781. begin
  1782. if isaddressregister(reg) then
  1783. begin
  1784. scratch_reg:=getintregister(list,OS_INT);
  1785. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1786. add_move_instruction(instr);
  1787. list.concat(instr);
  1788. result:=scratch_reg;
  1789. end
  1790. else
  1791. result:=reg;
  1792. end;
  1793. { moves source register to destination register, if the two are not the same. can be used in pair
  1794. with force_to_dataregister() }
  1795. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1796. var
  1797. instr: Taicpu;
  1798. begin
  1799. if (src <> dest) then
  1800. begin
  1801. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1802. add_move_instruction(instr);
  1803. list.concat(instr);
  1804. end;
  1805. end;
  1806. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1807. var
  1808. hsym : tsym;
  1809. href : treference;
  1810. paraloc : Pcgparalocation;
  1811. begin
  1812. { calculate the parameter info for the procdef }
  1813. procdef.init_paraloc_info(callerside);
  1814. hsym:=tsym(procdef.parast.Find('self'));
  1815. if not(assigned(hsym) and
  1816. (hsym.typ=paravarsym)) then
  1817. internalerror(2013100702);
  1818. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1819. while paraloc<>nil do
  1820. with paraloc^ do
  1821. begin
  1822. case loc of
  1823. LOC_REGISTER:
  1824. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1825. LOC_REFERENCE:
  1826. begin
  1827. { offset in the wrapper needs to be adjusted for the stored
  1828. return address }
  1829. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1830. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  1831. and it's probably smaller code for the majority of cases (if ioffset small, the
  1832. load will use MOVEQ) (KB) }
  1833. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  1834. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  1835. end
  1836. else
  1837. internalerror(2013100703);
  1838. end;
  1839. paraloc:=next;
  1840. end;
  1841. end;
  1842. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1843. procedure getselftoa0(offs:longint);
  1844. var
  1845. href : treference;
  1846. selfoffsetfromsp : longint;
  1847. begin
  1848. { move.l offset(%sp),%a0 }
  1849. { framepointer is pushed for nested procs }
  1850. if procdef.parast.symtablelevel>normal_function_level then
  1851. selfoffsetfromsp:=sizeof(aint)
  1852. else
  1853. selfoffsetfromsp:=0;
  1854. reference_reset_base(href,NR_SP,selfoffsetfromsp+offs,4);
  1855. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1856. end;
  1857. procedure loadvmttoa0;
  1858. var
  1859. href : treference;
  1860. begin
  1861. { move.l (%a0),%a0 ; load vmt}
  1862. reference_reset_base(href,NR_A0,0,4);
  1863. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1864. end;
  1865. procedure op_ona0methodaddr;
  1866. var
  1867. href : treference;
  1868. begin
  1869. if (procdef.extnumber=$ffff) then
  1870. Internalerror(2013100701);
  1871. reference_reset_base(href,NR_A0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  1872. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,NR_A0));
  1873. reference_reset_base(href,NR_A0,0,4);
  1874. list.concat(taicpu.op_ref(A_JMP,S_NO,href));
  1875. end;
  1876. var
  1877. make_global : boolean;
  1878. begin
  1879. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1880. Internalerror(200006137);
  1881. if not assigned(procdef.struct) or
  1882. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1883. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1884. Internalerror(200006138);
  1885. if procdef.owner.symtabletype<>ObjectSymtable then
  1886. Internalerror(200109191);
  1887. make_global:=false;
  1888. if (not current_module.is_unit) or
  1889. create_smartlink or
  1890. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1891. make_global:=true;
  1892. if make_global then
  1893. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1894. else
  1895. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1896. { set param1 interface to self }
  1897. g_adjust_self_value(list,procdef,ioffset);
  1898. { case 4 }
  1899. if (po_virtualmethod in procdef.procoptions) and
  1900. not is_objectpascal_helper(procdef.struct) then
  1901. begin
  1902. getselftoa0(4);
  1903. loadvmttoa0;
  1904. op_ona0methodaddr;
  1905. end
  1906. { case 0 }
  1907. else
  1908. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1909. List.concat(Tai_symbol_end.Createname(labelname));
  1910. end;
  1911. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1912. begin
  1913. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  1914. end;
  1915. {****************************************************************************}
  1916. { TCG64F68K }
  1917. {****************************************************************************}
  1918. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1919. var
  1920. opcode : tasmop;
  1921. xopcode : tasmop;
  1922. instr : taicpu;
  1923. begin
  1924. opcode := topcg2tasmop[op];
  1925. xopcode := topcg2tasmopx[op];
  1926. case op of
  1927. OP_ADD,OP_SUB:
  1928. begin
  1929. { if one of these three registers is an address
  1930. register, we'll really get into problems! }
  1931. if isaddressregister(regdst.reglo) or
  1932. isaddressregister(regdst.reghi) or
  1933. isaddressregister(regsrc.reghi) then
  1934. internalerror(2014030101);
  1935. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  1936. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  1937. end;
  1938. OP_AND,OP_OR:
  1939. begin
  1940. { at least one of the registers must be a data register }
  1941. if (isaddressregister(regdst.reglo) and
  1942. isaddressregister(regsrc.reglo)) or
  1943. (isaddressregister(regsrc.reghi) and
  1944. isaddressregister(regdst.reghi)) then
  1945. internalerror(2014030102);
  1946. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1947. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1948. end;
  1949. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1950. OP_IDIV,OP_DIV,
  1951. OP_IMUL,OP_MUL:
  1952. internalerror(2002081701);
  1953. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1954. OP_SAR,OP_SHL,OP_SHR:
  1955. internalerror(2002081702);
  1956. OP_XOR:
  1957. begin
  1958. if isaddressregister(regdst.reglo) or
  1959. isaddressregister(regsrc.reglo) or
  1960. isaddressregister(regsrc.reghi) or
  1961. isaddressregister(regdst.reghi) then
  1962. internalerror(2014030103);
  1963. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1964. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1965. end;
  1966. OP_NEG,OP_NOT:
  1967. begin
  1968. if isaddressregister(regdst.reglo) or
  1969. isaddressregister(regdst.reghi) then
  1970. internalerror(2014030104);
  1971. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1972. cg.add_move_instruction(instr);
  1973. list.concat(instr);
  1974. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1975. cg.add_move_instruction(instr);
  1976. list.concat(instr);
  1977. if (op = OP_NOT) then
  1978. xopcode:=opcode;
  1979. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  1980. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  1981. end;
  1982. end; { end case }
  1983. end;
  1984. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  1985. var
  1986. tempref : treference;
  1987. begin
  1988. case op of
  1989. OP_NEG,OP_NOT:
  1990. begin
  1991. a_load64_ref_reg(list,ref,reg);
  1992. a_op64_reg_reg(list,op,size,reg,reg);
  1993. end;
  1994. OP_AND,OP_OR:
  1995. begin
  1996. tempref:=ref;
  1997. tcg68k(cg).fixref(list,tempref);
  1998. inc(tempref.offset,4);
  1999. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reglo));
  2000. dec(tempref.offset,4);
  2001. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reghi));
  2002. end;
  2003. else
  2004. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2005. high dword, although low dword can still be handled directly. }
  2006. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2007. end;
  2008. end;
  2009. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2010. var
  2011. lowvalue : cardinal;
  2012. highvalue : cardinal;
  2013. opcode : tasmop;
  2014. xopcode : tasmop;
  2015. hreg : tregister;
  2016. begin
  2017. { is it optimized out ? }
  2018. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2019. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2020. exit; }
  2021. lowvalue := cardinal(value);
  2022. highvalue := value shr 32;
  2023. opcode := topcg2tasmop[op];
  2024. xopcode := topcg2tasmopx[op];
  2025. { the destination registers must be data registers }
  2026. if isaddressregister(regdst.reglo) or
  2027. isaddressregister(regdst.reghi) then
  2028. internalerror(2014030105);
  2029. case op of
  2030. OP_ADD,OP_SUB:
  2031. begin
  2032. hreg:=cg.getintregister(list,OS_INT);
  2033. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2034. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2035. { don't use cg.a_op_const_reg() here, because a possible optimized
  2036. ADDQ/SUBQ wouldn't set the eXtend bit }
  2037. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2038. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2039. end;
  2040. OP_AND,OP_OR,OP_XOR:
  2041. begin
  2042. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2043. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2044. end;
  2045. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2046. OP_IDIV,OP_DIV,
  2047. OP_IMUL,OP_MUL:
  2048. internalerror(2002081701);
  2049. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2050. OP_SAR,OP_SHL,OP_SHR:
  2051. internalerror(2002081702);
  2052. { these should have been handled already by earlier passes }
  2053. OP_NOT,OP_NEG:
  2054. internalerror(2012110403);
  2055. end; { end case }
  2056. end;
  2057. procedure create_codegen;
  2058. begin
  2059. cg := tcg68k.create;
  2060. cg64 :=tcg64f68k.create;
  2061. end;
  2062. end.