cgcpu.pas 87 KB

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