cgcpu.pas 85 KB

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