cgcpu.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. {$WARNINGS OFF}
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,globtype,
  23. aasmbase,aasmtai,aasmcpu,
  24. cpubase,cpuinfo,
  25. parabase,cpupara,
  26. node,symconst,symtype,
  27. cgutils,cg64f32;
  28. type
  29. tcg68k = class(tcg)
  30. procedure init_register_allocators;override;
  31. procedure done_register_allocators;override;
  32. procedure a_call_name(list : taasmoutput;const s : string);override;
  33. procedure a_call_reg(list : taasmoutput;reg : tregister);override;
  34. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aint;register : tregister);override;
  35. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  36. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  37. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  38. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  39. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  40. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  41. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  42. procedure a_loadmm_reg_reg(list: taasmoutput;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle); override;
  43. procedure a_loadmm_ref_reg(list: taasmoutput;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  44. procedure a_loadmm_reg_ref(list: taasmoutput;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  45. procedure a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle); override;
  46. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: tcgsize; a: aint; reg: TRegister); override;
  47. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); override;
  48. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  49. l : tasmlabel);override;
  50. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  51. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  52. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  53. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); override;
  54. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);override;
  55. { generates overflow checking code for a node }
  56. procedure g_overflowcheck(list: taasmoutput; const l:tlocation; def:tdef); override;
  57. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);override;
  58. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  59. procedure g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);override;
  60. // procedure g_restore_frame_pointer(list : taasmoutput);override;
  61. // procedure g_return_from_proc(list : taasmoutput;parasize : aint);override;
  62. procedure g_restore_standard_registers(list:Taasmoutput);override;
  63. procedure g_save_standard_registers(list:Taasmoutput);override;
  64. // procedure g_save_all_registers(list : taasmoutput);override;
  65. // procedure g_restore_all_registers(list : taasmoutput;const funcretparaloc:TCGPara);override;
  66. protected
  67. function fixref(list: taasmoutput; var ref: treference): boolean;
  68. private
  69. { # Sign or zero extend the register to a full 32-bit value.
  70. The new value is left in the same register.
  71. }
  72. procedure sign_extend(list: taasmoutput;_oldsize : tcgsize; reg: tregister);
  73. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  74. end;
  75. tcg64f68k = class(tcg64f32)
  76. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  77. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  78. end;
  79. { This function returns true if the reference+offset is valid.
  80. Otherwise extra code must be generated to solve the reference.
  81. On the m68k, this verifies that the reference is valid
  82. (e.g : if index register is used, then the max displacement
  83. is 256 bytes, if only base is used, then max displacement
  84. is 32K
  85. }
  86. function isvalidrefoffset(const ref: treference): boolean;
  87. const
  88. TCGSize2OpSize: Array[tcgsize] of topsize =
  89. (S_NO,S_B,S_W,S_L,S_L,S_NO,S_B,S_W,S_L,S_L,S_NO,
  90. S_FS,S_FD,S_FX,S_NO,S_NO,
  91. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  92. implementation
  93. uses
  94. globals,verbose,systems,cutils,
  95. symdef,symsym,defutil,paramgr,procinfo,
  96. rgobj,tgobj,rgcpu;
  97. const
  98. { opcode table lookup }
  99. topcg2tasmop: Array[topcg] of tasmop =
  100. (
  101. A_NONE,
  102. A_ADD,
  103. A_AND,
  104. A_DIVU,
  105. A_DIVS,
  106. A_MULS,
  107. A_MULU,
  108. A_NEG,
  109. A_NOT,
  110. A_OR,
  111. A_ASR,
  112. A_LSL,
  113. A_LSR,
  114. A_SUB,
  115. A_EOR
  116. );
  117. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  118. (
  119. C_NONE,
  120. C_EQ,
  121. C_GT,
  122. C_LT,
  123. C_GE,
  124. C_LE,
  125. C_NE,
  126. C_LS,
  127. C_CS,
  128. C_CC,
  129. C_HI
  130. );
  131. function isvalidrefoffset(const ref: treference): boolean;
  132. begin
  133. isvalidrefoffset := true;
  134. if ref.index <> NR_NO then
  135. begin
  136. if ref.base <> NR_NO then
  137. internalerror(20020814);
  138. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  139. isvalidrefoffset := false
  140. end
  141. else
  142. begin
  143. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  144. isvalidrefoffset := false;
  145. end;
  146. end;
  147. {****************************************************************************}
  148. { TCG68K }
  149. {****************************************************************************}
  150. procedure tcg68k.init_register_allocators;
  151. begin
  152. inherited init_register_allocators;
  153. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  154. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  155. first_int_imreg,[]);
  156. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  157. [RS_A0,RS_A1,RS_A2,RS_A3,RS_A4,RS_A5,RS_A6],
  158. first_addr_imreg,[]);
  159. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  160. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  161. first_fpu_imreg,[]);
  162. end;
  163. procedure tcg68k.done_register_allocators;
  164. begin
  165. rg[R_INTREGISTER].free;
  166. rg[R_FPUREGISTER].free;
  167. rg[R_ADDRESSREGISTER].free;
  168. inherited done_register_allocators;
  169. end;
  170. function tcg68k.fixref(list: taasmoutput; var ref: treference): boolean;
  171. begin
  172. result:=false;
  173. { The Coldfire and MC68020+ have extended
  174. addressing capabilities with a 32-bit
  175. displacement.
  176. }
  177. if (aktoptprocessor<>MC68000) then
  178. exit;
  179. if (ref.base<>NR_NO) then
  180. begin
  181. if (ref.index <> NR_NO) and assigned(ref.symbol) then
  182. internalerror(20020814);
  183. { base + reg }
  184. if ref.index <> NR_NO then
  185. begin
  186. { base + reg + offset }
  187. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  188. begin
  189. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,ref.base));
  190. fixref := true;
  191. ref.offset := 0;
  192. exit;
  193. end;
  194. end
  195. else
  196. { base + offset }
  197. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  198. begin
  199. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,ref.base));
  200. fixref := true;
  201. ref.offset := 0;
  202. exit;
  203. end;
  204. end;
  205. end;
  206. procedure tcg68k.a_call_name(list : taasmoutput;const s : string);
  207. begin
  208. list.concat(taicpu.op_sym(A_JSR,S_NO,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  209. end;
  210. procedure tcg68k.a_call_reg(list : taasmoutput;reg : tregister);
  211. var
  212. href : treference;
  213. begin
  214. reference_reset_base(href, reg, 0);
  215. //!!! a_call_ref(list,href);
  216. end;
  217. procedure tcg68k.a_load_const_reg(list : taasmoutput;size : tcgsize;a : aint;register : tregister);
  218. begin
  219. if getregtype(register)=R_ADDRESSREGISTER then
  220. begin
  221. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),register))
  222. end
  223. else
  224. if a = 0 then
  225. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  226. else
  227. begin
  228. if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  229. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  230. else
  231. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),register))
  232. end;
  233. end;
  234. procedure tcg68k.a_load_reg_ref(list : taasmoutput;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  235. var
  236. href : treference;
  237. begin
  238. href := ref;
  239. fixref(list,href);
  240. { move to destination reference }
  241. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[fromsize],register,href));
  242. end;
  243. procedure tcg68k.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  244. begin
  245. { move to destination register }
  246. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2));
  247. { zero/sign extend register to 32-bit }
  248. sign_extend(list, fromsize, reg2);
  249. end;
  250. procedure tcg68k.a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  251. var
  252. href : treference;
  253. begin
  254. href := ref;
  255. fixref(list,href);
  256. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],href,register));
  257. { extend the value in the register }
  258. sign_extend(list, tosize, register);
  259. end;
  260. procedure tcg68k.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  261. var
  262. href : treference;
  263. begin
  264. if getregtype(r)=R_ADDRESSREGISTER then
  265. begin
  266. internalerror(2002072901);
  267. end;
  268. href:=ref;
  269. fixref(list, href);
  270. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  271. end;
  272. procedure tcg68k.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  273. begin
  274. { in emulation mode, only 32-bit single is supported }
  275. if cs_fp_emulation in aktmoduleswitches then
  276. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2))
  277. else
  278. list.concat(taicpu.op_reg_reg(A_FMOVE,S_FD,reg1,reg2));
  279. end;
  280. procedure tcg68k.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  281. var
  282. opsize : topsize;
  283. href : treference;
  284. begin
  285. opsize := tcgsize2opsize[size];
  286. { extended is not supported, since it is not available on Coldfire }
  287. if opsize = S_FX then
  288. internalerror(20020729);
  289. href := ref;
  290. fixref(list,href);
  291. { in emulation mode, only 32-bit single is supported }
  292. if cs_fp_emulation in aktmoduleswitches then
  293. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  294. else
  295. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  296. end;
  297. procedure tcg68k.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  298. var
  299. opsize : topsize;
  300. begin
  301. opsize := tcgsize2opsize[size];
  302. { extended is not supported, since it is not available on Coldfire }
  303. if opsize = S_FX then
  304. internalerror(20020729);
  305. { in emulation mode, only 32-bit single is supported }
  306. if cs_fp_emulation in aktmoduleswitches then
  307. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  308. else
  309. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  310. end;
  311. procedure tcg68k.a_loadmm_reg_reg(list: taasmoutput;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle);
  312. begin
  313. internalerror(20020729);
  314. end;
  315. procedure tcg68k.a_loadmm_ref_reg(list: taasmoutput;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle);
  316. begin
  317. internalerror(20020729);
  318. end;
  319. procedure tcg68k.a_loadmm_reg_ref(list: taasmoutput;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle);
  320. begin
  321. internalerror(20020729);
  322. end;
  323. procedure tcg68k.a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle);
  324. begin
  325. internalerror(20020729);
  326. end;
  327. procedure tcg68k.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: tcgsize; a: aint; reg: TRegister);
  328. var
  329. scratch_reg : tregister;
  330. scratch_reg2: tregister;
  331. opcode : tasmop;
  332. r,r2 : Tregister;
  333. begin
  334. { need to emit opcode? }
  335. if optimize_op_const_reg(list, op, a, reg) then
  336. exit;
  337. opcode := topcg2tasmop[op];
  338. case op of
  339. OP_ADD :
  340. begin
  341. if (a >= 1) and (a <= 8) then
  342. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,a, reg))
  343. else
  344. begin
  345. { all others, including coldfire }
  346. list.concat(taicpu.op_const_reg(A_ADD,S_L,a, reg));
  347. end;
  348. end;
  349. OP_AND,
  350. OP_OR:
  351. begin
  352. list.concat(taicpu.op_const_reg(topcg2tasmop[op],S_L,longint(a), reg));
  353. end;
  354. OP_DIV :
  355. begin
  356. internalerror(20020816);
  357. end;
  358. OP_IDIV :
  359. begin
  360. internalerror(20020816);
  361. end;
  362. OP_IMUL :
  363. begin
  364. if aktoptprocessor = MC68000 then
  365. begin
  366. r:=NR_D0;
  367. r2:=NR_D1;
  368. cg.getcpuregister(list,NR_D0);
  369. cg.getcpuregister(list,NR_D1);
  370. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, r));
  371. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, r2));
  372. cg.a_call_name(list,'FPC_MUL_LONGINT');
  373. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg));
  374. cg.ungetcpuregister(list,r);
  375. cg.ungetcpuregister(list,r2);
  376. end
  377. else
  378. begin
  379. if (isaddressregister(reg)) then
  380. begin
  381. scratch_reg := getintregister(list,OS_INT);
  382. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  383. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,scratch_reg));
  384. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  385. end
  386. else
  387. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,reg));
  388. end;
  389. end;
  390. OP_MUL :
  391. begin
  392. if aktoptprocessor = MC68000 then
  393. begin
  394. r:=NR_D0;
  395. r2:=NR_D1;
  396. cg.getcpuregister(list,NR_D0);
  397. cg.getcpuregister(list,NR_D1);
  398. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, r));
  399. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, r2));
  400. cg.a_call_name(list,'FPC_MUL_LONGWORD');
  401. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg));
  402. cg.ungetcpuregister(list,r);
  403. cg.ungetcpuregister(list,r2);
  404. end
  405. else
  406. begin
  407. if (isaddressregister(reg)) then
  408. begin
  409. scratch_reg := getintregister(list,OS_INT);
  410. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  411. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,scratch_reg));
  412. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  413. end
  414. else
  415. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,reg));
  416. end;
  417. end;
  418. OP_SAR,
  419. OP_SHL,
  420. OP_SHR :
  421. begin
  422. if (a >= 1) and (a <= 8) then
  423. begin
  424. { now allowed to shift an address register }
  425. if (isaddressregister(reg)) then
  426. begin
  427. scratch_reg := getintregister(list,OS_INT);
  428. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  429. list.concat(taicpu.op_const_reg(opcode,S_L,a, scratch_reg));
  430. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  431. end
  432. else
  433. list.concat(taicpu.op_const_reg(opcode,S_L,a, reg));
  434. end
  435. else
  436. begin
  437. { we must load the data into a register ... :() }
  438. scratch_reg := cg.getintregister(list,OS_INT);
  439. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, scratch_reg));
  440. { again... since shifting with address register is not allowed }
  441. if (isaddressregister(reg)) then
  442. begin
  443. scratch_reg2 := cg.getintregister(list,OS_INT);
  444. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg2));
  445. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, scratch_reg2));
  446. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg2,reg));
  447. end
  448. else
  449. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, reg));
  450. end;
  451. end;
  452. OP_SUB :
  453. begin
  454. if (a >= 1) and (a <= 8) then
  455. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,a,reg))
  456. else
  457. begin
  458. { all others, including coldfire }
  459. list.concat(taicpu.op_const_reg(A_SUB,S_L,a, reg));
  460. end;
  461. end;
  462. OP_XOR :
  463. Begin
  464. list.concat(taicpu.op_const_reg(A_EORI,S_L,a, reg));
  465. end;
  466. else
  467. internalerror(20020729);
  468. end;
  469. end;
  470. procedure tcg68k.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister);
  471. var
  472. hreg1,hreg2,r,r2: tregister;
  473. begin
  474. case op of
  475. OP_ADD :
  476. begin
  477. if aktoptprocessor = ColdFire then
  478. begin
  479. { operation only allowed only a longword }
  480. sign_extend(list, size, reg1);
  481. sign_extend(list, size, reg2);
  482. list.concat(taicpu.op_reg_reg(A_ADD,S_L,reg1, reg2));
  483. end
  484. else
  485. begin
  486. list.concat(taicpu.op_reg_reg(A_ADD,TCGSize2OpSize[size],reg1, reg2));
  487. end;
  488. end;
  489. OP_AND,OP_OR,
  490. OP_SAR,OP_SHL,
  491. OP_SHR,OP_SUB,OP_XOR :
  492. begin
  493. { load to data registers }
  494. if (isaddressregister(reg1)) then
  495. begin
  496. hreg1 := getintregister(list,OS_INT);
  497. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  498. end
  499. else
  500. hreg1 := reg1;
  501. if (isaddressregister(reg2)) then
  502. begin
  503. hreg2:= getintregister(list,OS_INT);
  504. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  505. end
  506. else
  507. hreg2 := reg2;
  508. if aktoptprocessor = ColdFire then
  509. begin
  510. { operation only allowed only a longword }
  511. {!***************************************
  512. in the case of shifts, the value to
  513. shift by, should already be valid, so
  514. no need to sign extend the value
  515. !
  516. }
  517. if op in [OP_AND,OP_OR,OP_SUB,OP_XOR] then
  518. sign_extend(list, size, hreg1);
  519. sign_extend(list, size, hreg2);
  520. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],S_L,hreg1, hreg2));
  521. end
  522. else
  523. begin
  524. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg1, hreg2));
  525. end;
  526. { move back result into destination register }
  527. if reg2 <> hreg2 then
  528. begin
  529. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  530. end;
  531. end;
  532. OP_DIV :
  533. begin
  534. internalerror(20020816);
  535. end;
  536. OP_IDIV :
  537. begin
  538. internalerror(20020816);
  539. end;
  540. OP_IMUL :
  541. begin
  542. sign_extend(list, size,reg1);
  543. sign_extend(list, size,reg2);
  544. if aktoptprocessor = MC68000 then
  545. begin
  546. r:=NR_D0;
  547. r2:=NR_D1;
  548. cg.getcpuregister(list,NR_D0);
  549. cg.getcpuregister(list,NR_D1);
  550. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1, r));
  551. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2, r2));
  552. cg.a_call_name(list,'FPC_MUL_LONGINT');
  553. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg2));
  554. cg.ungetcpuregister(list,r);
  555. cg.ungetcpuregister(list,r2);
  556. end
  557. else
  558. begin
  559. // writeln('doing 68020');
  560. if (isaddressregister(reg1)) then
  561. hreg1 := getintregister(list,OS_INT)
  562. else
  563. hreg1 := reg1;
  564. if (isaddressregister(reg2)) then
  565. hreg2:= getintregister(list,OS_INT)
  566. else
  567. hreg2 := reg2;
  568. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  569. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  570. list.concat(taicpu.op_reg_reg(A_MULS,S_L,reg1,reg2));
  571. { move back result into destination register }
  572. if reg2 <> hreg2 then
  573. begin
  574. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  575. end;
  576. end;
  577. end;
  578. OP_MUL :
  579. begin
  580. sign_extend(list, size,reg1);
  581. sign_extend(list, size,reg2);
  582. if aktoptprocessor = MC68000 then
  583. begin
  584. r:=NR_D0;
  585. r2:=NR_D1;
  586. cg.getcpuregister(list,NR_D0);
  587. cg.getcpuregister(list,NR_D1);
  588. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1, r));
  589. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2, r2));
  590. cg.a_call_name(list,'FPC_MUL_LONGWORD');
  591. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg2));
  592. cg.ungetcpuregister(list,r);
  593. cg.ungetcpuregister(list,r2);
  594. end
  595. else
  596. begin
  597. if (isaddressregister(reg1)) then
  598. begin
  599. hreg1 := cg.getintregister(list,OS_INT);
  600. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  601. end
  602. else
  603. hreg1 := reg1;
  604. if (isaddressregister(reg2)) then
  605. begin
  606. hreg2:= cg.getintregister(list,OS_INT);
  607. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  608. end
  609. else
  610. hreg2 := reg2;
  611. list.concat(taicpu.op_reg_reg(A_MULU,S_L,reg1,reg2));
  612. { move back result into destination register }
  613. if reg2<>hreg2 then
  614. begin
  615. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  616. end;
  617. end;
  618. end;
  619. OP_NEG,
  620. OP_NOT :
  621. Begin
  622. { if there are two operands, move the register,
  623. since the operation will only be done on the result
  624. register.
  625. }
  626. if reg1 <> NR_NO then
  627. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,reg1,reg2);
  628. if (isaddressregister(reg2)) then
  629. begin
  630. hreg2 := getintregister(list,OS_INT);
  631. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  632. end
  633. else
  634. hreg2 := reg2;
  635. { coldfire only supports long version }
  636. if aktoptprocessor = ColdFire then
  637. begin
  638. sign_extend(list, size,hreg2);
  639. list.concat(taicpu.op_reg(topcg2tasmop[op],S_L,hreg2));
  640. end
  641. else
  642. begin
  643. list.concat(taicpu.op_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg2));
  644. end;
  645. if reg2 <> hreg2 then
  646. begin
  647. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  648. end;
  649. end;
  650. else
  651. internalerror(20020729);
  652. end;
  653. end;
  654. procedure tcg68k.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  655. l : tasmlabel);
  656. var
  657. hregister : tregister;
  658. begin
  659. if a = 0 then
  660. begin
  661. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg));
  662. end
  663. else
  664. begin
  665. if (aktoptprocessor = ColdFire) then
  666. begin
  667. {
  668. only longword comparison is supported,
  669. and only on data registers.
  670. }
  671. hregister := getintregister(list,OS_INT);
  672. { always move to a data register }
  673. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,hregister));
  674. { sign/zero extend the register }
  675. sign_extend(list, size,hregister);
  676. list.concat(taicpu.op_const_reg(A_CMPI,S_L,a,hregister));
  677. end
  678. else
  679. begin
  680. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  681. end;
  682. end;
  683. { emit the actual jump to the label }
  684. a_jmp_cond(list,cmp_op,l);
  685. end;
  686. procedure tcg68k.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  687. begin
  688. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  689. { emit the actual jump to the label }
  690. a_jmp_cond(list,cmp_op,l);
  691. end;
  692. procedure tcg68k.a_jmp_always(list : taasmoutput;l: tasmlabel);
  693. var
  694. ai: taicpu;
  695. begin
  696. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  697. ai.is_jmp := true;
  698. list.concat(ai);
  699. end;
  700. procedure tcg68k.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  701. var
  702. ai : taicpu;
  703. begin
  704. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  705. ai.SetCondition(flags_to_cond(f));
  706. ai.is_jmp := true;
  707. list.concat(ai);
  708. end;
  709. procedure tcg68k.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  710. var
  711. ai : taicpu;
  712. hreg : tregister;
  713. begin
  714. { move to a Dx register? }
  715. if (isaddressregister(reg)) then
  716. begin
  717. hreg := getintregister(list,OS_INT);
  718. a_load_const_reg(list,size,0,hreg);
  719. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  720. ai.SetCondition(flags_to_cond(f));
  721. list.concat(ai);
  722. if (aktoptprocessor = ColdFire) then
  723. begin
  724. { neg.b does not exist on the Coldfire
  725. so we need to sign extend the value
  726. before doing a neg.l
  727. }
  728. list.concat(taicpu.op_reg(A_EXTB,S_L,hreg));
  729. list.concat(taicpu.op_reg(A_NEG,S_L,hreg));
  730. end
  731. else
  732. begin
  733. list.concat(taicpu.op_reg(A_NEG,S_B,hreg));
  734. end;
  735. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg));
  736. end
  737. else
  738. begin
  739. a_load_const_reg(list,size,0,reg);
  740. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  741. ai.SetCondition(flags_to_cond(f));
  742. list.concat(ai);
  743. if (aktoptprocessor = ColdFire) then
  744. begin
  745. { neg.b does not exist on the Coldfire
  746. so we need to sign extend the value
  747. before doing a neg.l
  748. }
  749. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  750. list.concat(taicpu.op_reg(A_NEG,S_L,reg));
  751. end
  752. else
  753. begin
  754. list.concat(taicpu.op_reg(A_NEG,S_B,reg));
  755. end;
  756. end;
  757. end;
  758. procedure tcg68k.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);
  759. var
  760. helpsize : longint;
  761. i : byte;
  762. reg8,reg32 : tregister;
  763. swap : boolean;
  764. hregister : tregister;
  765. iregister : tregister;
  766. jregister : tregister;
  767. hp1 : treference;
  768. hp2 : treference;
  769. hl : tasmlabel;
  770. hl2: tasmlabel;
  771. popaddress : boolean;
  772. srcref,dstref : treference;
  773. begin
  774. popaddress := false;
  775. // writeln('concatcopy:',len);
  776. { this should never occur }
  777. if len > 65535 then
  778. internalerror(0);
  779. hregister := getintregister(list,OS_INT);
  780. // if delsource then
  781. // reference_release(list,source);
  782. { from 12 bytes movs is being used }
  783. if {(not loadref) and} ((len<=8) or (not(cs_littlesize in aktglobalswitches) and (len<=12))) then
  784. begin
  785. srcref := source;
  786. dstref := dest;
  787. helpsize:=len div 4;
  788. { move a dword x times }
  789. for i:=1 to helpsize do
  790. begin
  791. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  792. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  793. inc(srcref.offset,4);
  794. inc(dstref.offset,4);
  795. dec(len,4);
  796. end;
  797. { move a word }
  798. if len>1 then
  799. begin
  800. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  801. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  802. inc(srcref.offset,2);
  803. inc(dstref.offset,2);
  804. dec(len,2);
  805. end;
  806. { move a single byte }
  807. if len>0 then
  808. begin
  809. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  810. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  811. end
  812. end
  813. else
  814. begin
  815. iregister:=getaddressregister(list);
  816. jregister:=getaddressregister(list);
  817. { reference for move (An)+,(An)+ }
  818. reference_reset(hp1);
  819. hp1.base := iregister; { source register }
  820. hp1.direction := dir_inc;
  821. reference_reset(hp2);
  822. hp2.base := jregister;
  823. hp2.direction := dir_inc;
  824. { iregister = source }
  825. { jregister = destination }
  826. { if loadref then
  827. cg.a_load_ref_reg(list,OS_INT,OS_INT,source,iregister)
  828. else}
  829. a_loadaddr_ref_reg(list,source,iregister);
  830. a_loadaddr_ref_reg(list,dest,jregister);
  831. { double word move only on 68020+ machines }
  832. { because of possible alignment problems }
  833. { use fast loop mode }
  834. if (aktoptprocessor=MC68020) then
  835. begin
  836. helpsize := len - len mod 4;
  837. len := len mod 4;
  838. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize div 4,hregister));
  839. objectlibrary.getjumplabel(hl2);
  840. a_jmp_always(list,hl2);
  841. objectlibrary.getjumplabel(hl);
  842. a_label(list,hl);
  843. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  844. a_label(list,hl2);
  845. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  846. if len > 1 then
  847. begin
  848. dec(len,2);
  849. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  850. end;
  851. if len = 1 then
  852. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  853. end
  854. else
  855. begin
  856. { Fast 68010 loop mode with no possible alignment problems }
  857. helpsize := len;
  858. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize,hregister));
  859. objectlibrary.getjumplabel(hl2);
  860. a_jmp_always(list,hl2);
  861. objectlibrary.getjumplabel(hl);
  862. a_label(list,hl);
  863. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  864. a_label(list,hl2);
  865. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  866. end;
  867. { restore the registers that we have just used olny if they are used! }
  868. if jregister = NR_A1 then
  869. hp2.base := NR_NO;
  870. if iregister = NR_A0 then
  871. hp1.base := NR_NO;
  872. // reference_release(list,hp1);
  873. // reference_release(list,hp2);
  874. end;
  875. // if delsource then
  876. // tg.ungetiftemp(list,source);
  877. end;
  878. procedure tcg68k.g_overflowcheck(list: taasmoutput; const l:tlocation; def:tdef);
  879. begin
  880. end;
  881. procedure tcg68k.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);
  882. begin
  883. end;
  884. procedure tcg68k.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  885. var
  886. r,rsp:Tregister;
  887. ref : treference;
  888. begin
  889. r:=NR_FRAME_POINTER_REG;
  890. rsp:=NR_STACK_POINTER_REG;
  891. if localsize<>0 then
  892. begin
  893. { Not to complicate the code generator too much, and since some }
  894. { of the systems only support this format, the localsize cannot }
  895. { exceed 32K in size. }
  896. if (localsize < low(smallint)) or (localsize > high(smallint)) then
  897. CGMessage(cg_e_localsize_too_big);
  898. list.concat(taicpu.op_reg_const(A_LINK,S_W,r,-localsize));
  899. end { endif localsize <> 0 }
  900. else
  901. begin
  902. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  903. ref.direction:=dir_dec;
  904. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,r,ref));
  905. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,rsp,r));
  906. end;
  907. end;
  908. { procedure tcg68k.g_restore_frame_pointer(list : taasmoutput);
  909. var
  910. r:Tregister;
  911. begin
  912. r:=NR_FRAME_POINTER_REG;
  913. list.concat(taicpu.op_reg(A_UNLK,S_NO,r));
  914. end;
  915. }
  916. procedure tcg68k.g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);
  917. var
  918. r,hregister : tregister;
  919. ref : treference;
  920. begin
  921. // writeln('g_proc_exit');
  922. { Routines with the poclearstack flag set use only a ret.
  923. also routines with parasize=0 }
  924. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  925. begin
  926. { complex return values are removed from stack in C code PM }
  927. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  928. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  929. else
  930. list.concat(taicpu.op_none(A_RTS,S_NO));
  931. end
  932. else if (parasize=0) then
  933. begin
  934. list.concat(taicpu.op_none(A_RTS,S_NO));
  935. end
  936. else
  937. begin
  938. { return with immediate size possible here
  939. signed!
  940. RTD is not supported on the coldfire }
  941. if (aktoptprocessor=MC68020) and (parasize<$7FFF) then
  942. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  943. { manually restore the stack }
  944. else
  945. begin
  946. { We must pull the PC Counter from the stack, before }
  947. { restoring the stack pointer, otherwise the PC would }
  948. { point to nowhere! }
  949. { save the PC counter (pop it from the stack) }
  950. hregister:=NR_A3;
  951. cg.a_reg_alloc(list,hregister);
  952. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  953. ref.direction:=dir_inc;
  954. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  955. { can we do a quick addition ... }
  956. r:=NR_SP;
  957. if (parasize > 0) and (parasize < 9) then
  958. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  959. else { nope ... }
  960. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  961. { restore the PC counter (push it on the stack) }
  962. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  963. ref.direction:=dir_dec;
  964. cg.a_reg_alloc(list,hregister);
  965. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hregister,ref));
  966. list.concat(taicpu.op_none(A_RTS,S_NO));
  967. end;
  968. end;
  969. end;
  970. procedure Tcg68k.g_save_standard_registers(list:Taasmoutput);
  971. var
  972. tosave : tcpuregisterset;
  973. ref : treference;
  974. begin
  975. {!!!!!
  976. tosave:=std_saved_registers;
  977. { only save the registers which are not used and must be saved }
  978. tosave:=tosave*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  979. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  980. ref.direction:=dir_dec;
  981. if tosave<>[] then
  982. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,tosave,ref));
  983. }
  984. end;
  985. procedure Tcg68k.g_restore_standard_registers(list:Taasmoutput);
  986. var
  987. torestore : tcpuregisterset;
  988. r:Tregister;
  989. ref : treference;
  990. begin
  991. {!!!!!!!!
  992. torestore:=std_saved_registers;
  993. { should be intersected with used regs, no ? }
  994. torestore:=torestore*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  995. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  996. ref.direction:=dir_inc;
  997. if torestore<>[] then
  998. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,ref,torestore));
  999. }
  1000. end;
  1001. {
  1002. procedure tcg68k.g_save_all_registers(list : taasmoutput);
  1003. begin
  1004. end;
  1005. procedure tcg68k.g_restore_all_registers(list : taasmoutput;const funcretparaloc:TCGPara);
  1006. begin
  1007. end;
  1008. }
  1009. procedure tcg68k.sign_extend(list: taasmoutput;_oldsize : tcgsize; reg: tregister);
  1010. begin
  1011. case _oldsize of
  1012. { sign extend }
  1013. OS_S8:
  1014. begin
  1015. if (isaddressregister(reg)) then
  1016. internalerror(20020729);
  1017. if (aktoptprocessor = MC68000) then
  1018. begin
  1019. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1020. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1021. end
  1022. else
  1023. begin
  1024. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1025. end;
  1026. end;
  1027. OS_S16:
  1028. begin
  1029. if (isaddressregister(reg)) then
  1030. internalerror(20020729);
  1031. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1032. end;
  1033. { zero extend }
  1034. OS_8:
  1035. begin
  1036. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1037. end;
  1038. OS_16:
  1039. begin
  1040. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1041. end;
  1042. end; { otherwise the size is already correct }
  1043. end;
  1044. procedure tcg68k.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  1045. var
  1046. ai : taicpu;
  1047. begin
  1048. if cond=OC_None then
  1049. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1050. else
  1051. begin
  1052. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1053. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1054. end;
  1055. ai.is_jmp:=true;
  1056. list.concat(ai);
  1057. end;
  1058. {****************************************************************************}
  1059. { TCG64F68K }
  1060. {****************************************************************************}
  1061. procedure tcg64f68k.a_op64_reg_reg(list : taasmoutput;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1062. var
  1063. hreg1, hreg2 : tregister;
  1064. opcode : tasmop;
  1065. begin
  1066. // writeln('a_op64_reg_reg');
  1067. opcode := topcg2tasmop[op];
  1068. case op of
  1069. OP_ADD :
  1070. begin
  1071. { if one of these three registers is an address
  1072. register, we'll really get into problems!
  1073. }
  1074. if isaddressregister(regdst.reglo) or
  1075. isaddressregister(regdst.reghi) or
  1076. isaddressregister(regsrc.reghi) then
  1077. internalerror(20020817);
  1078. list.concat(taicpu.op_reg_reg(A_ADD,S_L,regsrc.reglo,regdst.reglo));
  1079. list.concat(taicpu.op_reg_reg(A_ADDX,S_L,regsrc.reghi,regdst.reghi));
  1080. end;
  1081. OP_AND,OP_OR :
  1082. begin
  1083. { at least one of the registers must be a data register }
  1084. if (isaddressregister(regdst.reglo) and
  1085. isaddressregister(regsrc.reglo)) or
  1086. (isaddressregister(regsrc.reghi) and
  1087. isaddressregister(regdst.reghi))
  1088. then
  1089. internalerror(20020817);
  1090. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1091. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1092. end;
  1093. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1094. OP_IDIV,OP_DIV,
  1095. OP_IMUL,OP_MUL: internalerror(2002081701);
  1096. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1097. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1098. OP_SUB:
  1099. begin
  1100. { if one of these three registers is an address
  1101. register, we'll really get into problems!
  1102. }
  1103. if isaddressregister(regdst.reglo) or
  1104. isaddressregister(regdst.reghi) or
  1105. isaddressregister(regsrc.reghi) then
  1106. internalerror(20020817);
  1107. list.concat(taicpu.op_reg_reg(A_SUB,S_L,regsrc.reglo,regdst.reglo));
  1108. list.concat(taicpu.op_reg_reg(A_SUBX,S_L,regsrc.reghi,regdst.reghi));
  1109. end;
  1110. OP_XOR:
  1111. begin
  1112. if isaddressregister(regdst.reglo) or
  1113. isaddressregister(regsrc.reglo) or
  1114. isaddressregister(regsrc.reghi) or
  1115. isaddressregister(regdst.reghi) then
  1116. internalerror(20020817);
  1117. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reglo,regdst.reglo));
  1118. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reghi,regdst.reghi));
  1119. end;
  1120. end; { end case }
  1121. end;
  1122. procedure tcg64f68k.a_op64_const_reg(list : taasmoutput;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  1123. var
  1124. lowvalue : cardinal;
  1125. highvalue : cardinal;
  1126. begin
  1127. // writeln('a_op64_const_reg');
  1128. { is it optimized out ? }
  1129. // if cg.optimize64_op_const_reg(list,op,value,reg) then
  1130. // exit;
  1131. lowvalue := cardinal(value);
  1132. highvalue:= value shr 32;
  1133. { the destination registers must be data registers }
  1134. if isaddressregister(regdst.reglo) or
  1135. isaddressregister(regdst.reghi) then
  1136. internalerror(20020817);
  1137. case op of
  1138. OP_ADD :
  1139. begin
  1140. list.concat(taicpu.op_const_reg(A_ADD,S_L,lowvalue,regdst.reglo));
  1141. list.concat(taicpu.op_const_reg(A_ADDX,S_L,highvalue,regdst.reglo));
  1142. end;
  1143. OP_AND :
  1144. begin
  1145. { should already be optimized out }
  1146. internalerror(2002081801);
  1147. end;
  1148. OP_OR :
  1149. begin
  1150. { should already be optimized out }
  1151. internalerror(2002081802);
  1152. end;
  1153. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1154. OP_IDIV,OP_DIV,
  1155. OP_IMUL,OP_MUL: internalerror(2002081701);
  1156. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1157. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1158. OP_SUB:
  1159. begin
  1160. list.concat(taicpu.op_const_reg(A_SUB,S_L,lowvalue,regdst.reglo));
  1161. list.concat(taicpu.op_const_reg(A_SUBX,S_L,highvalue,regdst.reglo));
  1162. end;
  1163. OP_XOR:
  1164. begin
  1165. list.concat(taicpu.op_const_reg(A_EOR,S_L,lowvalue,regdst.reglo));
  1166. list.concat(taicpu.op_const_reg(A_EOR,S_L,highvalue,regdst.reglo));
  1167. end;
  1168. end; { end case }
  1169. end;
  1170. begin
  1171. cg := tcg68k.create;
  1172. cg64 :=tcg64f68k.create;
  1173. end.