cgcpu.pas 82 KB

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