cgcpu.pas 79 KB

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