cgcpu.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by the FPC team
  4. This unit implements the code generator for the 680x0
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,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;delsource,loadref : boolean);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); override;
  58. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  59. procedure g_restore_frame_pointer(list : taasmoutput);override;
  60. procedure g_return_from_proc(list : taasmoutput;parasize : aint);override;
  61. procedure g_restore_standard_registers(list:Taasmoutput);override;
  62. procedure g_save_standard_registers(list:Taasmoutput);override;
  63. procedure g_save_all_registers(list : taasmoutput);override;
  64. procedure g_restore_all_registers(list : taasmoutput;const funcretparaloc:TCGPara);override;
  65. protected
  66. function fixref(list: taasmoutput; var ref: treference): boolean;
  67. private
  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: taasmoutput;_oldsize : tcgsize; reg: tregister);
  72. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  73. end;
  74. tcg64f68k = class(tcg64f32)
  75. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  76. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  77. end;
  78. { This function returns true if the reference+offset is valid.
  79. Otherwise extra code must be generated to solve the reference.
  80. On the m68k, this verifies that the reference is valid
  81. (e.g : if index register is used, then the max displacement
  82. is 256 bytes, if only base is used, then max displacement
  83. is 32K
  84. }
  85. function isvalidrefoffset(const ref: treference): boolean;
  86. const
  87. TCGSize2OpSize: Array[tcgsize] of topsize =
  88. (S_NO,S_B,S_W,S_L,S_L,S_NO,S_B,S_W,S_L,S_L,S_NO,
  89. S_FS,S_FD,S_FX,S_NO,S_NO,
  90. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  91. implementation
  92. uses
  93. globtype,globals,verbose,systems,cutils,
  94. symdef,symsym,defutil,paramgr,procinfo,
  95. rgobj,tgobj,rgcpu,
  96. cgutils;
  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. getcpuregister(list,NR_D0);
  369. 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. ungetregister(list,r);
  375. ungetregister(list,r2);
  376. end
  377. else
  378. begin
  379. if (isaddressregister(reg)) then
  380. begin
  381. scratch_reg := cg.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. cg.ungetregister(list,scratch_reg);
  386. end
  387. else
  388. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,reg));
  389. end;
  390. end;
  391. OP_MUL :
  392. Begin
  393. if aktoptprocessor = MC68000 then
  394. begin
  395. r:=NR_D0;
  396. r2:=NR_D1;
  397. getcpuregister(list,NR_D0);
  398. getcpuregister(list,NR_D1);
  399. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, r));
  400. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, r2));
  401. cg.a_call_name(list,'FPC_MUL_LONGWORD');
  402. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg));
  403. ungetregister(list,r);
  404. ungetregister(list,r2);
  405. end
  406. else
  407. begin
  408. if (isaddressregister(reg)) then
  409. begin
  410. scratch_reg := cg.getintregister(list,OS_INT);
  411. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  412. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,scratch_reg));
  413. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  414. cg.ungetregister(list,scratch_reg);
  415. end
  416. else
  417. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,reg));
  418. end;
  419. end;
  420. OP_SAR,
  421. OP_SHL,
  422. OP_SHR :
  423. Begin
  424. if (a >= 1) and (a <= 8) then
  425. begin
  426. { now allowed to shift an address register }
  427. if (isaddressregister(reg)) then
  428. begin
  429. scratch_reg := cg.getintregister(list,OS_INT);
  430. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  431. list.concat(taicpu.op_const_reg(opcode,S_L,a, scratch_reg));
  432. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  433. cg.ungetregister(list,scratch_reg);
  434. end
  435. else
  436. list.concat(taicpu.op_const_reg(opcode,S_L,a, reg));
  437. end
  438. else
  439. begin
  440. { we must load the data into a register ... :() }
  441. scratch_reg := cg.getintregister(list,OS_INT);
  442. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, scratch_reg));
  443. { again... since shifting with address register is not allowed }
  444. if (isaddressregister(reg)) then
  445. begin
  446. scratch_reg2 := cg.getintregister(list,OS_INT);
  447. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg2));
  448. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, scratch_reg2));
  449. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg2,reg));
  450. cg.ungetregister(list,scratch_reg2);
  451. end
  452. else
  453. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, reg));
  454. cg.ungetregister(list,scratch_reg);
  455. end;
  456. end;
  457. OP_SUB :
  458. Begin
  459. if (a >= 1) and (a <= 8) then
  460. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,a,reg))
  461. else
  462. begin
  463. { all others, including coldfire }
  464. list.concat(taicpu.op_const_reg(A_SUB,S_L,a, reg));
  465. end;
  466. end;
  467. OP_XOR :
  468. Begin
  469. list.concat(taicpu.op_const_reg(A_EORI,S_L,a, reg));
  470. end;
  471. else
  472. internalerror(20020729);
  473. end;
  474. end;
  475. procedure tcg68k.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister);
  476. var
  477. hreg1,hreg2,r,r2: tregister;
  478. begin
  479. case op of
  480. OP_ADD :
  481. Begin
  482. if aktoptprocessor = ColdFire then
  483. begin
  484. { operation only allowed only a longword }
  485. sign_extend(list, size, reg1);
  486. sign_extend(list, size, reg2);
  487. list.concat(taicpu.op_reg_reg(A_ADD,S_L,reg1, reg2));
  488. end
  489. else
  490. begin
  491. list.concat(taicpu.op_reg_reg(A_ADD,TCGSize2OpSize[size],reg1, reg2));
  492. end;
  493. end;
  494. OP_AND,OP_OR,
  495. OP_SAR,OP_SHL,
  496. OP_SHR,OP_SUB,OP_XOR :
  497. Begin
  498. { load to data registers }
  499. if (isaddressregister(reg1)) then
  500. begin
  501. hreg1 := cg.getintregister(list,OS_INT);
  502. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  503. end
  504. else
  505. hreg1 := reg1;
  506. if (isaddressregister(reg2)) then
  507. begin
  508. hreg2:= cg.getintregister(list,OS_INT);
  509. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  510. end
  511. else
  512. hreg2 := reg2;
  513. if aktoptprocessor = ColdFire then
  514. begin
  515. { operation only allowed only a longword }
  516. {!***************************************
  517. in the case of shifts, the value to
  518. shift by, should already be valid, so
  519. no need to sign extend the value
  520. !
  521. }
  522. if op in [OP_AND,OP_OR,OP_SUB,OP_XOR] then
  523. sign_extend(list, size, hreg1);
  524. sign_extend(list, size, hreg2);
  525. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],S_L,hreg1, hreg2));
  526. end
  527. else
  528. begin
  529. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg1, hreg2));
  530. end;
  531. if reg1 <> hreg1 then
  532. cg.ungetregister(list,hreg1);
  533. { move back result into destination register }
  534. if reg2 <> hreg2 then
  535. begin
  536. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  537. cg.ungetregister(list,hreg2);
  538. end;
  539. end;
  540. OP_DIV :
  541. Begin
  542. internalerror(20020816);
  543. end;
  544. OP_IDIV :
  545. Begin
  546. internalerror(20020816);
  547. end;
  548. OP_IMUL :
  549. Begin
  550. sign_extend(list, size,reg1);
  551. sign_extend(list, size,reg2);
  552. if aktoptprocessor = MC68000 then
  553. begin
  554. r:=NR_D0;
  555. r2:=NR_D1;
  556. getcpuregister(list,NR_D0);
  557. getcpuregister(list,NR_D1);
  558. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1, r));
  559. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2, r2));
  560. cg.a_call_name(list,'FPC_MUL_LONGINT');
  561. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg2));
  562. ungetregister(list,r);
  563. ungetregister(list,r2);
  564. end
  565. else
  566. begin
  567. if (isaddressregister(reg1)) then
  568. hreg1 := cg.getintregister(list,OS_INT)
  569. else
  570. hreg1 := reg1;
  571. if (isaddressregister(reg2)) then
  572. hreg2:= cg.getintregister(list,OS_INT)
  573. else
  574. hreg2 := reg2;
  575. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  576. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  577. list.concat(taicpu.op_reg_reg(A_MULS,S_L,reg1,reg2));
  578. if reg1 <> hreg1 then
  579. cg.ungetregister(list,hreg1);
  580. { move back result into destination register }
  581. if reg2 <> hreg2 then
  582. begin
  583. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  584. cg.ungetregister(list,hreg2);
  585. end;
  586. end;
  587. end;
  588. OP_MUL :
  589. Begin
  590. sign_extend(list, size,reg1);
  591. sign_extend(list, size,reg2);
  592. if aktoptprocessor = MC68000 then
  593. begin
  594. r:=NR_D0;
  595. r2:=NR_D1;
  596. getcpuregister(list,NR_D0);
  597. getcpuregister(list,NR_D1);
  598. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1, r));
  599. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2, r2));
  600. cg.a_call_name(list,'FPC_MUL_LONGWORD');
  601. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg2));
  602. ungetregister(list,r);
  603. ungetregister(list,r2);
  604. end
  605. else
  606. begin
  607. if (isaddressregister(reg1)) then
  608. begin
  609. hreg1 := cg.getintregister(list,OS_INT);
  610. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  611. end
  612. else
  613. hreg1 := reg1;
  614. if (isaddressregister(reg2)) then
  615. begin
  616. hreg2:= cg.getintregister(list,OS_INT);
  617. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  618. end
  619. else
  620. hreg2 := reg2;
  621. list.concat(taicpu.op_reg_reg(A_MULU,S_L,reg1,reg2));
  622. if reg1<>hreg1 then
  623. cg.ungetregister(list,hreg1);
  624. { move back result into destination register }
  625. if reg2<>hreg2 then
  626. begin
  627. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  628. cg.ungetregister(list,hreg2);
  629. end;
  630. end;
  631. end;
  632. OP_NEG,
  633. OP_NOT :
  634. Begin
  635. { if there are two operands, move the register,
  636. since the operation will only be done on the result
  637. register.
  638. }
  639. if reg1 <> NR_NO then
  640. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,reg1,reg2);
  641. if (isaddressregister(reg2)) then
  642. begin
  643. hreg2 := cg.getintregister(list,OS_INT);
  644. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  645. end
  646. else
  647. hreg2 := reg2;
  648. { coldfire only supports long version }
  649. if aktoptprocessor = ColdFire then
  650. begin
  651. sign_extend(list, size,hreg2);
  652. list.concat(taicpu.op_reg(topcg2tasmop[op],S_L,hreg2));
  653. end
  654. else
  655. begin
  656. list.concat(taicpu.op_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg2));
  657. end;
  658. if reg2 <> hreg2 then
  659. begin
  660. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  661. cg.ungetregister(list,hreg2);
  662. end;
  663. end;
  664. else
  665. internalerror(20020729);
  666. end;
  667. end;
  668. procedure tcg68k.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  669. l : tasmlabel);
  670. var
  671. hregister : tregister;
  672. begin
  673. if a = 0 then
  674. begin
  675. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg));
  676. end
  677. else
  678. begin
  679. if (aktoptprocessor = ColdFire) then
  680. begin
  681. {
  682. only longword comparison is supported,
  683. and only on data registers.
  684. }
  685. hregister := cg.getintregister(list,OS_INT);
  686. { always move to a data register }
  687. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,hregister));
  688. { sign/zero extend the register }
  689. sign_extend(list, size,hregister);
  690. list.concat(taicpu.op_const_reg(A_CMPI,S_L,a,hregister));
  691. cg.ungetregister(list,hregister);
  692. end
  693. else
  694. begin
  695. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  696. end;
  697. end;
  698. { emit the actual jump to the label }
  699. a_jmp_cond(list,cmp_op,l);
  700. end;
  701. procedure tcg68k.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  702. begin
  703. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  704. { emit the actual jump to the label }
  705. a_jmp_cond(list,cmp_op,l);
  706. end;
  707. procedure tcg68k.a_jmp_always(list : taasmoutput;l: tasmlabel);
  708. var
  709. ai: taicpu;
  710. begin
  711. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  712. ai.is_jmp := true;
  713. list.concat(ai);
  714. end;
  715. procedure tcg68k.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  716. var
  717. ai : taicpu;
  718. begin
  719. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  720. ai.SetCondition(flags_to_cond(f));
  721. ai.is_jmp := true;
  722. list.concat(ai);
  723. end;
  724. procedure tcg68k.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  725. var
  726. ai : taicpu;
  727. hreg : tregister;
  728. begin
  729. { move to a Dx register? }
  730. if (isaddressregister(reg)) then
  731. begin
  732. hreg := getintregister(list,OS_INT);
  733. a_load_const_reg(list,size,0,hreg);
  734. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  735. ai.SetCondition(flags_to_cond(f));
  736. list.concat(ai);
  737. if (aktoptprocessor = ColdFire) then
  738. begin
  739. { neg.b does not exist on the Coldfire
  740. so we need to sign extend the value
  741. before doing a neg.l
  742. }
  743. list.concat(taicpu.op_reg(A_EXTB,S_L,hreg));
  744. list.concat(taicpu.op_reg(A_NEG,S_L,hreg));
  745. end
  746. else
  747. begin
  748. list.concat(taicpu.op_reg(A_NEG,S_B,hreg));
  749. end;
  750. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg));
  751. ungetregister(list,hreg);
  752. end
  753. else
  754. begin
  755. a_load_const_reg(list,size,0,reg);
  756. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  757. ai.SetCondition(flags_to_cond(f));
  758. list.concat(ai);
  759. if (aktoptprocessor = ColdFire) then
  760. begin
  761. { neg.b does not exist on the Coldfire
  762. so we need to sign extend the value
  763. before doing a neg.l
  764. }
  765. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  766. list.concat(taicpu.op_reg(A_NEG,S_L,reg));
  767. end
  768. else
  769. begin
  770. list.concat(taicpu.op_reg(A_NEG,S_B,reg));
  771. end;
  772. end;
  773. end;
  774. procedure tcg68k.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint;delsource,loadref : boolean);
  775. var
  776. helpsize : longint;
  777. i : byte;
  778. reg8,reg32 : tregister;
  779. swap : boolean;
  780. hregister : tregister;
  781. iregister : tregister;
  782. jregister : tregister;
  783. hp1 : treference;
  784. hp2 : treference;
  785. hl : tasmlabel;
  786. hl2: tasmlabel;
  787. popaddress : boolean;
  788. srcref,dstref : treference;
  789. begin
  790. popaddress := false;
  791. { this should never occur }
  792. if len > 65535 then
  793. internalerror(0);
  794. hregister := getintregister(list,OS_INT);
  795. if delsource then
  796. reference_release(list,source);
  797. { from 12 bytes movs is being used }
  798. if (not loadref) and ((len<=8) or (not(cs_littlesize in aktglobalswitches) and (len<=12))) then
  799. begin
  800. srcref := source;
  801. dstref := dest;
  802. helpsize:=len div 4;
  803. { move a dword x times }
  804. for i:=1 to helpsize do
  805. begin
  806. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  807. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  808. inc(srcref.offset,4);
  809. inc(dstref.offset,4);
  810. dec(len,4);
  811. end;
  812. { move a word }
  813. if len>1 then
  814. begin
  815. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  816. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  817. inc(srcref.offset,2);
  818. inc(dstref.offset,2);
  819. dec(len,2);
  820. end;
  821. { move a single byte }
  822. if len>0 then
  823. begin
  824. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  825. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  826. end
  827. end
  828. else
  829. begin
  830. iregister:=getaddressregister(list);
  831. jregister:=getaddressregister(list);
  832. { reference for move (An)+,(An)+ }
  833. reference_reset(hp1);
  834. hp1.base := iregister; { source register }
  835. hp1.direction := dir_inc;
  836. reference_reset(hp2);
  837. hp2.base := jregister;
  838. hp2.direction := dir_inc;
  839. { iregister = source }
  840. { jregister = destination }
  841. if loadref then
  842. a_load_ref_reg(list,OS_INT,OS_INT,source,iregister)
  843. else
  844. a_loadaddr_ref_reg(list,source,iregister);
  845. a_loadaddr_ref_reg(list,dest,jregister);
  846. { double word move only on 68020+ machines }
  847. { because of possible alignment problems }
  848. { use fast loop mode }
  849. if (aktoptprocessor=MC68020) then
  850. begin
  851. helpsize := len - len mod 4;
  852. len := len mod 4;
  853. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize div 4,hregister));
  854. objectlibrary.getlabel(hl2);
  855. a_jmp_always(list,hl2);
  856. objectlibrary.getlabel(hl);
  857. a_label(list,hl);
  858. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  859. cg.a_label(list,hl2);
  860. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  861. if len > 1 then
  862. begin
  863. dec(len,2);
  864. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  865. end;
  866. if len = 1 then
  867. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  868. end
  869. else
  870. begin
  871. { Fast 68010 loop mode with no possible alignment problems }
  872. helpsize := len;
  873. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize,hregister));
  874. objectlibrary.getlabel(hl2);
  875. a_jmp_always(list,hl2);
  876. objectlibrary.getlabel(hl);
  877. a_label(list,hl);
  878. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  879. a_label(list,hl2);
  880. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  881. end;
  882. { restore the registers that we have just used olny if they are used! }
  883. ungetregister(list, iregister);
  884. ungetregister(list, jregister);
  885. if jregister = NR_A1 then
  886. hp2.base := NR_NO;
  887. if iregister = NR_A0 then
  888. hp1.base := NR_NO;
  889. reference_release(list,hp1);
  890. reference_release(list,hp2);
  891. end;
  892. if delsource then
  893. tg.ungetiftemp(list,source);
  894. ungetregister(list,hregister);
  895. end;
  896. procedure tcg68k.g_overflowcheck(list: taasmoutput; const l:tlocation; def:tdef);
  897. begin
  898. end;
  899. procedure tcg68k.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint);
  900. begin
  901. end;
  902. procedure tcg68k.g_stackframe_entry(list : taasmoutput;localsize : longint);
  903. var
  904. r,rsp:Tregister;
  905. ref : treference;
  906. begin
  907. r:=NR_FRAME_POINTER_REG;
  908. rsp:=NR_STACK_POINTER_REG;
  909. if localsize<>0 then
  910. begin
  911. { Not to complicate the code generator too much, and since some }
  912. { of the systems only support this format, the localsize cannot }
  913. { exceed 32K in size. }
  914. if (localsize < low(smallint)) or (localsize > high(smallint)) then
  915. CGMessage(cg_e_localsize_too_big);
  916. list.concat(taicpu.op_reg_const(A_LINK,S_W,r,-localsize));
  917. end { endif localsize <> 0 }
  918. else
  919. begin
  920. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  921. ref.direction:=dir_dec;
  922. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,r,ref));
  923. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,rsp,r));
  924. end;
  925. end;
  926. procedure tcg68k.g_restore_frame_pointer(list : taasmoutput);
  927. var
  928. r:Tregister;
  929. begin
  930. r:=NR_FRAME_POINTER_REG;
  931. list.concat(taicpu.op_reg(A_UNLK,S_NO,r));
  932. end;
  933. procedure tcg68k.g_return_from_proc(list : taasmoutput;parasize : aint);
  934. var
  935. r,hregister : tregister;
  936. ref : treference;
  937. begin
  938. { Routines with the poclearstack flag set use only a ret.
  939. also routines with parasize=0 }
  940. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  941. begin
  942. { complex return values are removed from stack in C code PM }
  943. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  944. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  945. else
  946. list.concat(taicpu.op_none(A_RTS,S_NO));
  947. end
  948. else if (parasize=0) then
  949. begin
  950. list.concat(taicpu.op_none(A_RTS,S_NO));
  951. end
  952. else
  953. begin
  954. { return with immediate size possible here
  955. signed!
  956. RTD is not supported on the coldfire }
  957. if (aktoptprocessor=MC68020) and (parasize<$7FFF) then
  958. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  959. { manually restore the stack }
  960. else
  961. begin
  962. { We must pull the PC Counter from the stack, before }
  963. { restoring the stack pointer, otherwise the PC would }
  964. { point to nowhere! }
  965. { save the PC counter (pop it from the stack) }
  966. hregister:=NR_A3;
  967. a_reg_alloc(list,hregister);
  968. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  969. ref.direction:=dir_inc;
  970. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  971. { can we do a quick addition ... }
  972. r:=NR_SP;
  973. if (parasize > 0) and (parasize < 9) then
  974. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  975. else { nope ... }
  976. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  977. { restore the PC counter (push it on the stack) }
  978. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  979. ref.direction:=dir_dec;
  980. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hregister,ref));
  981. a_reg_alloc(list,hregister);
  982. list.concat(taicpu.op_none(A_RTS,S_NO));
  983. end;
  984. end;
  985. end;
  986. procedure Tcg68k.g_save_standard_registers(list:Taasmoutput);
  987. var
  988. tosave : tcpuregisterset;
  989. ref : treference;
  990. begin
  991. {!!!!!
  992. tosave:=std_saved_registers;
  993. { only save the registers which are not used and must be saved }
  994. tosave:=tosave*(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_dec;
  997. if tosave<>[] then
  998. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,tosave,ref));
  999. }
  1000. end;
  1001. procedure Tcg68k.g_restore_standard_registers(list:Taasmoutput);
  1002. var
  1003. torestore : tcpuregisterset;
  1004. r:Tregister;
  1005. ref : treference;
  1006. begin
  1007. {!!!!!!!!
  1008. torestore:=std_saved_registers;
  1009. { should be intersected with used regs, no ? }
  1010. torestore:=torestore*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  1011. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1012. ref.direction:=dir_inc;
  1013. if torestore<>[] then
  1014. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,ref,torestore));
  1015. }
  1016. end;
  1017. procedure tcg68k.g_save_all_registers(list : taasmoutput);
  1018. begin
  1019. end;
  1020. procedure tcg68k.g_restore_all_registers(list : taasmoutput;const funcretparaloc:TCGPara);
  1021. begin
  1022. end;
  1023. procedure tcg68k.sign_extend(list: taasmoutput;_oldsize : tcgsize; reg: tregister);
  1024. begin
  1025. case _oldsize of
  1026. { sign extend }
  1027. OS_S8:
  1028. begin
  1029. if (isaddressregister(reg)) then
  1030. internalerror(20020729);
  1031. if (aktoptprocessor = MC68000) then
  1032. begin
  1033. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1034. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1035. end
  1036. else
  1037. begin
  1038. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1039. end;
  1040. end;
  1041. OS_S16:
  1042. begin
  1043. if (isaddressregister(reg)) then
  1044. internalerror(20020729);
  1045. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1046. end;
  1047. { zero extend }
  1048. OS_8:
  1049. begin
  1050. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1051. end;
  1052. OS_16:
  1053. begin
  1054. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1055. end;
  1056. end; { otherwise the size is already correct }
  1057. end;
  1058. procedure tcg68k.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  1059. var
  1060. ai : taicpu;
  1061. begin
  1062. if cond=OC_None then
  1063. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1064. else
  1065. begin
  1066. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1067. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1068. end;
  1069. ai.is_jmp:=true;
  1070. list.concat(ai);
  1071. end;
  1072. {****************************************************************************}
  1073. { TCG64F68K }
  1074. {****************************************************************************}
  1075. procedure tcg64f68k.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1076. var
  1077. hreg1, hreg2 : tregister;
  1078. opcode : tasmop;
  1079. begin
  1080. opcode := topcg2tasmop[op];
  1081. case op of
  1082. OP_ADD :
  1083. begin
  1084. { if one of these three registers is an address
  1085. register, we'll really get into problems!
  1086. }
  1087. if isaddressregister(regdst.reglo) or
  1088. isaddressregister(regdst.reghi) or
  1089. isaddressregister(regsrc.reghi) then
  1090. internalerror(20020817);
  1091. list.concat(taicpu.op_reg_reg(A_ADD,S_L,regsrc.reglo,regdst.reglo));
  1092. list.concat(taicpu.op_reg_reg(A_ADDX,S_L,regsrc.reghi,regdst.reghi));
  1093. end;
  1094. OP_AND,OP_OR :
  1095. begin
  1096. { at least one of the registers must be a data register }
  1097. if (isaddressregister(regdst.reglo) and
  1098. isaddressregister(regsrc.reglo)) or
  1099. (isaddressregister(regsrc.reghi) and
  1100. isaddressregister(regdst.reghi))
  1101. then
  1102. internalerror(20020817);
  1103. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1104. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1105. end;
  1106. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1107. OP_IDIV,OP_DIV,
  1108. OP_IMUL,OP_MUL: internalerror(2002081701);
  1109. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1110. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1111. OP_SUB:
  1112. begin
  1113. { if one of these three registers is an address
  1114. register, we'll really get into problems!
  1115. }
  1116. if isaddressregister(regdst.reglo) or
  1117. isaddressregister(regdst.reghi) or
  1118. isaddressregister(regsrc.reghi) then
  1119. internalerror(20020817);
  1120. list.concat(taicpu.op_reg_reg(A_SUB,S_L,regsrc.reglo,regdst.reglo));
  1121. list.concat(taicpu.op_reg_reg(A_SUBX,S_L,regsrc.reghi,regdst.reghi));
  1122. end;
  1123. OP_XOR:
  1124. begin
  1125. if isaddressregister(regdst.reglo) or
  1126. isaddressregister(regsrc.reglo) or
  1127. isaddressregister(regsrc.reghi) or
  1128. isaddressregister(regdst.reghi) then
  1129. internalerror(20020817);
  1130. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reglo,regdst.reglo));
  1131. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reghi,regdst.reghi));
  1132. end;
  1133. end; { end case }
  1134. end;
  1135. procedure tcg64f68k.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1136. var
  1137. lowvalue : cardinal;
  1138. highvalue : cardinal;
  1139. begin
  1140. { is it optimized out ? }
  1141. if optimize64_op_const_reg(list,op,value,reg) then
  1142. exit;
  1143. lowvalue := cardinal(value);
  1144. highvalue:= value shr 32;
  1145. { the destination registers must be data registers }
  1146. if isaddressregister(reg.reglo) or
  1147. isaddressregister(reg.reghi) then
  1148. internalerror(20020817);
  1149. case op of
  1150. OP_ADD :
  1151. begin
  1152. list.concat(taicpu.op_const_reg(A_ADD,S_L,lowvalue,reg.reglo));
  1153. list.concat(taicpu.op_const_reg(A_ADDX,S_L,highvalue,reg.reglo));
  1154. end;
  1155. OP_AND :
  1156. begin
  1157. { should already be optimized out }
  1158. internalerror(2002081801);
  1159. end;
  1160. OP_OR :
  1161. begin
  1162. { should already be optimized out }
  1163. internalerror(2002081802);
  1164. end;
  1165. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1166. OP_IDIV,OP_DIV,
  1167. OP_IMUL,OP_MUL: internalerror(2002081701);
  1168. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1169. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1170. OP_SUB:
  1171. begin
  1172. list.concat(taicpu.op_const_reg(A_SUB,S_L,lowvalue,reg.reglo));
  1173. list.concat(taicpu.op_const_reg(A_SUBX,S_L,highvalue,reg.reglo));
  1174. end;
  1175. OP_XOR:
  1176. begin
  1177. list.concat(taicpu.op_const_reg(A_EOR,S_L,lowvalue,reg.reglo));
  1178. list.concat(taicpu.op_const_reg(A_EOR,S_L,highvalue,reg.reglo));
  1179. end;
  1180. end; { end case }
  1181. end;
  1182. begin
  1183. cg := tcg68k.create;
  1184. cg64 :=tcg64f68k.create;
  1185. end.
  1186. {
  1187. $Log$
  1188. Revision 1.32 2004-11-27 16:16:02 florian
  1189. * some m68k stuff updated
  1190. Revision 1.31 2004/11/09 22:32:59 peter
  1191. * small m68k updates to bring it up2date
  1192. * give better error for external local variable
  1193. Revision 1.30 2004/10/11 15:48:15 peter
  1194. * small regvar for para fixes
  1195. * function tvarsym.is_regvar added
  1196. * tvarsym.getvaluesize removed, use getsize instead
  1197. Revision 1.29 2004/09/25 14:23:54 peter
  1198. * ungetregister is now only used for cpuregisters, renamed to
  1199. ungetcpuregister
  1200. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1201. * removed location-release/reference_release
  1202. Revision 1.28 2004/06/20 08:55:31 florian
  1203. * logs truncated
  1204. Revision 1.27 2004/05/20 21:54:33 florian
  1205. + <pointer> - <pointer> result is divided by the pointer element size now
  1206. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  1207. Revision 1.26 2004/05/06 22:01:54 florian
  1208. * register numbers for address registers fixed
  1209. Revision 1.25 2004/05/06 20:30:51 florian
  1210. * m68k compiler compilation fixed
  1211. Revision 1.24 2004/04/19 21:15:12 florian
  1212. * fixed compilation
  1213. Revision 1.23 2004/04/18 21:13:59 florian
  1214. * more adaptions for m68k
  1215. }