cgcpu.pas 83 KB

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