cgcpu.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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. {DEFINE DEBUG_CHARLIE}
  18. {$IFNDEF DEBUG_CHARLIE}
  19. {$WARNINGS OFF}
  20. {$ENDIF}
  21. unit cgcpu;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cgbase,cgobj,globtype,
  26. aasmbase,aasmtai,aasmdata,aasmcpu,
  27. cpubase,cpuinfo,
  28. parabase,cpupara,
  29. node,symconst,symtype,symdef,
  30. cgutils,cg64f32;
  31. type
  32. tcg68k = class(tcg)
  33. procedure init_register_allocators;override;
  34. procedure done_register_allocators;override;
  35. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  36. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  37. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  38. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  39. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  40. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  41. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  42. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  43. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  44. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  45. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  46. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  47. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  48. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  49. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  50. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  51. procedure a_loadmm_reg_reg(list: TAsmList;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle); override;
  52. procedure a_loadmm_ref_reg(list: TAsmList;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  53. procedure a_loadmm_reg_ref(list: TAsmList;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  54. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle); override;
  55. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  56. // procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  57. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); override;
  58. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  59. l : tasmlabel);override;
  60. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  61. procedure a_jmp_name(list : TAsmList;const s : string); override;
  62. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  63. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  64. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  65. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  66. { generates overflow checking code for a node }
  67. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  68. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  69. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  70. // procedure g_restore_frame_pointer(list : TAsmList);override;
  71. // procedure g_return_from_proc(list : TAsmList;parasize : tcgint);override;
  72. procedure g_restore_registers(list:TAsmList);override;
  73. procedure g_save_registers(list:TAsmList);override;
  74. // procedure g_save_all_registers(list : TAsmList);override;
  75. // procedure g_restore_all_registers(list : TAsmList;const funcretparaloc:TCGPara);override;
  76. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  77. protected
  78. function fixref(list: TAsmList; var ref: treference): boolean;
  79. private
  80. { # Sign or zero extend the register to a full 32-bit value.
  81. The new value is left in the same register.
  82. }
  83. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  84. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  85. end;
  86. tcg64f68k = class(tcg64f32)
  87. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  88. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  89. end;
  90. { This function returns true if the reference+offset is valid.
  91. Otherwise extra code must be generated to solve the reference.
  92. On the m68k, this verifies that the reference is valid
  93. (e.g : if index register is used, then the max displacement
  94. is 256 bytes, if only base is used, then max displacement
  95. is 32K
  96. }
  97. function isvalidrefoffset(const ref: treference): boolean;
  98. const
  99. TCGSize2OpSize: Array[tcgsize] of topsize =
  100. (S_NO,S_B,S_W,S_L,S_L,S_NO,S_B,S_W,S_L,S_L,S_NO,
  101. S_FS,S_FD,S_FX,S_NO,S_NO,
  102. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,
  103. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  104. procedure create_codegen;
  105. implementation
  106. uses
  107. globals,verbose,systems,cutils,
  108. symsym,defutil,paramgr,procinfo,
  109. rgobj,tgobj,rgcpu,fmodule;
  110. const
  111. { opcode table lookup }
  112. topcg2tasmop: Array[topcg] of tasmop =
  113. (
  114. A_NONE,
  115. A_MOVE,
  116. A_ADD,
  117. A_AND,
  118. A_DIVU,
  119. A_DIVS,
  120. A_MULS,
  121. A_MULU,
  122. A_NEG,
  123. A_NOT,
  124. A_OR,
  125. A_ASR,
  126. A_LSL,
  127. A_LSR,
  128. A_SUB,
  129. A_EOR,
  130. A_NONE,
  131. A_NONE
  132. );
  133. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  134. (
  135. C_NONE,
  136. C_EQ,
  137. C_GT,
  138. C_LT,
  139. C_GE,
  140. C_LE,
  141. C_NE,
  142. C_LS,
  143. C_CS,
  144. C_CC,
  145. C_HI
  146. );
  147. function isvalidrefoffset(const ref: treference): boolean;
  148. begin
  149. isvalidrefoffset := true;
  150. if ref.index <> NR_NO then
  151. begin
  152. if ref.base <> NR_NO then
  153. internalerror(2002081401);
  154. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  155. isvalidrefoffset := false
  156. end
  157. else
  158. begin
  159. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  160. isvalidrefoffset := false;
  161. end;
  162. end;
  163. {****************************************************************************}
  164. { TCG68K }
  165. {****************************************************************************}
  166. function use_push(const cgpara:tcgpara):boolean;
  167. begin
  168. result:=(not paramanager.use_fixed_stack) and
  169. assigned(cgpara.location) and
  170. (cgpara.location^.loc=LOC_REFERENCE) and
  171. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  172. end;
  173. procedure tcg68k.init_register_allocators;
  174. begin
  175. inherited init_register_allocators;
  176. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  177. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  178. first_int_imreg,[]);
  179. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  180. [RS_A0,RS_A1,RS_A2,RS_A3,RS_A4,RS_A5,RS_A6],
  181. first_addr_imreg,[]);
  182. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  183. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  184. first_fpu_imreg,[]);
  185. end;
  186. procedure tcg68k.done_register_allocators;
  187. begin
  188. rg[R_INTREGISTER].free;
  189. rg[R_FPUREGISTER].free;
  190. rg[R_ADDRESSREGISTER].free;
  191. inherited done_register_allocators;
  192. end;
  193. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  194. var
  195. pushsize : tcgsize;
  196. ref : treference;
  197. begin
  198. {$ifdef DEBUG_CHARLIE}
  199. // writeln('a_load_reg');_cgpara
  200. {$endif DEBUG_CHARLIE}
  201. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  202. { TODO: FIX ME! check_register_size()}
  203. // check_register_size(size,r);
  204. if use_push(cgpara) then
  205. begin
  206. cgpara.check_simple_location;
  207. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  208. pushsize:=cgpara.location^.size
  209. else
  210. pushsize:=int_cgsize(cgpara.alignment);
  211. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  212. ref.direction := dir_dec;
  213. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  214. end
  215. else
  216. inherited a_load_reg_cgpara(list,size,r,cgpara);
  217. end;
  218. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  219. var
  220. pushsize : tcgsize;
  221. ref : treference;
  222. begin
  223. {$ifdef DEBUG_CHARLIE}
  224. // writeln('a_load_const');_cgpara
  225. {$endif DEBUG_CHARLIE}
  226. if use_push(cgpara) then
  227. begin
  228. cgpara.check_simple_location;
  229. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  230. pushsize:=cgpara.location^.size
  231. else
  232. pushsize:=int_cgsize(cgpara.alignment);
  233. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  234. ref.direction := dir_dec;
  235. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[pushsize],a,ref));
  236. end
  237. else
  238. inherited a_load_const_cgpara(list,size,a,cgpara);
  239. end;
  240. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  241. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  242. var
  243. pushsize : tcgsize;
  244. tmpreg : tregister;
  245. href : treference;
  246. ref : treference;
  247. begin
  248. if not assigned(paraloc) then
  249. exit;
  250. { TODO: FIX ME!!! this also triggers location bug }
  251. {if (paraloc^.loc<>LOC_REFERENCE) or
  252. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  253. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  254. internalerror(200501162);}
  255. { Pushes are needed in reverse order, add the size of the
  256. current location to the offset where to load from. This
  257. prevents wrong calculations for the last location when
  258. the size is not a power of 2 }
  259. if assigned(paraloc^.next) then
  260. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  261. { Push the data starting at ofs }
  262. href:=r;
  263. inc(href.offset,ofs);
  264. fixref(list,href);
  265. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  266. pushsize:=paraloc^.size
  267. else
  268. pushsize:=int_cgsize(cgpara.alignment);
  269. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[paraloc^.size]);
  270. ref.direction := dir_dec;
  271. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  272. begin
  273. tmpreg:=getintregister(list,pushsize);
  274. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  275. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],tmpreg,ref));
  276. end
  277. else
  278. list.concat(taicpu.op_ref_ref(A_MOVE,tcgsize2opsize[pushsize],href,ref));
  279. end;
  280. var
  281. len : tcgint;
  282. href : treference;
  283. begin
  284. {$ifdef DEBUG_CHARLIE}
  285. // writeln('a_load_ref');_cgpara
  286. {$endif DEBUG_CHARLIE}
  287. { cgpara.size=OS_NO requires a copy on the stack }
  288. if use_push(cgpara) then
  289. begin
  290. { Record copy? }
  291. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  292. begin
  293. cgpara.check_simple_location;
  294. len:=align(cgpara.intsize,cgpara.alignment);
  295. g_stackpointer_alloc(list,len);
  296. reference_reset_base(href,NR_STACK_POINTER_REG,0,cgpara.alignment);
  297. g_concatcopy(list,r,href,len);
  298. end
  299. else
  300. begin
  301. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  302. internalerror(200501161);
  303. { We need to push the data in reverse order,
  304. therefor we use a recursive algorithm }
  305. pushdata(cgpara.location,0);
  306. end
  307. end
  308. else
  309. inherited a_load_ref_cgpara(list,size,r,cgpara);
  310. end;
  311. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  312. var
  313. tmpreg : tregister;
  314. opsize : topsize;
  315. begin
  316. {$ifdef DEBUG_CHARLIE}
  317. // writeln('a_loadaddr_ref');_cgpara
  318. {$endif DEBUG_CHARLIE}
  319. with r do
  320. begin
  321. { i suppose this is not required for m68k (KB) }
  322. // if (segment<>NR_NO) then
  323. // cgmessage(cg_e_cant_use_far_pointer_there);
  324. if not use_push(cgpara) then
  325. begin
  326. cgpara.check_simple_location;
  327. opsize:=tcgsize2opsize[OS_ADDR];
  328. if (segment=NR_NO) and (base=NR_NO) and (index=NR_NO) then
  329. begin
  330. if assigned(symbol) then
  331. // list.concat(Taicpu.Op_sym_ofs(A_PUSH,opsize,symbol,offset))
  332. else;
  333. // list.concat(Taicpu.Op_const(A_PUSH,opsize,offset));
  334. end
  335. else if (segment=NR_NO) and (base=NR_NO) and (index<>NR_NO) and
  336. (offset=0) and (scalefactor=0) and (symbol=nil) then
  337. // list.concat(Taicpu.Op_reg(A_PUSH,opsize,index))
  338. else if (segment=NR_NO) and (base<>NR_NO) and (index=NR_NO) and
  339. (offset=0) and (symbol=nil) then
  340. // list.concat(Taicpu.Op_reg(A_PUSH,opsize,base))
  341. else
  342. begin
  343. tmpreg:=getaddressregister(list);
  344. a_loadaddr_ref_reg(list,r,tmpreg);
  345. // list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  346. end;
  347. end
  348. else
  349. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  350. end;
  351. end;
  352. function tcg68k.fixref(list: TAsmList; var ref: treference): boolean;
  353. var
  354. hreg,idxreg : tregister;
  355. href : treference;
  356. begin
  357. result:=false;
  358. { The MC68020+ has extended
  359. addressing capabilities with a 32-bit
  360. displacement.
  361. }
  362. { first ensure that base is an address register }
  363. if (not assigned (ref.symbol) and (current_settings.cputype<>cpu_MC68000)) and
  364. (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  365. begin
  366. hreg:=getaddressregister(list);
  367. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  368. fixref:=true;
  369. ref.base:=hreg;
  370. end;
  371. if (current_settings.cputype=cpu_MC68020) then
  372. exit;
  373. { ToDo: check which constraints of Coldfire also apply to MC68000 }
  374. case current_settings.cputype of
  375. cpu_MC68000:
  376. begin
  377. if (ref.base<>NR_NO) then
  378. begin
  379. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  380. begin
  381. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,ref.index));
  382. ref.index:=NR_NO;
  383. end;
  384. { base + reg }
  385. if ref.index <> NR_NO then
  386. begin
  387. { base + reg + offset }
  388. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  389. begin
  390. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,ref.base));
  391. fixref := true;
  392. ref.offset := 0;
  393. exit;
  394. end;
  395. end
  396. else
  397. { base + offset }
  398. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  399. begin
  400. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,ref.base));
  401. fixref := true;
  402. ref.offset := 0;
  403. exit;
  404. end;
  405. if assigned(ref.symbol) then
  406. begin
  407. hreg:=getaddressregister(list);
  408. idxreg:=ref.base;
  409. ref.base:=NR_NO;
  410. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  411. reference_reset_base(ref,hreg,0,ref.alignment);
  412. fixref:=true;
  413. ref.index:=idxreg;
  414. end
  415. else if not isaddressregister(ref.base) then
  416. begin
  417. hreg:=getaddressregister(list);
  418. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  419. fixref:=true;
  420. ref.base:=hreg;
  421. end;
  422. end
  423. else
  424. { Note: symbol -> ref would be supported as long as ref does not
  425. contain a offset or index... (maybe something for the
  426. optimizer) }
  427. if Assigned(ref.symbol) and (ref.index<>NR_NO) then
  428. begin
  429. hreg:=cg.getaddressregister(list);
  430. idxreg:=ref.index;
  431. ref.index:=NR_NO;
  432. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  433. reference_reset_base(ref,hreg,0,ref.alignment);
  434. ref.index:=idxreg;
  435. fixref:=true;
  436. end;
  437. end;
  438. cpu_Coldfire:
  439. begin
  440. if (ref.base<>NR_NO) then
  441. begin
  442. if assigned(ref.symbol) and (ref.index=NR_NO) then
  443. begin
  444. hreg:=cg.getaddressregister(list);
  445. reference_reset_symbol(href,ref.symbol,0,ref.alignment);
  446. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  447. ref.index:=ref.base;
  448. ref.base:=hreg;
  449. ref.symbol:=nil;
  450. end;
  451. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  452. begin
  453. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,ref.index));
  454. ref.index:=NR_NO;
  455. end;
  456. {if (ref.index <> NR_NO) and assigned(ref.symbol) then
  457. internalerror(2002081403);}
  458. { base + reg }
  459. if ref.index <> NR_NO then
  460. begin
  461. { base + reg + offset }
  462. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  463. begin
  464. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,ref.base));
  465. fixref := true;
  466. ref.offset := 0;
  467. exit;
  468. end;
  469. end
  470. else
  471. { base + offset }
  472. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  473. begin
  474. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,ref.base));
  475. fixref:=true;
  476. ref.offset:=0;
  477. exit;
  478. end;
  479. end
  480. else
  481. { Note: symbol -> ref would be supported as long as ref does not
  482. contain a offset or index... (maybe something for the
  483. optimizer) }
  484. if Assigned(ref.symbol) {and (ref.index<>NR_NO)} then
  485. begin
  486. hreg:=cg.getaddressregister(list);
  487. idxreg:=ref.index;
  488. ref.index:=NR_NO;
  489. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  490. reference_reset_base(ref,hreg,0,ref.alignment);
  491. ref.index:=idxreg;
  492. fixref:=true;
  493. end;
  494. end;
  495. end;
  496. end;
  497. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  498. var
  499. sym: tasmsymbol;
  500. begin
  501. if not(weak) then
  502. sym:=current_asmdata.RefAsmSymbol(s)
  503. else
  504. sym:=current_asmdata.WeakRefAsmSymbol(s);
  505. list.concat(taicpu.op_sym(A_JSR,S_NO,current_asmdata.RefAsmSymbol(s)));
  506. end;
  507. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  508. var
  509. tmpref : treference;
  510. tmpreg : tregister;
  511. begin
  512. {$ifdef DEBUG_CHARLIE}
  513. list.concat(tai_comment.create(strpnew('a_call_reg')));
  514. {$endif}
  515. if isaddressregister(reg) then
  516. begin
  517. { if we have an address register, we can jump to the address directly }
  518. reference_reset_base(tmpref,reg,0,4);
  519. end
  520. else
  521. begin
  522. { if we have a data register, we need to move it to an address register first }
  523. tmpreg:=getaddressregister(list);
  524. reference_reset_base(tmpref,tmpreg,0,4);
  525. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg));
  526. end;
  527. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  528. end;
  529. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  530. begin
  531. {$ifdef DEBUG_CHARLIE}
  532. // writeln('a_load_const_reg');
  533. {$endif DEBUG_CHARLIE}
  534. if isaddressregister(register) then
  535. begin
  536. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),register))
  537. end
  538. else
  539. if a = 0 then
  540. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  541. else
  542. begin
  543. if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  544. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  545. else
  546. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),register))
  547. end;
  548. end;
  549. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  550. var
  551. hreg : tregister;
  552. href : treference;
  553. begin
  554. {$ifdef DEBUG_CHARLIE}
  555. list.concat(tai_comment.create(strpnew('a_load_const_ref')));
  556. {$endif DEBUG_CHARLIE}
  557. href:=ref;
  558. fixref(list,href);
  559. { for coldfire we need to go through a temporary register if we have a
  560. offset, index or symbol given }
  561. if (current_settings.cputype=cpu_coldfire) and
  562. (
  563. (href.offset<>0) or
  564. { TODO : check whether we really need this second condition }
  565. (href.index<>NR_NO) or
  566. assigned(href.symbol)
  567. ) then
  568. begin
  569. hreg:=getintregister(list,tosize);
  570. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),hreg));
  571. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href));
  572. end
  573. else
  574. list.concat(taicpu.op_const_ref(A_MOVE,S_L,longint(a),href));
  575. end;
  576. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  577. var
  578. href : treference;
  579. begin
  580. href := ref;
  581. fixref(list,href);
  582. {$ifdef DEBUG_CHARLIE}
  583. list.concat(tai_comment.create(strpnew('a_load_reg_ref')));
  584. {$endif DEBUG_CHARLIE}
  585. { move to destination reference }
  586. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[fromsize],register,href));
  587. end;
  588. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  589. var
  590. aref: treference;
  591. bref: treference;
  592. dofix : boolean;
  593. hreg: TRegister;
  594. begin
  595. aref := sref;
  596. bref := dref;
  597. fixref(list,aref);
  598. fixref(list,bref);
  599. {$ifdef DEBUG_CHARLIE}
  600. // writeln('a_load_ref_ref');
  601. {$endif DEBUG_CHARLIE}
  602. { Coldfire dislikes certain move combinations }
  603. if current_settings.cputype=cpu_coldfire then
  604. begin
  605. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  606. dofix:=false;
  607. if { (d16,Ax) and (d8,Ax,Xi) }
  608. (
  609. (aref.base<>NR_NO) and
  610. (
  611. (aref.index<>NR_NO) or
  612. (aref.offset<>0)
  613. )
  614. ) or
  615. { (xxx) }
  616. assigned(aref.symbol) then
  617. begin
  618. if aref.index<>NR_NO then
  619. begin
  620. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  621. (
  622. (bref.base<>NR_NO) and
  623. (
  624. (bref.index<>NR_NO) or
  625. (bref.offset<>0)
  626. )
  627. ) or
  628. { (xxx) }
  629. assigned(bref.symbol);
  630. end
  631. else
  632. { offset <> 0, but no index }
  633. begin
  634. dofix:={ (d8,Ax,Xi) }
  635. (
  636. (bref.base<>NR_NO) and
  637. (bref.index<>NR_NO)
  638. ) or
  639. { (xxx) }
  640. assigned(bref.symbol);
  641. end;
  642. end;
  643. if dofix then
  644. begin
  645. hreg:=getaddressregister(list);
  646. list.concat(taicpu.op_ref_reg(A_LEA,S_L,bref,hreg));
  647. list.concat(taicpu.op_reg_ref(A_MOVE,S_L{TCGSize2OpSize[fromsize]},hreg,bref));
  648. exit;
  649. end;
  650. end;
  651. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  652. end;
  653. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  654. begin
  655. { move to destination register }
  656. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2));
  657. { zero/sign extend register to 32-bit }
  658. sign_extend(list, fromsize, reg2);
  659. end;
  660. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  661. var
  662. href : treference;
  663. begin
  664. href:=ref;
  665. fixref(list,href);
  666. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],href,register));
  667. { extend the value in the register }
  668. sign_extend(list, tosize, register);
  669. end;
  670. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  671. var
  672. href : treference;
  673. // p: pointer;
  674. begin
  675. { TODO: FIX ME!!! take a look on this mess again...}
  676. // if getregtype(r)=R_ADDRESSREGISTER then
  677. // begin
  678. // writeln('address reg?!?');
  679. // p:=nil; dword(p^):=0; {DEBUG CODE... :D )
  680. // internalerror(2002072901);
  681. // end;
  682. href:=ref;
  683. fixref(list, href);
  684. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  685. end;
  686. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  687. begin
  688. { in emulation mode, only 32-bit single is supported }
  689. if cs_fp_emulation in current_settings.moduleswitches then
  690. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2))
  691. else
  692. list.concat(taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[tosize],reg1,reg2));
  693. end;
  694. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  695. var
  696. opsize : topsize;
  697. href : treference;
  698. tmpreg : tregister;
  699. begin
  700. opsize := tcgsize2opsize[fromsize];
  701. { extended is not supported, since it is not available on Coldfire }
  702. if opsize = S_FX then
  703. internalerror(20020729);
  704. href := ref;
  705. fixref(list,href);
  706. { in emulation mode, only 32-bit single is supported }
  707. if cs_fp_emulation in current_settings.moduleswitches then
  708. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  709. else
  710. begin
  711. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  712. if (tosize < fromsize) then
  713. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  714. end;
  715. end;
  716. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  717. var
  718. opsize : topsize;
  719. begin
  720. opsize := tcgsize2opsize[tosize];
  721. { extended is not supported, since it is not available on Coldfire }
  722. if opsize = S_FX then
  723. internalerror(20020729);
  724. { in emulation mode, only 32-bit single is supported }
  725. if cs_fp_emulation in current_settings.moduleswitches then
  726. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  727. else
  728. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  729. end;
  730. procedure tcg68k.a_loadmm_reg_reg(list: TAsmList;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle);
  731. begin
  732. internalerror(20020729);
  733. end;
  734. procedure tcg68k.a_loadmm_ref_reg(list: TAsmList;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle);
  735. begin
  736. internalerror(20020729);
  737. end;
  738. procedure tcg68k.a_loadmm_reg_ref(list: TAsmList;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle);
  739. begin
  740. internalerror(20020729);
  741. end;
  742. procedure tcg68k.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle);
  743. begin
  744. internalerror(20020729);
  745. end;
  746. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  747. var
  748. scratch_reg : tregister;
  749. scratch_reg2: tregister;
  750. opcode : tasmop;
  751. r,r2 : Tregister;
  752. begin
  753. optimize_op_const(op, a);
  754. opcode := topcg2tasmop[op];
  755. case op of
  756. OP_NONE :
  757. begin
  758. { Opcode is optimized away }
  759. end;
  760. OP_MOVE :
  761. begin
  762. { Optimized, replaced with a simple load }
  763. a_load_const_reg(list,size,a,reg);
  764. end;
  765. OP_ADD :
  766. begin
  767. if (a >= 1) and (a <= 8) then
  768. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,a, reg))
  769. else
  770. begin
  771. { all others, including coldfire }
  772. list.concat(taicpu.op_const_reg(A_ADD,S_L,a, reg));
  773. end;
  774. end;
  775. OP_AND,
  776. OP_OR:
  777. begin
  778. if isaddressregister(reg) then
  779. begin
  780. { use scratch register (there is a anda/ora though...) }
  781. scratch_reg:=getintregister(list,OS_INT);
  782. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg));
  783. list.concat(taicpu.op_const_reg(opcode,S_L,longint(a),scratch_reg));
  784. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  785. end
  786. else
  787. list.concat(taicpu.op_const_reg(topcg2tasmop[op],S_L,longint(a), reg));
  788. end;
  789. OP_DIV :
  790. begin
  791. internalerror(20020816);
  792. end;
  793. OP_IDIV :
  794. begin
  795. internalerror(20020816);
  796. end;
  797. OP_IMUL :
  798. begin
  799. if current_settings.cputype<>cpu_MC68020 then
  800. begin
  801. r:=NR_D0;
  802. r2:=NR_D1;
  803. cg.getcpuregister(list,NR_D0);
  804. cg.getcpuregister(list,NR_D1);
  805. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, r));
  806. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, r2));
  807. cg.a_call_name(list,'FPC_MUL_LONGINT',false);
  808. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg));
  809. cg.ungetcpuregister(list,r);
  810. cg.ungetcpuregister(list,r2);
  811. end
  812. else
  813. begin
  814. if (isaddressregister(reg)) then
  815. begin
  816. scratch_reg := getintregister(list,OS_INT);
  817. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  818. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,scratch_reg));
  819. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  820. end
  821. else
  822. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,reg));
  823. end;
  824. end;
  825. OP_MUL :
  826. begin
  827. if current_settings.cputype<>cpu_MC68020 then
  828. begin
  829. r:=NR_D0;
  830. r2:=NR_D1;
  831. cg.getcpuregister(list,NR_D0);
  832. cg.getcpuregister(list,NR_D1);
  833. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, r));
  834. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, r2));
  835. cg.a_call_name(list,'FPC_MUL_DWORD',false);
  836. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg));
  837. cg.ungetcpuregister(list,r);
  838. cg.ungetcpuregister(list,r2);
  839. end
  840. else
  841. begin
  842. if (isaddressregister(reg)) then
  843. begin
  844. scratch_reg := getintregister(list,OS_INT);
  845. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  846. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,scratch_reg));
  847. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  848. end
  849. else
  850. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,reg));
  851. end;
  852. end;
  853. OP_SAR,
  854. OP_SHL,
  855. OP_SHR :
  856. begin
  857. if (a >= 1) and (a <= 8) then
  858. begin
  859. { not allowed to shift an address register }
  860. if (isaddressregister(reg)) then
  861. begin
  862. scratch_reg := getintregister(list,OS_INT);
  863. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg));
  864. list.concat(taicpu.op_const_reg(opcode,S_L,a, scratch_reg));
  865. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg));
  866. end
  867. else
  868. list.concat(taicpu.op_const_reg(opcode,S_L,a, reg));
  869. end
  870. else
  871. begin
  872. { we must load the data into a register ... :() }
  873. scratch_reg := cg.getintregister(list,OS_INT);
  874. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, scratch_reg));
  875. { again... since shifting with address register is not allowed }
  876. if (isaddressregister(reg)) then
  877. begin
  878. scratch_reg2 := cg.getintregister(list,OS_INT);
  879. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg2));
  880. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, scratch_reg2));
  881. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg2,reg));
  882. end
  883. else
  884. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, reg));
  885. end;
  886. end;
  887. OP_SUB :
  888. begin
  889. if (a >= 1) and (a <= 8) then
  890. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,a,reg))
  891. else
  892. begin
  893. { all others, including coldfire }
  894. list.concat(taicpu.op_const_reg(A_SUB,S_L,a, reg));
  895. end;
  896. end;
  897. OP_XOR :
  898. begin
  899. list.concat(taicpu.op_const_reg(A_EORI,S_L,a, reg));
  900. end;
  901. else
  902. internalerror(20020729);
  903. end;
  904. end;
  905. {
  906. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  907. var
  908. opcode: tasmop;
  909. begin
  910. writeln('a_op_const_ref');
  911. optimize_op_const(op, a);
  912. opcode := topcg2tasmop[op];
  913. case op of
  914. OP_NONE :
  915. begin
  916. { opcode was optimized away }
  917. end;
  918. OP_MOVE :
  919. begin
  920. { Optimized, replaced with a simple load }
  921. a_load_const_ref(list,size,a,ref);
  922. end;
  923. else
  924. begin
  925. internalerror(2007010101);
  926. end;
  927. end;
  928. end;
  929. }
  930. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister);
  931. var
  932. hreg1,hreg2,r,r2: tregister;
  933. begin
  934. case op of
  935. OP_ADD :
  936. begin
  937. if current_settings.cputype = cpu_ColdFire then
  938. begin
  939. { operation only allowed only a longword }
  940. sign_extend(list, size, reg1);
  941. sign_extend(list, size, reg2);
  942. list.concat(taicpu.op_reg_reg(A_ADD,S_L,reg1, reg2));
  943. end
  944. else
  945. begin
  946. list.concat(taicpu.op_reg_reg(A_ADD,TCGSize2OpSize[size],reg1, reg2));
  947. end;
  948. end;
  949. OP_AND,OP_OR,
  950. OP_SAR,OP_SHL,
  951. OP_SHR,OP_SUB,OP_XOR :
  952. begin
  953. { load to data registers }
  954. if (isaddressregister(reg1)) then
  955. begin
  956. hreg1 := getintregister(list,OS_INT);
  957. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  958. end
  959. else
  960. hreg1 := reg1;
  961. if (isaddressregister(reg2)) then
  962. begin
  963. hreg2:= getintregister(list,OS_INT);
  964. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  965. end
  966. else
  967. hreg2 := reg2;
  968. if current_settings.cputype = cpu_ColdFire then
  969. begin
  970. { operation only allowed only a longword }
  971. {!***************************************
  972. in the case of shifts, the value to
  973. shift by, should already be valid, so
  974. no need to sign extend the value
  975. !
  976. }
  977. if op in [OP_AND,OP_OR,OP_SUB,OP_XOR] then
  978. sign_extend(list, size, hreg1);
  979. sign_extend(list, size, hreg2);
  980. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],S_L,hreg1, hreg2));
  981. end
  982. else
  983. begin
  984. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg1, hreg2));
  985. end;
  986. { move back result into destination register }
  987. if reg2 <> hreg2 then
  988. begin
  989. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  990. end;
  991. end;
  992. OP_DIV :
  993. begin
  994. internalerror(20020816);
  995. end;
  996. OP_IDIV :
  997. begin
  998. internalerror(20020816);
  999. end;
  1000. OP_IMUL :
  1001. begin
  1002. sign_extend(list, size,reg1);
  1003. sign_extend(list, size,reg2);
  1004. if current_settings.cputype = cpu_MC68000 then
  1005. begin
  1006. r:=NR_D0;
  1007. r2:=NR_D1;
  1008. cg.getcpuregister(list,NR_D0);
  1009. cg.getcpuregister(list,NR_D1);
  1010. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1, r));
  1011. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2, r2));
  1012. cg.a_call_name(list,'FPC_MUL_LONGINT',false);
  1013. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg2));
  1014. cg.ungetcpuregister(list,r);
  1015. cg.ungetcpuregister(list,r2);
  1016. end
  1017. else
  1018. begin
  1019. // writeln('doing 68020');
  1020. if (isaddressregister(reg1)) then
  1021. hreg1 := getintregister(list,OS_INT)
  1022. else
  1023. hreg1 := reg1;
  1024. if (isaddressregister(reg2)) then
  1025. hreg2:= getintregister(list,OS_INT)
  1026. else
  1027. hreg2 := reg2;
  1028. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  1029. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  1030. list.concat(taicpu.op_reg_reg(A_MULS,S_L,reg1,reg2));
  1031. { move back result into destination register }
  1032. if reg2 <> hreg2 then
  1033. begin
  1034. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  1035. end;
  1036. end;
  1037. end;
  1038. OP_MUL :
  1039. begin
  1040. sign_extend(list, size,reg1);
  1041. sign_extend(list, size,reg2);
  1042. if current_settings.cputype <> cpu_MC68020 then
  1043. begin
  1044. r:=NR_D0;
  1045. r2:=NR_D1;
  1046. cg.getcpuregister(list,NR_D0);
  1047. cg.getcpuregister(list,NR_D1);
  1048. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1, r));
  1049. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2, r2));
  1050. cg.a_call_name(list,'FPC_MUL_DWORD',false);
  1051. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,r, reg2));
  1052. cg.ungetcpuregister(list,r);
  1053. cg.ungetcpuregister(list,r2);
  1054. end
  1055. else
  1056. begin
  1057. if (isaddressregister(reg1)) then
  1058. begin
  1059. hreg1 := cg.getintregister(list,OS_INT);
  1060. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1));
  1061. end
  1062. else
  1063. hreg1 := reg1;
  1064. if (isaddressregister(reg2)) then
  1065. begin
  1066. hreg2:= cg.getintregister(list,OS_INT);
  1067. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  1068. end
  1069. else
  1070. hreg2 := reg2;
  1071. list.concat(taicpu.op_reg_reg(A_MULU,S_L,reg1,reg2));
  1072. { move back result into destination register }
  1073. if reg2<>hreg2 then
  1074. begin
  1075. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  1076. end;
  1077. end;
  1078. end;
  1079. OP_NEG,
  1080. OP_NOT :
  1081. Begin
  1082. { if there are two operands, move the register,
  1083. since the operation will only be done on the result
  1084. register.
  1085. }
  1086. if reg1 <> NR_NO then
  1087. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,reg1,reg2);
  1088. if (isaddressregister(reg2)) then
  1089. begin
  1090. hreg2 := getintregister(list,OS_INT);
  1091. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2));
  1092. end
  1093. else
  1094. hreg2 := reg2;
  1095. { coldfire only supports long version }
  1096. if current_settings.cputype = cpu_ColdFire then
  1097. begin
  1098. sign_extend(list, size,hreg2);
  1099. list.concat(taicpu.op_reg(topcg2tasmop[op],S_L,hreg2));
  1100. end
  1101. else
  1102. begin
  1103. list.concat(taicpu.op_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg2));
  1104. end;
  1105. if reg2 <> hreg2 then
  1106. begin
  1107. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2));
  1108. end;
  1109. end;
  1110. else
  1111. internalerror(20020729);
  1112. end;
  1113. end;
  1114. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1115. l : tasmlabel);
  1116. var
  1117. hregister : tregister;
  1118. begin
  1119. if a = 0 then
  1120. begin
  1121. if (current_settings.cputype = cpu_MC68000) and isaddressregister(reg) then
  1122. begin
  1123. {
  1124. 68000 does not seem to like address register for TST instruction
  1125. }
  1126. { always move to a data register }
  1127. hregister := getintregister(list,OS_INT);
  1128. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,hregister));
  1129. { sign/zero extend the register }
  1130. sign_extend(list, size,hregister);
  1131. reg:=hregister;
  1132. end;
  1133. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg));
  1134. end
  1135. else
  1136. begin
  1137. if (current_settings.cputype = cpu_ColdFire) then
  1138. begin
  1139. {
  1140. only longword comparison is supported,
  1141. and only on data registers.
  1142. }
  1143. hregister := getintregister(list,OS_INT);
  1144. { always move to a data register }
  1145. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,hregister));
  1146. { sign/zero extend the register }
  1147. sign_extend(list, size,hregister);
  1148. list.concat(taicpu.op_const_reg(A_CMPI,S_L,a,hregister));
  1149. end
  1150. else
  1151. begin
  1152. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1153. end;
  1154. end;
  1155. { emit the actual jump to the label }
  1156. a_jmp_cond(list,cmp_op,l);
  1157. end;
  1158. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1159. begin
  1160. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1161. { emit the actual jump to the label }
  1162. a_jmp_cond(list,cmp_op,l);
  1163. end;
  1164. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1165. var
  1166. ai: taicpu;
  1167. begin
  1168. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1169. ai.is_jmp := true;
  1170. list.concat(ai);
  1171. end;
  1172. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1173. var
  1174. ai: taicpu;
  1175. begin
  1176. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1177. ai.is_jmp := true;
  1178. list.concat(ai);
  1179. end;
  1180. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1181. var
  1182. ai : taicpu;
  1183. begin
  1184. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1185. ai.SetCondition(flags_to_cond(f));
  1186. ai.is_jmp := true;
  1187. list.concat(ai);
  1188. end;
  1189. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1190. var
  1191. ai : taicpu;
  1192. hreg : tregister;
  1193. begin
  1194. { move to a Dx register? }
  1195. if (isaddressregister(reg)) then
  1196. begin
  1197. hreg := getintregister(list,OS_INT);
  1198. a_load_const_reg(list,size,0,hreg);
  1199. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1200. ai.SetCondition(flags_to_cond(f));
  1201. list.concat(ai);
  1202. if (current_settings.cputype = cpu_ColdFire) then
  1203. begin
  1204. { neg.b does not exist on the Coldfire
  1205. so we need to sign extend the value
  1206. before doing a neg.l
  1207. }
  1208. list.concat(taicpu.op_reg(A_EXTB,S_L,hreg));
  1209. list.concat(taicpu.op_reg(A_NEG,S_L,hreg));
  1210. end
  1211. else
  1212. begin
  1213. list.concat(taicpu.op_reg(A_NEG,S_B,hreg));
  1214. end;
  1215. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg));
  1216. end
  1217. else
  1218. begin
  1219. a_load_const_reg(list,size,0,reg);
  1220. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  1221. ai.SetCondition(flags_to_cond(f));
  1222. list.concat(ai);
  1223. if (current_settings.cputype = cpu_ColdFire) then
  1224. begin
  1225. { neg.b does not exist on the Coldfire
  1226. so we need to sign extend the value
  1227. before doing a neg.l
  1228. }
  1229. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1230. list.concat(taicpu.op_reg(A_NEG,S_L,reg));
  1231. end
  1232. else
  1233. begin
  1234. list.concat(taicpu.op_reg(A_NEG,S_B,reg));
  1235. end;
  1236. end;
  1237. end;
  1238. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1239. var
  1240. helpsize : longint;
  1241. i : byte;
  1242. reg8,reg32 : tregister;
  1243. swap : boolean;
  1244. hregister : tregister;
  1245. iregister : tregister;
  1246. jregister : tregister;
  1247. hp1 : treference;
  1248. hp2 : treference;
  1249. hl : tasmlabel;
  1250. hl2: tasmlabel;
  1251. popaddress : boolean;
  1252. srcref,dstref : treference;
  1253. begin
  1254. popaddress := false;
  1255. // writeln('concatcopy:',len);
  1256. { this should never occur }
  1257. if len > 65535 then
  1258. internalerror(0);
  1259. hregister := getintregister(list,OS_INT);
  1260. // if delsource then
  1261. // reference_release(list,source);
  1262. { from 12 bytes movs is being used }
  1263. if {(not loadref) and} ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1264. begin
  1265. srcref := source;
  1266. dstref := dest;
  1267. helpsize:=len div 4;
  1268. { move a dword x times }
  1269. for i:=1 to helpsize do
  1270. begin
  1271. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1272. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1273. inc(srcref.offset,4);
  1274. inc(dstref.offset,4);
  1275. dec(len,4);
  1276. end;
  1277. { move a word }
  1278. if len>1 then
  1279. begin
  1280. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1281. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1282. inc(srcref.offset,2);
  1283. inc(dstref.offset,2);
  1284. dec(len,2);
  1285. end;
  1286. { move a single byte }
  1287. if len>0 then
  1288. begin
  1289. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1290. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1291. end
  1292. end
  1293. else
  1294. begin
  1295. iregister:=getaddressregister(list);
  1296. jregister:=getaddressregister(list);
  1297. { reference for move (An)+,(An)+ }
  1298. reference_reset(hp1,source.alignment);
  1299. hp1.base := iregister; { source register }
  1300. hp1.direction := dir_inc;
  1301. reference_reset(hp2,dest.alignment);
  1302. hp2.base := jregister;
  1303. hp2.direction := dir_inc;
  1304. { iregister = source }
  1305. { jregister = destination }
  1306. { if loadref then
  1307. cg.a_load_ref_reg(list,OS_INT,OS_INT,source,iregister)
  1308. else}
  1309. a_loadaddr_ref_reg(list,source,iregister);
  1310. a_loadaddr_ref_reg(list,dest,jregister);
  1311. { double word move only on 68020+ machines }
  1312. { because of possible alignment problems }
  1313. { use fast loop mode }
  1314. if (current_settings.cputype=cpu_MC68020) then
  1315. begin
  1316. helpsize := len - len mod 4;
  1317. len := len mod 4;
  1318. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize div 4,hregister));
  1319. current_asmdata.getjumplabel(hl2);
  1320. a_jmp_always(list,hl2);
  1321. current_asmdata.getjumplabel(hl);
  1322. a_label(list,hl);
  1323. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1324. a_label(list,hl2);
  1325. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1326. if len > 1 then
  1327. begin
  1328. dec(len,2);
  1329. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1330. end;
  1331. if len = 1 then
  1332. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1333. end
  1334. else
  1335. begin
  1336. { Fast 68010 loop mode with no possible alignment problems }
  1337. helpsize := len;
  1338. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize,hregister));
  1339. current_asmdata.getjumplabel(hl2);
  1340. a_jmp_always(list,hl2);
  1341. current_asmdata.getjumplabel(hl);
  1342. a_label(list,hl);
  1343. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1344. a_label(list,hl2);
  1345. if current_settings.cputype=cpu_coldfire then
  1346. begin
  1347. { Coldfire does not support DBRA }
  1348. list.concat(taicpu.op_const_reg(A_SUB,S_L,1,hregister));
  1349. list.concat(taicpu.op_sym(A_BMI,S_L,hl));
  1350. end
  1351. else
  1352. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1353. end;
  1354. { restore the registers that we have just used olny if they are used! }
  1355. if jregister = NR_A1 then
  1356. hp2.base := NR_NO;
  1357. if iregister = NR_A0 then
  1358. hp1.base := NR_NO;
  1359. // reference_release(list,hp1);
  1360. // reference_release(list,hp2);
  1361. end;
  1362. // if delsource then
  1363. // tg.ungetiftemp(list,source);
  1364. end;
  1365. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1366. begin
  1367. end;
  1368. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1369. var
  1370. r,rsp: TRegister;
  1371. ref : TReference;
  1372. begin
  1373. {$ifdef DEBUG_CHARLIE}
  1374. // writeln('proc entry, localsize:',localsize);
  1375. {$endif DEBUG_CHARLIE}
  1376. if not nostackframe then
  1377. begin
  1378. if localsize<>0 then
  1379. begin
  1380. { size can't be negative }
  1381. if (localsize < 0) then
  1382. internalerror(2006122601);
  1383. { Not to complicate the code generator too much, and since some }
  1384. { of the systems only support this format, the localsize cannot }
  1385. { exceed 32K in size. }
  1386. if (localsize > high(smallint)) then
  1387. CGMessage(cg_e_localsize_too_big);
  1388. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1389. end
  1390. else
  1391. begin
  1392. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1393. (*
  1394. { FIXME! - Carl's original code uses this method. However,
  1395. according to the 68060 users manual, a LINK is faster than
  1396. two moves. So, use a link in #0 case too, for now. I'm not
  1397. really sure tho', that LINK supports #0 disposition, but i
  1398. see no reason why it shouldn't support it. (KB) }
  1399. { when localsize = 0, use two moves, instead of link }
  1400. r:=NR_FRAME_POINTER_REG;
  1401. rsp:=NR_STACK_POINTER_REG;
  1402. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1403. ref.direction:=dir_dec;
  1404. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,r,ref));
  1405. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,rsp,r));
  1406. *)
  1407. end;
  1408. end;
  1409. end;
  1410. { procedure tcg68k.g_restore_frame_pointer(list : TAsmList);
  1411. var
  1412. r:Tregister;
  1413. begin
  1414. r:=NR_FRAME_POINTER_REG;
  1415. list.concat(taicpu.op_reg(A_UNLK,S_NO,r));
  1416. end;
  1417. }
  1418. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1419. var
  1420. r,hregister : TRegister;
  1421. localsize: tcgint;
  1422. spr : TRegister;
  1423. fpr : TRegister;
  1424. ref : TReference;
  1425. begin
  1426. if not nostackframe then
  1427. begin
  1428. localsize := current_procinfo.calc_stackframe_size;
  1429. {$ifdef DEBUG_CHARLIE}
  1430. // writeln('proc exit with stackframe, size:',localsize,' parasize:',parasize);
  1431. {$endif DEBUG_CHARLIE}
  1432. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1433. parasize := parasize - target_info.first_parm_offset; { i'm still not 100% confident that this is
  1434. correct here, but at least it looks less
  1435. hacky, and makes some sense (KB) }
  1436. if (parasize<>0) then
  1437. begin
  1438. { only 68020+ supports RTD, so this needs another code path
  1439. for 68000 and Coldfire (KB) }
  1440. { TODO: 68020+ only code generation, without fallback}
  1441. if current_settings.cputype=cpu_mc68020 then
  1442. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1443. else
  1444. begin
  1445. { We must pull the PC Counter from the stack, before }
  1446. { restoring the stack pointer, otherwise the PC would }
  1447. { point to nowhere! }
  1448. { save the PC counter (pop it from the stack) }
  1449. //hregister:=cg.getaddressregister(list);
  1450. hregister:=NR_A3;
  1451. cg.a_reg_alloc(list,hregister);
  1452. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1453. ref.direction:=dir_inc;
  1454. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1455. { can we do a quick addition ... }
  1456. r:=NR_SP;
  1457. if (parasize > 0) and (parasize < 9) then
  1458. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1459. else { nope ... }
  1460. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  1461. { restore the PC counter (push it on the stack) }
  1462. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1463. ref.direction:=dir_dec;
  1464. cg.a_reg_alloc(list,hregister);
  1465. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hregister,ref));
  1466. list.concat(taicpu.op_none(A_RTS,S_NO));
  1467. end;
  1468. end
  1469. else
  1470. list.concat(taicpu.op_none(A_RTS,S_NO));
  1471. end
  1472. else
  1473. begin
  1474. {$ifdef DEBUG_CHARLIE}
  1475. // writeln('proc exit, no stackframe');
  1476. {$endif DEBUG_CHARLIE}
  1477. list.concat(taicpu.op_none(A_RTS,S_NO));
  1478. end;
  1479. // writeln('g_proc_exit');
  1480. { Routines with the poclearstack flag set use only a ret.
  1481. also routines with parasize=0 }
  1482. (*
  1483. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1484. begin
  1485. { complex return values are removed from stack in C code PM }
  1486. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1487. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1488. else
  1489. list.concat(taicpu.op_none(A_RTS,S_NO));
  1490. end
  1491. else if (parasize=0) then
  1492. begin
  1493. list.concat(taicpu.op_none(A_RTS,S_NO));
  1494. end
  1495. else
  1496. begin
  1497. { return with immediate size possible here
  1498. signed!
  1499. RTD is not supported on the coldfire }
  1500. if (current_settings.cputype=cpu_MC68020) and (parasize<$7FFF) then
  1501. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1502. { manually restore the stack }
  1503. else
  1504. begin
  1505. { We must pull the PC Counter from the stack, before }
  1506. { restoring the stack pointer, otherwise the PC would }
  1507. { point to nowhere! }
  1508. { save the PC counter (pop it from the stack) }
  1509. hregister:=NR_A3;
  1510. cg.a_reg_alloc(list,hregister);
  1511. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1512. ref.direction:=dir_inc;
  1513. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1514. { can we do a quick addition ... }
  1515. r:=NR_SP;
  1516. if (parasize > 0) and (parasize < 9) then
  1517. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1518. else { nope ... }
  1519. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  1520. { restore the PC counter (push it on the stack) }
  1521. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1522. ref.direction:=dir_dec;
  1523. cg.a_reg_alloc(list,hregister);
  1524. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hregister,ref));
  1525. list.concat(taicpu.op_none(A_RTS,S_NO));
  1526. end;
  1527. end;
  1528. *)
  1529. end;
  1530. procedure Tcg68k.g_save_registers(list:TAsmList);
  1531. var
  1532. tosave : tcpuregisterset;
  1533. ref : treference;
  1534. begin
  1535. {!!!!!
  1536. tosave:=std_saved_registers;
  1537. { only save the registers which are not used and must be saved }
  1538. tosave:=tosave*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  1539. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1540. ref.direction:=dir_dec;
  1541. if tosave<>[] then
  1542. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,tosave,ref));
  1543. }
  1544. end;
  1545. procedure Tcg68k.g_restore_registers(list:TAsmList);
  1546. var
  1547. torestore : tcpuregisterset;
  1548. r:Tregister;
  1549. ref : treference;
  1550. begin
  1551. {!!!!!!!!
  1552. torestore:=std_saved_registers;
  1553. { should be intersected with used regs, no ? }
  1554. torestore:=torestore*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  1555. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1556. ref.direction:=dir_inc;
  1557. if torestore<>[] then
  1558. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,ref,torestore));
  1559. }
  1560. end;
  1561. {
  1562. procedure tcg68k.g_save_all_registers(list : TAsmList);
  1563. begin
  1564. end;
  1565. procedure tcg68k.g_restore_all_registers(list : TAsmList;const funcretparaloc:TCGPara);
  1566. begin
  1567. end;
  1568. }
  1569. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1570. begin
  1571. case _oldsize of
  1572. { sign extend }
  1573. OS_S8:
  1574. begin
  1575. if (isaddressregister(reg)) then
  1576. internalerror(20020729);
  1577. if (current_settings.cputype = cpu_MC68000) then
  1578. begin
  1579. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1580. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1581. end
  1582. else
  1583. begin
  1584. // list.concat(tai_comment.create(strpnew('sign extend byte')));
  1585. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1586. end;
  1587. end;
  1588. OS_S16:
  1589. begin
  1590. if (isaddressregister(reg)) then
  1591. internalerror(20020729);
  1592. // list.concat(tai_comment.create(strpnew('sign extend word')));
  1593. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1594. end;
  1595. { zero extend }
  1596. OS_8:
  1597. begin
  1598. // list.concat(tai_comment.create(strpnew('zero extend byte')));
  1599. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1600. end;
  1601. OS_16:
  1602. begin
  1603. // list.concat(tai_comment.create(strpnew('zero extend word')));
  1604. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1605. end;
  1606. end; { otherwise the size is already correct }
  1607. end;
  1608. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1609. var
  1610. ai : taicpu;
  1611. begin
  1612. if cond=OC_None then
  1613. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1614. else
  1615. begin
  1616. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1617. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1618. end;
  1619. ai.is_jmp:=true;
  1620. list.concat(ai);
  1621. end;
  1622. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1623. {
  1624. procedure loadvmttor11;
  1625. var
  1626. href : treference;
  1627. begin
  1628. reference_reset_base(href,NR_R3,0);
  1629. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  1630. end;
  1631. procedure op_onr11methodaddr;
  1632. var
  1633. href : treference;
  1634. begin
  1635. if (procdef.extnumber=$ffff) then
  1636. Internalerror(200006139);
  1637. { call/jmp vmtoffs(%eax) ; method offs }
  1638. reference_reset_base(href,NR_R11,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber));
  1639. if not((longint(href.offset) >= low(smallint)) and
  1640. (longint(href.offset) <= high(smallint))) then
  1641. begin
  1642. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  1643. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  1644. href.offset := smallint(href.offset and $ffff);
  1645. end;
  1646. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  1647. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  1648. list.concat(taicpu.op_none(A_BCTR));
  1649. end;
  1650. }
  1651. var
  1652. make_global : boolean;
  1653. begin
  1654. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1655. Internalerror(200006137);
  1656. if not assigned(procdef.struct) or
  1657. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1658. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1659. Internalerror(200006138);
  1660. if procdef.owner.symtabletype<>ObjectSymtable then
  1661. Internalerror(200109191);
  1662. make_global:=false;
  1663. if (not current_module.is_unit) or
  1664. create_smartlink or
  1665. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1666. make_global:=true;
  1667. if make_global then
  1668. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1669. else
  1670. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1671. { set param1 interface to self }
  1672. // g_adjust_self_value(list,procdef,ioffset);
  1673. { case 4 }
  1674. if (po_virtualmethod in procdef.procoptions) and
  1675. not is_objectpascal_helper(procdef.struct) then
  1676. begin
  1677. // loadvmttor11;
  1678. // op_onr11methodaddr;
  1679. end
  1680. { case 0 }
  1681. else
  1682. // list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1683. List.concat(Tai_symbol_end.Createname(labelname));
  1684. end;
  1685. {****************************************************************************}
  1686. { TCG64F68K }
  1687. {****************************************************************************}
  1688. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1689. var
  1690. hreg1, hreg2 : tregister;
  1691. opcode : tasmop;
  1692. begin
  1693. // writeln('a_op64_reg_reg');
  1694. opcode := topcg2tasmop[op];
  1695. case op of
  1696. OP_ADD :
  1697. begin
  1698. { if one of these three registers is an address
  1699. register, we'll really get into problems!
  1700. }
  1701. if isaddressregister(regdst.reglo) or
  1702. isaddressregister(regdst.reghi) or
  1703. isaddressregister(regsrc.reghi) then
  1704. internalerror(20020817);
  1705. list.concat(taicpu.op_reg_reg(A_ADD,S_L,regsrc.reglo,regdst.reglo));
  1706. list.concat(taicpu.op_reg_reg(A_ADDX,S_L,regsrc.reghi,regdst.reghi));
  1707. end;
  1708. OP_AND,OP_OR :
  1709. begin
  1710. { at least one of the registers must be a data register }
  1711. if (isaddressregister(regdst.reglo) and
  1712. isaddressregister(regsrc.reglo)) or
  1713. (isaddressregister(regsrc.reghi) and
  1714. isaddressregister(regdst.reghi))
  1715. then
  1716. internalerror(20020817);
  1717. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1718. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1719. end;
  1720. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1721. OP_IDIV,OP_DIV,
  1722. OP_IMUL,OP_MUL: internalerror(2002081701);
  1723. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1724. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1725. OP_SUB:
  1726. begin
  1727. { if one of these three registers is an address
  1728. register, we'll really get into problems!
  1729. }
  1730. if isaddressregister(regdst.reglo) or
  1731. isaddressregister(regdst.reghi) or
  1732. isaddressregister(regsrc.reghi) then
  1733. internalerror(20020817);
  1734. list.concat(taicpu.op_reg_reg(A_SUB,S_L,regsrc.reglo,regdst.reglo));
  1735. list.concat(taicpu.op_reg_reg(A_SUBX,S_L,regsrc.reghi,regdst.reghi));
  1736. end;
  1737. OP_XOR:
  1738. begin
  1739. if isaddressregister(regdst.reglo) or
  1740. isaddressregister(regsrc.reglo) or
  1741. isaddressregister(regsrc.reghi) or
  1742. isaddressregister(regdst.reghi) then
  1743. internalerror(20020817);
  1744. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reglo,regdst.reglo));
  1745. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reghi,regdst.reghi));
  1746. end;
  1747. end; { end case }
  1748. end;
  1749. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  1750. var
  1751. lowvalue : cardinal;
  1752. highvalue : cardinal;
  1753. hreg : tregister;
  1754. begin
  1755. // writeln('a_op64_const_reg');
  1756. { is it optimized out ? }
  1757. // if cg.optimize64_op_const_reg(list,op,value,reg) then
  1758. // exit;
  1759. lowvalue := cardinal(value);
  1760. highvalue:= value shr 32;
  1761. { the destination registers must be data registers }
  1762. if isaddressregister(regdst.reglo) or
  1763. isaddressregister(regdst.reghi) then
  1764. internalerror(20020817);
  1765. case op of
  1766. OP_ADD :
  1767. begin
  1768. hreg:=cg.getintregister(list,OS_INT);
  1769. list.concat(taicpu.op_const_reg(A_MOVE,S_L,highvalue,hreg));
  1770. list.concat(taicpu.op_const_reg(A_ADD,S_L,lowvalue,regdst.reglo));
  1771. list.concat(taicpu.op_reg_reg(A_ADDX,S_L,hreg,regdst.reglo));
  1772. end;
  1773. OP_AND :
  1774. begin
  1775. list.concat(taicpu.op_const_reg(A_AND,S_L,lowvalue,regdst.reglo));
  1776. list.concat(taicpu.op_const_reg(A_AND,S_L,highvalue,regdst.reglo));
  1777. end;
  1778. OP_OR :
  1779. begin
  1780. list.concat(taicpu.op_const_reg(A_OR,S_L,lowvalue,regdst.reglo));
  1781. list.concat(taicpu.op_const_reg(A_OR,S_L,highvalue,regdst.reglo));
  1782. end;
  1783. { this is handled in 1st pass for 32-bit cpus (helper call) }
  1784. OP_IDIV,OP_DIV,
  1785. OP_IMUL,OP_MUL: internalerror(2002081701);
  1786. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  1787. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1788. OP_SUB:
  1789. begin
  1790. hreg:=cg.getintregister(list,OS_INT);
  1791. list.concat(taicpu.op_const_reg(A_MOVE,S_L,highvalue,hreg));
  1792. list.concat(taicpu.op_const_reg(A_SUB,S_L,lowvalue,regdst.reglo));
  1793. list.concat(taicpu.op_reg_reg(A_SUBX,S_L,hreg,regdst.reglo));
  1794. end;
  1795. OP_XOR:
  1796. begin
  1797. list.concat(taicpu.op_const_reg(A_EOR,S_L,lowvalue,regdst.reglo));
  1798. list.concat(taicpu.op_const_reg(A_EOR,S_L,highvalue,regdst.reglo));
  1799. end;
  1800. end; { end case }
  1801. end;
  1802. procedure create_codegen;
  1803. begin
  1804. cg := tcg68k.create;
  1805. cg64 :=tcg64f68k.create;
  1806. end;
  1807. end.