cgcpu.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265
  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. protected
  73. function fixref(list: TAsmList; var ref: treference): boolean;
  74. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  75. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  76. private
  77. { # Sign or zero extend the register to a full 32-bit value.
  78. The new value is left in the same register.
  79. }
  80. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  81. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  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. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),register));
  697. end
  698. else
  699. if a = 0 then
  700. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  701. else
  702. begin
  703. if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  704. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  705. else
  706. begin
  707. { ISA B/C Coldfire has sign extend/zero extend moves }
  708. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  709. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  710. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  711. begin
  712. if size in [OS_16, OS_8] then
  713. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  714. else
  715. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  716. end
  717. else
  718. begin
  719. { clear the register first, for unsigned and positive values, so
  720. we don't need to zero extend after }
  721. if (size in [OS_16,OS_8]) or
  722. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  723. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  724. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  725. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  726. if (size in [OS_S16,OS_S8]) and (a < 0) then
  727. sign_extend(list,size,register);
  728. end;
  729. end;
  730. end;
  731. end;
  732. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  733. var
  734. hreg : tregister;
  735. href : treference;
  736. begin
  737. href:=ref;
  738. fixref(list,href);
  739. { for coldfire we need to go through a temporary register if we have a
  740. offset, index or symbol given }
  741. if (current_settings.cputype in cpu_coldfire) and
  742. (
  743. (href.offset<>0) or
  744. { TODO : check whether we really need this second condition }
  745. (href.index<>NR_NO) or
  746. assigned(href.symbol)
  747. ) then
  748. begin
  749. hreg:=getintregister(list,tosize);
  750. list.concat(taicpu.op_const_reg(A_MOVE,tcgsize2opsize[tosize],longint(a),hreg));
  751. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  752. end
  753. else
  754. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  755. end;
  756. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  757. var
  758. href : treference;
  759. size : tcgsize;
  760. begin
  761. href := ref;
  762. fixref(list,href);
  763. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  764. size:=fromsize
  765. else
  766. size:=tosize;
  767. { move to destination reference }
  768. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[size],register,href));
  769. end;
  770. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  771. var
  772. aref: treference;
  773. bref: treference;
  774. tmpref : treference;
  775. dofix : boolean;
  776. hreg: TRegister;
  777. begin
  778. aref := sref;
  779. bref := dref;
  780. fixref(list,aref);
  781. fixref(list,bref);
  782. if TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize] then
  783. begin
  784. { if we need to change the size then always use a temporary
  785. register }
  786. hreg:=getintregister(list,fromsize);
  787. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  788. sign_extend(list,fromsize,tosize,hreg);
  789. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  790. exit;
  791. end;
  792. { Coldfire dislikes certain move combinations }
  793. if current_settings.cputype in cpu_coldfire then
  794. begin
  795. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  796. dofix:=false;
  797. if { (d16,Ax) and (d8,Ax,Xi) }
  798. (
  799. (aref.base<>NR_NO) and
  800. (
  801. (aref.index<>NR_NO) or
  802. (aref.offset<>0)
  803. )
  804. ) or
  805. { (xxx) }
  806. assigned(aref.symbol) then
  807. begin
  808. if aref.index<>NR_NO then
  809. begin
  810. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  811. (
  812. (bref.base<>NR_NO) and
  813. (
  814. (bref.index<>NR_NO) or
  815. (bref.offset<>0)
  816. )
  817. ) or
  818. { (xxx) }
  819. assigned(bref.symbol);
  820. end
  821. else
  822. { offset <> 0, but no index }
  823. begin
  824. dofix:={ (d8,Ax,Xi) }
  825. (
  826. (bref.base<>NR_NO) and
  827. (bref.index<>NR_NO)
  828. ) or
  829. { (xxx) }
  830. assigned(bref.symbol);
  831. end;
  832. end;
  833. if dofix then
  834. begin
  835. hreg:=getaddressregister(list);
  836. reference_reset_base(tmpref,hreg,0,0);
  837. list.concat(taicpu.op_ref_reg(A_LEA,S_L,aref,hreg));
  838. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],tmpref,bref));
  839. exit;
  840. end;
  841. end;
  842. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  843. end;
  844. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  845. var
  846. instr : taicpu;
  847. begin
  848. { move to destination register }
  849. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,reg2);
  850. add_move_instruction(instr);
  851. list.concat(instr);
  852. sign_extend(list, fromsize, tosize, reg2);
  853. end;
  854. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  855. var
  856. href : treference;
  857. size : tcgsize;
  858. begin
  859. href:=ref;
  860. fixref(list,href);
  861. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  862. size:=fromsize
  863. else
  864. size:=tosize;
  865. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[size],href,register));
  866. { extend the value in the register }
  867. sign_extend(list, fromsize, tosize, register);
  868. end;
  869. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  870. var
  871. href : treference;
  872. // p: pointer;
  873. begin
  874. { TODO: FIX ME!!! take a look on this mess again...}
  875. // if getregtype(r)=R_ADDRESSREGISTER then
  876. // begin
  877. // writeln('address reg?!?');
  878. // p:=nil; dword(p^):=0; {DEBUG CODE... :D )
  879. // internalerror(2002072901);
  880. // end;
  881. href:=ref;
  882. fixref(list, href);
  883. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  884. end;
  885. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  886. var
  887. instr : taicpu;
  888. begin
  889. { in emulation mode, only 32-bit single is supported }
  890. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  891. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2)
  892. else
  893. instr:=taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[tosize],reg1,reg2);
  894. add_move_instruction(instr);
  895. list.concat(instr);
  896. end;
  897. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  898. var
  899. opsize : topsize;
  900. href : treference;
  901. tmpreg : tregister;
  902. begin
  903. opsize := tcgsize2opsize[fromsize];
  904. { extended is not supported, since it is not available on Coldfire }
  905. if opsize = S_FX then
  906. internalerror(20020729);
  907. href := ref;
  908. fixref(list,href);
  909. { in emulation mode, only 32-bit single is supported }
  910. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  911. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  912. else
  913. begin
  914. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  915. if (tosize < fromsize) then
  916. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  917. end;
  918. end;
  919. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  920. var
  921. opsize : topsize;
  922. begin
  923. opsize := tcgsize2opsize[tosize];
  924. { extended is not supported, since it is not available on Coldfire }
  925. if opsize = S_FX then
  926. internalerror(20020729);
  927. { in emulation mode, only 32-bit single is supported }
  928. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  929. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  930. else
  931. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  932. end;
  933. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  934. begin
  935. case cgpara.location^.loc of
  936. LOC_REFERENCE,LOC_CREFERENCE:
  937. begin
  938. case size of
  939. OS_F64:
  940. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  941. OS_F32:
  942. a_load_ref_cgpara(list,size,ref,cgpara);
  943. else
  944. internalerror(2013021201);
  945. end;
  946. end;
  947. else
  948. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  949. end;
  950. end;
  951. procedure tcg68k.a_loadmm_reg_reg(list: TAsmList;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle);
  952. begin
  953. internalerror(20020729);
  954. end;
  955. procedure tcg68k.a_loadmm_ref_reg(list: TAsmList;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle);
  956. begin
  957. internalerror(20020729);
  958. end;
  959. procedure tcg68k.a_loadmm_reg_ref(list: TAsmList;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle);
  960. begin
  961. internalerror(20020729);
  962. end;
  963. procedure tcg68k.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle);
  964. begin
  965. internalerror(20020729);
  966. end;
  967. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  968. var
  969. scratch_reg : tregister;
  970. scratch_reg2: tregister;
  971. opcode : tasmop;
  972. r,r2 : Tregister;
  973. instr : taicpu;
  974. paraloc1,paraloc2,paraloc3 : tcgpara;
  975. begin
  976. optimize_op_const(size, op, a);
  977. opcode := topcg2tasmop[op];
  978. case op of
  979. OP_NONE :
  980. begin
  981. { Opcode is optimized away }
  982. end;
  983. OP_MOVE :
  984. begin
  985. { Optimized, replaced with a simple load }
  986. a_load_const_reg(list,size,a,reg);
  987. end;
  988. OP_ADD,
  989. OP_SUB:
  990. begin
  991. { add/sub works the same way, so have it unified here }
  992. if (a >= 1) and (a <= 8) and not isaddressregister(reg) then
  993. if (op = OP_ADD) then
  994. opcode:=A_ADDQ
  995. else
  996. opcode:=A_SUBQ;
  997. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  998. end;
  999. OP_AND,
  1000. OP_OR,
  1001. OP_XOR:
  1002. begin
  1003. scratch_reg := force_to_dataregister(list, size, reg);
  1004. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1005. move_if_needed(list, size, scratch_reg, reg);
  1006. end;
  1007. OP_DIV,
  1008. OP_IDIV:
  1009. begin
  1010. internalerror(20020816);
  1011. end;
  1012. OP_MUL,
  1013. OP_IMUL:
  1014. begin
  1015. { NOTE: better have this as fast as possible on every CPU in all cases,
  1016. because the compiler uses OP_IMUL for array indexing... (KB) }
  1017. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1018. if current_settings.cputype in cpu_coldfire then
  1019. begin
  1020. { move const to a register first }
  1021. scratch_reg := getintregister(list,OS_INT);
  1022. a_load_const_reg(list, size, a, scratch_reg);
  1023. { do the multiplication }
  1024. scratch_reg2 := force_to_dataregister(list, size, reg);
  1025. sign_extend(list, size, scratch_reg2);
  1026. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1027. { move the value back to the original register }
  1028. move_if_needed(list, size, scratch_reg2, reg);
  1029. end
  1030. else
  1031. begin
  1032. if current_settings.cputype = cpu_mc68020 then
  1033. begin
  1034. { do the multiplication }
  1035. scratch_reg := force_to_dataregister(list, size, reg);
  1036. sign_extend(list, size, scratch_reg);
  1037. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1038. { move the value back to the original register }
  1039. move_if_needed(list, size, scratch_reg, reg);
  1040. end
  1041. else
  1042. { Fallback branch, plain 68000 for now }
  1043. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1044. if op = OP_MUL then
  1045. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1046. else
  1047. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1048. end;
  1049. end;
  1050. OP_SAR,
  1051. OP_SHL,
  1052. OP_SHR :
  1053. begin
  1054. scratch_reg := force_to_dataregister(list, size, reg);
  1055. sign_extend(list, size, scratch_reg);
  1056. if (a >= 1) and (a <= 8) then
  1057. begin
  1058. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1059. end
  1060. else
  1061. begin
  1062. { move const to a register first }
  1063. scratch_reg2 := getintregister(list,OS_INT);
  1064. a_load_const_reg(list, size, a, scratch_reg2);
  1065. { do the operation }
  1066. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1067. end;
  1068. { move the value back to the original register }
  1069. move_if_needed(list, size, scratch_reg, reg);
  1070. end;
  1071. else
  1072. internalerror(20020729);
  1073. end;
  1074. end;
  1075. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1076. var
  1077. opcode: tasmop;
  1078. opsize: topsize;
  1079. href : treference;
  1080. begin
  1081. optimize_op_const(size, op, a);
  1082. opcode := topcg2tasmop[op];
  1083. opsize := TCGSize2OpSize[size];
  1084. { on ColdFire all arithmetic operations are only possible on 32bit }
  1085. if not isvalidreference(ref) or
  1086. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1087. and not (op in [OP_NONE,OP_MOVE])) then
  1088. begin
  1089. inherited;
  1090. exit;
  1091. end;
  1092. href:=ref;
  1093. fixref(list,href);
  1094. case op of
  1095. OP_NONE :
  1096. begin
  1097. { opcode was optimized away }
  1098. end;
  1099. OP_MOVE :
  1100. begin
  1101. { Optimized, replaced with a simple load }
  1102. a_load_const_ref(list,size,a,ref);
  1103. end;
  1104. OP_ADD,
  1105. OP_SUB :
  1106. begin
  1107. { add/sub works the same way, so have it unified here }
  1108. if (a >= 1) and (a <= 8) then
  1109. begin
  1110. if (op = OP_ADD) then
  1111. opcode:=A_ADDQ
  1112. else
  1113. opcode:=A_SUBQ;
  1114. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1115. end
  1116. else
  1117. if not(current_settings.cputype in cpu_coldfire) then
  1118. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1119. else
  1120. { on ColdFire, ADDI/SUBI cannot act on memory
  1121. so we can only go through a register }
  1122. inherited;
  1123. end;
  1124. else begin
  1125. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1126. inherited;
  1127. end;
  1128. end;
  1129. end;
  1130. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister);
  1131. var
  1132. hreg1, hreg2,r,r2: tregister;
  1133. instr : taicpu;
  1134. opcode : tasmop;
  1135. opsize : topsize;
  1136. begin
  1137. opcode := topcg2tasmop[op];
  1138. if current_settings.cputype in cpu_coldfire then
  1139. opsize := S_L
  1140. else
  1141. opsize := TCGSize2OpSize[size];
  1142. case op of
  1143. OP_ADD,
  1144. OP_SUB:
  1145. begin
  1146. if current_settings.cputype in cpu_coldfire then
  1147. begin
  1148. { operation only allowed only a longword }
  1149. sign_extend(list, size, reg1);
  1150. sign_extend(list, size, reg2);
  1151. end;
  1152. list.concat(taicpu.op_reg_reg(opcode, opsize, reg1, reg2));
  1153. end;
  1154. OP_AND,OP_OR,
  1155. OP_SAR,OP_SHL,
  1156. OP_SHR,OP_XOR:
  1157. begin
  1158. { load to data registers }
  1159. hreg1 := force_to_dataregister(list, size, reg1);
  1160. hreg2 := force_to_dataregister(list, size, reg2);
  1161. if current_settings.cputype in cpu_coldfire then
  1162. begin
  1163. { operation only allowed only a longword }
  1164. {!***************************************
  1165. in the case of shifts, the value to
  1166. shift by, should already be valid, so
  1167. no need to sign extend the value
  1168. !
  1169. }
  1170. if op in [OP_AND,OP_OR,OP_XOR] then
  1171. sign_extend(list, size, hreg1);
  1172. sign_extend(list, size, hreg2);
  1173. end;
  1174. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1175. { move back result into destination register }
  1176. move_if_needed(list, size, hreg2, reg2);
  1177. end;
  1178. OP_DIV,
  1179. OP_IDIV :
  1180. begin
  1181. internalerror(20020816);
  1182. end;
  1183. OP_MUL,
  1184. OP_IMUL:
  1185. begin
  1186. if (current_settings.cputype <> cpu_mc68020) and
  1187. (not (current_settings.cputype in cpu_coldfire)) then
  1188. if op = OP_MUL then
  1189. call_rtl_mul_reg_reg(list,reg1,reg2,'fpc_mul_dword')
  1190. else
  1191. call_rtl_mul_reg_reg(list,reg1,reg2,'fpc_mul_longint')
  1192. else
  1193. begin
  1194. { 68020+ and ColdFire codepath, probably could be improved }
  1195. hreg1 := force_to_dataregister(list, size, reg1);
  1196. hreg2 := force_to_dataregister(list, size, reg2);
  1197. sign_extend(list, size, hreg1);
  1198. sign_extend(list, size, hreg2);
  1199. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1200. { move back result into destination register }
  1201. move_if_needed(list, size, hreg2, reg2);
  1202. end;
  1203. end;
  1204. OP_NEG,
  1205. OP_NOT :
  1206. begin
  1207. { if there are two operands, move the register,
  1208. since the operation will only be done on the result
  1209. register. }
  1210. if reg1 <> NR_NO then
  1211. hreg1:=reg1
  1212. else
  1213. hreg1:=reg2;
  1214. hreg2 := force_to_dataregister(list, size, hreg1);
  1215. { coldfire only supports long version }
  1216. if current_settings.cputype in cpu_ColdFire then
  1217. sign_extend(list, size, hreg2);
  1218. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1219. { move back the result to the result register if needed }
  1220. move_if_needed(list, size, hreg2, reg2);
  1221. end;
  1222. else
  1223. internalerror(20020729);
  1224. end;
  1225. end;
  1226. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1227. var
  1228. opcode : tasmop;
  1229. opsize : topsize;
  1230. begin
  1231. opcode := topcg2tasmop[op];
  1232. opsize := TCGSize2OpSize[size];
  1233. { on ColdFire all arithmetic operations are only possible on 32bit
  1234. and addressing modes are limited }
  1235. if not isvalidreference(ref) or
  1236. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1237. begin
  1238. inherited;
  1239. exit;
  1240. end;
  1241. case op of
  1242. OP_ADD,
  1243. OP_SUB :
  1244. begin
  1245. { add/sub works the same way, so have it unified here }
  1246. list.concat(taicpu.op_reg_ref(opcode, opsize, reg, ref));
  1247. end;
  1248. else begin
  1249. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1250. inherited;
  1251. end;
  1252. end;
  1253. end;
  1254. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1255. l : tasmlabel);
  1256. var
  1257. hregister : tregister;
  1258. instr : taicpu;
  1259. need_temp_reg : boolean;
  1260. temp_size: topsize;
  1261. begin
  1262. need_temp_reg := false;
  1263. { plain 68000 doesn't support address registers for TST }
  1264. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1265. (a = 0) and isaddressregister(reg);
  1266. { ColdFire doesn't support address registers for CMPI }
  1267. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1268. and (a <> 0) and isaddressregister(reg));
  1269. if need_temp_reg then
  1270. begin
  1271. hregister := getintregister(list,OS_INT);
  1272. temp_size := TCGSize2OpSize[size];
  1273. if temp_size < S_W then
  1274. temp_size := S_W;
  1275. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1276. add_move_instruction(instr);
  1277. list.concat(instr);
  1278. reg := hregister;
  1279. { do sign extension if size had to be modified }
  1280. if temp_size <> TCGSize2OpSize[size] then
  1281. begin
  1282. sign_extend(list, size, reg);
  1283. size:=OS_INT;
  1284. end;
  1285. end;
  1286. if a = 0 then
  1287. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1288. else
  1289. begin
  1290. { ColdFire also needs S_L for CMPI }
  1291. if current_settings.cputype in cpu_coldfire then
  1292. begin
  1293. sign_extend(list, size, reg);
  1294. size:=OS_INT;
  1295. end;
  1296. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1297. end;
  1298. { emit the actual jump to the label }
  1299. a_jmp_cond(list,cmp_op,l);
  1300. end;
  1301. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1302. begin
  1303. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1304. { emit the actual jump to the label }
  1305. a_jmp_cond(list,cmp_op,l);
  1306. end;
  1307. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1308. var
  1309. ai: taicpu;
  1310. begin
  1311. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1312. ai.is_jmp := true;
  1313. list.concat(ai);
  1314. end;
  1315. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1316. var
  1317. ai: taicpu;
  1318. begin
  1319. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1320. ai.is_jmp := true;
  1321. list.concat(ai);
  1322. end;
  1323. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1324. var
  1325. ai : taicpu;
  1326. begin
  1327. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1328. ai.SetCondition(flags_to_cond(f));
  1329. ai.is_jmp := true;
  1330. list.concat(ai);
  1331. end;
  1332. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1333. var
  1334. ai : taicpu;
  1335. hreg : tregister;
  1336. instr : taicpu;
  1337. begin
  1338. { move to a Dx register? }
  1339. if (isaddressregister(reg)) then
  1340. hreg:=getintregister(list,OS_INT)
  1341. else
  1342. hreg:=reg;
  1343. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1344. ai.SetCondition(flags_to_cond(f));
  1345. list.concat(ai);
  1346. { Scc stores a complete byte of 1s, but the compiler expects only one
  1347. bit set, so ensure this is the case }
  1348. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1349. if hreg<>reg then
  1350. begin
  1351. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1352. add_move_instruction(instr);
  1353. list.concat(instr);
  1354. end;
  1355. end;
  1356. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1357. var
  1358. helpsize : longint;
  1359. i : byte;
  1360. reg8,reg32 : tregister;
  1361. swap : boolean;
  1362. hregister : tregister;
  1363. iregister : tregister;
  1364. jregister : tregister;
  1365. hp1 : treference;
  1366. hp2 : treference;
  1367. hl : tasmlabel;
  1368. hl2: tasmlabel;
  1369. popaddress : boolean;
  1370. srcref,dstref : treference;
  1371. alignsize : tcgsize;
  1372. orglen : tcgint;
  1373. begin
  1374. popaddress := false;
  1375. // writeln('concatcopy:',len);
  1376. { this should never occur }
  1377. if len > 65535 then
  1378. internalerror(0);
  1379. hregister := getintregister(list,OS_INT);
  1380. // if delsource then
  1381. // reference_release(list,source);
  1382. orglen:=len;
  1383. { from 12 bytes movs is being used }
  1384. if {(not loadref) and} ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1385. begin
  1386. srcref := source;
  1387. dstref := dest;
  1388. helpsize:=len div 4;
  1389. { move a dword x times }
  1390. for i:=1 to helpsize do
  1391. begin
  1392. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1393. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1394. inc(srcref.offset,4);
  1395. inc(dstref.offset,4);
  1396. dec(len,4);
  1397. end;
  1398. { move a word }
  1399. if len>1 then
  1400. begin
  1401. if (orglen<sizeof(aint)) and
  1402. (source.base=NR_FRAME_POINTER_REG) and
  1403. (source.offset>0) then
  1404. { copy of param to local location }
  1405. alignsize:=OS_INT
  1406. else
  1407. alignsize:=OS_16;
  1408. a_load_ref_reg(list,alignsize,alignsize,srcref,hregister);
  1409. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1410. inc(srcref.offset,2);
  1411. inc(dstref.offset,2);
  1412. dec(len,2);
  1413. end;
  1414. { move a single byte }
  1415. if len>0 then
  1416. begin
  1417. if (orglen<sizeof(aint)) and
  1418. (source.base=NR_FRAME_POINTER_REG) and
  1419. (source.offset>0) then
  1420. { copy of param to local location }
  1421. alignsize:=OS_INT
  1422. else
  1423. alignsize:=OS_8;
  1424. a_load_ref_reg(list,alignsize,alignsize,srcref,hregister);
  1425. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1426. end
  1427. end
  1428. else
  1429. begin
  1430. iregister:=getaddressregister(list);
  1431. jregister:=getaddressregister(list);
  1432. { reference for move (An)+,(An)+ }
  1433. reference_reset(hp1,source.alignment);
  1434. hp1.base := iregister; { source register }
  1435. hp1.direction := dir_inc;
  1436. reference_reset(hp2,dest.alignment);
  1437. hp2.base := jregister;
  1438. hp2.direction := dir_inc;
  1439. { iregister = source }
  1440. { jregister = destination }
  1441. { if loadref then
  1442. cg.a_load_ref_reg(list,OS_INT,OS_INT,source,iregister)
  1443. else}
  1444. a_loadaddr_ref_reg(list,source,iregister);
  1445. a_loadaddr_ref_reg(list,dest,jregister);
  1446. { double word move only on 68020+ machines }
  1447. { because of possible alignment problems }
  1448. { use fast loop mode }
  1449. if (current_settings.cputype=cpu_MC68020) then
  1450. begin
  1451. helpsize := len - len mod 4;
  1452. len := len mod 4;
  1453. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize div 4,hregister));
  1454. current_asmdata.getjumplabel(hl2);
  1455. a_jmp_always(list,hl2);
  1456. current_asmdata.getjumplabel(hl);
  1457. a_label(list,hl);
  1458. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1459. a_label(list,hl2);
  1460. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1461. if len > 1 then
  1462. begin
  1463. dec(len,2);
  1464. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1465. end;
  1466. if len = 1 then
  1467. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1468. end
  1469. else
  1470. begin
  1471. { Fast 68010 loop mode with no possible alignment problems }
  1472. helpsize := len;
  1473. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize,hregister));
  1474. current_asmdata.getjumplabel(hl2);
  1475. a_jmp_always(list,hl2);
  1476. current_asmdata.getjumplabel(hl);
  1477. a_label(list,hl);
  1478. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1479. a_label(list,hl2);
  1480. if current_settings.cputype in cpu_coldfire then
  1481. begin
  1482. { Coldfire does not support DBRA }
  1483. list.concat(taicpu.op_const_reg(A_SUB,S_L,1,hregister));
  1484. list.concat(taicpu.op_sym(A_BPL,S_L,hl));
  1485. end
  1486. else
  1487. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1488. end;
  1489. { restore the registers that we have just used olny if they are used! }
  1490. if jregister = NR_A1 then
  1491. hp2.base := NR_NO;
  1492. if iregister = NR_A0 then
  1493. hp1.base := NR_NO;
  1494. // reference_release(list,hp1);
  1495. // reference_release(list,hp2);
  1496. end;
  1497. // if delsource then
  1498. // tg.ungetiftemp(list,source);
  1499. end;
  1500. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1501. begin
  1502. end;
  1503. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1504. var
  1505. r,rsp: TRegister;
  1506. ref : TReference;
  1507. begin
  1508. if not nostackframe then
  1509. begin
  1510. if localsize<>0 then
  1511. begin
  1512. { size can't be negative }
  1513. if (localsize < 0) then
  1514. internalerror(2006122601);
  1515. { Not to complicate the code generator too much, and since some }
  1516. { of the systems only support this format, the localsize cannot }
  1517. { exceed 32K in size. }
  1518. if (localsize > high(smallint)) then
  1519. CGMessage(cg_e_localsize_too_big);
  1520. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1521. end
  1522. else
  1523. begin
  1524. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1525. (*
  1526. { FIXME! - Carl's original code uses this method. However,
  1527. according to the 68060 users manual, a LINK is faster than
  1528. two moves. So, use a link in #0 case too, for now. I'm not
  1529. really sure tho', that LINK supports #0 disposition, but i
  1530. see no reason why it shouldn't support it. (KB) }
  1531. { when localsize = 0, use two moves, instead of link }
  1532. r:=NR_FRAME_POINTER_REG;
  1533. rsp:=NR_STACK_POINTER_REG;
  1534. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1535. ref.direction:=dir_dec;
  1536. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,r,ref));
  1537. instr:=taicpu.op_reg_reg(A_MOVE,S_L,rsp,r);
  1538. add_move_instruction(instr); mwould also be needed
  1539. list.concat(instr);
  1540. *)
  1541. end;
  1542. end;
  1543. end;
  1544. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1545. var
  1546. r,hregister : TRegister;
  1547. spr : TRegister;
  1548. fpr : TRegister;
  1549. ref : TReference;
  1550. begin
  1551. if not nostackframe then
  1552. begin
  1553. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1554. parasize := parasize - target_info.first_parm_offset; { i'm still not 100% confident that this is
  1555. correct here, but at least it looks less
  1556. hacky, and makes some sense (KB) }
  1557. { if parasize is less than zero here, we probably have a cdecl function.
  1558. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1559. 68k GCC uses two different methods to free the stack, depending if the target
  1560. architecture supports RTD or not, and one does callee side, the other does
  1561. caller side free, which looks like a PITA to support. We have to figure this
  1562. out later. More info welcomed. (KB) }
  1563. if (parasize > 0) then
  1564. begin
  1565. if current_settings.cputype=cpu_mc68020 then
  1566. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1567. else
  1568. begin
  1569. { We must pull the PC Counter from the stack, before }
  1570. { restoring the stack pointer, otherwise the PC would }
  1571. { point to nowhere! }
  1572. { Instead of doing a slow copy of the return address while trying }
  1573. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1574. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1575. { return to the caller with the paras freed. (KB) }
  1576. hregister:=NR_A0;
  1577. cg.a_reg_alloc(list,hregister);
  1578. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1579. ref.direction:=dir_inc;
  1580. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1581. r:=NR_SP;
  1582. { can we do a quick addition ... }
  1583. if (parasize > 0) and (parasize < 9) then
  1584. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1585. else { nope ... }
  1586. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  1587. reference_reset_base(ref,hregister,0,4);
  1588. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1589. end;
  1590. end
  1591. else
  1592. list.concat(taicpu.op_none(A_RTS,S_NO));
  1593. end
  1594. else
  1595. begin
  1596. list.concat(taicpu.op_none(A_RTS,S_NO));
  1597. end;
  1598. { Routines with the poclearstack flag set use only a ret.
  1599. also routines with parasize=0 }
  1600. { TODO: figure out if these are still relevant to us (KB) }
  1601. (*
  1602. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1603. begin
  1604. { complex return values are removed from stack in C code PM }
  1605. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1606. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1607. else
  1608. list.concat(taicpu.op_none(A_RTS,S_NO));
  1609. end
  1610. else if (parasize=0) then
  1611. begin
  1612. list.concat(taicpu.op_none(A_RTS,S_NO));
  1613. end
  1614. else
  1615. *)
  1616. end;
  1617. procedure tcg68k.g_save_registers(list:TAsmList);
  1618. var
  1619. dataregs: tcpuregisterset;
  1620. addrregs: tcpuregisterset;
  1621. href : treference;
  1622. hreg : tregister;
  1623. size : longint;
  1624. r : integer;
  1625. begin
  1626. { The code generated by the section below, particularly the movem.l
  1627. instruction is known to cause an issue when compiled by some GNU
  1628. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1629. when you run into this problem, just call inherited here instead
  1630. to skip the movem.l generation. But better just use working GNU
  1631. AS version instead. (KB) }
  1632. dataregs:=[];
  1633. addrregs:=[];
  1634. { calculate temp. size }
  1635. size:=0;
  1636. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1637. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1638. begin
  1639. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1640. inc(size,sizeof(aint));
  1641. dataregs:=dataregs + [saved_standard_registers[r]];
  1642. end;
  1643. if uses_registers(R_ADDRESSREGISTER) then
  1644. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1645. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1646. begin
  1647. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1648. inc(size,sizeof(aint));
  1649. addrregs:=addrregs + [saved_address_registers[r]];
  1650. end;
  1651. { 68k has no MM registers }
  1652. if uses_registers(R_MMREGISTER) then
  1653. internalerror(2014030201);
  1654. if size>0 then
  1655. begin
  1656. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1657. include(current_procinfo.flags,pi_has_saved_regs);
  1658. { Copy registers to temp }
  1659. href:=current_procinfo.save_regs_ref;
  1660. if size = sizeof(aint) then
  1661. a_load_reg_ref(list, OS_32, OS_32, hreg, href)
  1662. else
  1663. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,href));
  1664. end;
  1665. end;
  1666. procedure tcg68k.g_restore_registers(list:TAsmList);
  1667. var
  1668. dataregs: tcpuregisterset;
  1669. addrregs: tcpuregisterset;
  1670. href : treference;
  1671. r : integer;
  1672. hreg : tregister;
  1673. size : longint;
  1674. begin
  1675. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1676. dataregs:=[];
  1677. addrregs:=[];
  1678. if not(pi_has_saved_regs in current_procinfo.flags) then
  1679. exit;
  1680. { Copy registers from temp }
  1681. size:=0;
  1682. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1683. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1684. begin
  1685. inc(size,sizeof(aint));
  1686. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1687. { Allocate register so the optimizer does not remove the load }
  1688. a_reg_alloc(list,hreg);
  1689. dataregs:=dataregs + [saved_standard_registers[r]];
  1690. end;
  1691. if uses_registers(R_ADDRESSREGISTER) then
  1692. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1693. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1694. begin
  1695. inc(size,sizeof(aint));
  1696. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1697. { Allocate register so the optimizer does not remove the load }
  1698. a_reg_alloc(list,hreg);
  1699. addrregs:=addrregs + [saved_address_registers[r]];
  1700. end;
  1701. { 68k has no MM registers }
  1702. if uses_registers(R_MMREGISTER) then
  1703. internalerror(2014030202);
  1704. { Restore registers from temp }
  1705. href:=current_procinfo.save_regs_ref;
  1706. if size = sizeof(aint) then
  1707. a_load_ref_reg(list, OS_32, OS_32, href, hreg)
  1708. else
  1709. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs));
  1710. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1711. end;
  1712. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1713. begin
  1714. case _newsize of
  1715. OS_S16, OS_16:
  1716. case _oldsize of
  1717. OS_S8:
  1718. begin { 8 -> 16 bit sign extend }
  1719. if (isaddressregister(reg)) then
  1720. internalerror(2014031201);
  1721. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1722. end;
  1723. OS_8: { 8 -> 16 bit zero extend }
  1724. begin
  1725. if (current_settings.cputype in cpu_coldfire) then
  1726. { ColdFire has no ANDI.W }
  1727. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1728. else
  1729. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1730. end;
  1731. end;
  1732. OS_S32, OS_32:
  1733. case _oldsize of
  1734. OS_S8:
  1735. begin { 8 -> 32 bit sign extend }
  1736. if (isaddressregister(reg)) then
  1737. internalerror(2014031202);
  1738. if (current_settings.cputype = cpu_MC68000) then
  1739. begin
  1740. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1741. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1742. end
  1743. else
  1744. begin
  1745. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1746. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1747. end;
  1748. end;
  1749. OS_8: { 8 -> 32 bit zero extend }
  1750. begin
  1751. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1752. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1753. end;
  1754. OS_S16: { 16 -> 32 bit sign extend }
  1755. begin
  1756. if (isaddressregister(reg)) then
  1757. internalerror(2014031203);
  1758. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1759. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1760. end;
  1761. OS_16:
  1762. begin
  1763. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1764. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1765. end;
  1766. end;
  1767. end; { otherwise the size is already correct }
  1768. end;
  1769. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1770. begin
  1771. sign_extend(list, _oldsize, OS_INT, reg);
  1772. end;
  1773. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1774. var
  1775. ai : taicpu;
  1776. begin
  1777. if cond=OC_None then
  1778. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1779. else
  1780. begin
  1781. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1782. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1783. end;
  1784. ai.is_jmp:=true;
  1785. list.concat(ai);
  1786. end;
  1787. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1788. operations on an address register. if the register is a dataregister anyway, it
  1789. just returns it untouched.}
  1790. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1791. var
  1792. scratch_reg: TRegister;
  1793. instr: Taicpu;
  1794. begin
  1795. if isaddressregister(reg) then
  1796. begin
  1797. scratch_reg:=getintregister(list,OS_INT);
  1798. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1799. add_move_instruction(instr);
  1800. list.concat(instr);
  1801. result:=scratch_reg;
  1802. end
  1803. else
  1804. result:=reg;
  1805. end;
  1806. { moves source register to destination register, if the two are not the same. can be used in pair
  1807. with force_to_dataregister() }
  1808. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1809. var
  1810. instr: Taicpu;
  1811. begin
  1812. if (src <> dest) then
  1813. begin
  1814. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1815. add_move_instruction(instr);
  1816. list.concat(instr);
  1817. end;
  1818. end;
  1819. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1820. var
  1821. hsym : tsym;
  1822. href : treference;
  1823. paraloc : Pcgparalocation;
  1824. begin
  1825. { calculate the parameter info for the procdef }
  1826. procdef.init_paraloc_info(callerside);
  1827. hsym:=tsym(procdef.parast.Find('self'));
  1828. if not(assigned(hsym) and
  1829. (hsym.typ=paravarsym)) then
  1830. internalerror(2013100702);
  1831. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1832. while paraloc<>nil do
  1833. with paraloc^ do
  1834. begin
  1835. case loc of
  1836. LOC_REGISTER:
  1837. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1838. LOC_REFERENCE:
  1839. begin
  1840. { offset in the wrapper needs to be adjusted for the stored
  1841. return address }
  1842. reference_reset_base(href,reference.index,reference.offset-sizeof(pint),sizeof(pint));
  1843. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,NR_D0));
  1844. list.concat(taicpu.op_const_reg(A_SUB,S_L,ioffset,NR_D0));
  1845. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,NR_D0,href));
  1846. end
  1847. else
  1848. internalerror(2013100703);
  1849. end;
  1850. paraloc:=next;
  1851. end;
  1852. end;
  1853. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1854. procedure getselftoa0(offs:longint);
  1855. var
  1856. href : treference;
  1857. selfoffsetfromsp : longint;
  1858. begin
  1859. { move.l offset(%sp),%a0 }
  1860. { framepointer is pushed for nested procs }
  1861. if procdef.parast.symtablelevel>normal_function_level then
  1862. selfoffsetfromsp:=sizeof(aint)
  1863. else
  1864. selfoffsetfromsp:=0;
  1865. reference_reset_base(href,NR_SP,selfoffsetfromsp+offs,4);
  1866. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1867. end;
  1868. procedure loadvmttoa0;
  1869. var
  1870. href : treference;
  1871. begin
  1872. { move.l (%a0),%a0 ; load vmt}
  1873. reference_reset_base(href,NR_A0,0,4);
  1874. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1875. end;
  1876. procedure op_ona0methodaddr;
  1877. var
  1878. href : treference;
  1879. offs : longint;
  1880. begin
  1881. if (procdef.extnumber=$ffff) then
  1882. Internalerror(2013100701);
  1883. reference_reset_base(href,NR_A0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  1884. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,NR_A0));
  1885. reference_reset_base(href,NR_A0,0,4);
  1886. list.concat(taicpu.op_ref(A_JMP,S_L,href));
  1887. end;
  1888. var
  1889. make_global : boolean;
  1890. begin
  1891. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1892. Internalerror(200006137);
  1893. if not assigned(procdef.struct) or
  1894. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1895. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1896. Internalerror(200006138);
  1897. if procdef.owner.symtabletype<>ObjectSymtable then
  1898. Internalerror(200109191);
  1899. make_global:=false;
  1900. if (not current_module.is_unit) or
  1901. create_smartlink or
  1902. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1903. make_global:=true;
  1904. if make_global then
  1905. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1906. else
  1907. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1908. { set param1 interface to self }
  1909. g_adjust_self_value(list,procdef,ioffset);
  1910. { case 4 }
  1911. if (po_virtualmethod in procdef.procoptions) and
  1912. not is_objectpascal_helper(procdef.struct) then
  1913. begin
  1914. getselftoa0(4);
  1915. loadvmttoa0;
  1916. op_ona0methodaddr;
  1917. end
  1918. { case 0 }
  1919. else
  1920. list.concat(taicpu.op_sym(A_JMP,S_L,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1921. List.concat(Tai_symbol_end.Createname(labelname));
  1922. end;
  1923. {****************************************************************************}
  1924. { TCG64F68K }
  1925. {****************************************************************************}
  1926. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1927. var
  1928. hreg1, hreg2 : tregister;
  1929. opcode : tasmop;
  1930. xopcode : tasmop;
  1931. instr : taicpu;
  1932. begin
  1933. opcode := topcg2tasmop[op];
  1934. xopcode := topcg2tasmopx[op];
  1935. case op of
  1936. OP_ADD,OP_SUB:
  1937. begin
  1938. { if one of these three registers is an address
  1939. register, we'll really get into problems! }
  1940. if isaddressregister(regdst.reglo) or
  1941. isaddressregister(regdst.reghi) or
  1942. isaddressregister(regsrc.reghi) then
  1943. internalerror(2014030101);
  1944. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  1945. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  1946. end;
  1947. OP_AND,OP_OR:
  1948. begin
  1949. { at least one of the registers must be a data register }
  1950. if (isaddressregister(regdst.reglo) and
  1951. isaddressregister(regsrc.reglo)) or
  1952. (isaddressregister(regsrc.reghi) and
  1953. isaddressregister(regdst.reghi)) then
  1954. internalerror(2014030102);
  1955. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1956. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1957. end;
  1958. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1959. OP_IDIV,OP_DIV,
  1960. OP_IMUL,OP_MUL:
  1961. internalerror(2002081701);
  1962. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1963. OP_SAR,OP_SHL,OP_SHR:
  1964. internalerror(2002081702);
  1965. OP_XOR:
  1966. begin
  1967. if isaddressregister(regdst.reglo) or
  1968. isaddressregister(regsrc.reglo) or
  1969. isaddressregister(regsrc.reghi) or
  1970. isaddressregister(regdst.reghi) then
  1971. internalerror(2014030103);
  1972. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1973. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1974. end;
  1975. OP_NEG,OP_NOT:
  1976. begin
  1977. if isaddressregister(regdst.reglo) or
  1978. isaddressregister(regdst.reghi) then
  1979. internalerror(2014030104);
  1980. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1981. cg.add_move_instruction(instr);
  1982. list.concat(instr);
  1983. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1984. cg.add_move_instruction(instr);
  1985. list.concat(instr);
  1986. if (op = OP_NOT) then
  1987. xopcode:=opcode;
  1988. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  1989. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  1990. end;
  1991. end; { end case }
  1992. end;
  1993. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  1994. var
  1995. lowvalue : cardinal;
  1996. highvalue : cardinal;
  1997. opcode : tasmop;
  1998. xopcode : tasmop;
  1999. hreg : tregister;
  2000. begin
  2001. { is it optimized out ? }
  2002. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2003. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2004. exit; }
  2005. lowvalue := cardinal(value);
  2006. highvalue := value shr 32;
  2007. opcode := topcg2tasmop[op];
  2008. xopcode := topcg2tasmopx[op];
  2009. { the destination registers must be data registers }
  2010. if isaddressregister(regdst.reglo) or
  2011. isaddressregister(regdst.reghi) then
  2012. internalerror(2014030105);
  2013. case op of
  2014. OP_ADD,OP_SUB:
  2015. begin
  2016. hreg:=cg.getintregister(list,OS_INT);
  2017. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2018. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2019. { don't use cg.a_op_const_reg() here, because a possible optimized
  2020. ADDQ/SUBQ wouldn't set the eXtend bit }
  2021. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2022. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2023. end;
  2024. OP_AND,OP_OR,OP_XOR:
  2025. begin
  2026. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2027. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2028. end;
  2029. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2030. OP_IDIV,OP_DIV,
  2031. OP_IMUL,OP_MUL:
  2032. internalerror(2002081701);
  2033. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2034. OP_SAR,OP_SHL,OP_SHR:
  2035. internalerror(2002081702);
  2036. { these should have been handled already by earlier passes }
  2037. OP_NOT,OP_NEG:
  2038. internalerror(2012110403);
  2039. end; { end case }
  2040. end;
  2041. procedure create_codegen;
  2042. begin
  2043. cg := tcg68k.create;
  2044. cg64 :=tcg64f68k.create;
  2045. end;
  2046. end.