cgcpu.pas 87 KB

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