cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. {
  2. Copyright (C) 1998-2000 by Florian Klaempfl
  3. This unit implements an asmlistitem class for the Alpha architecture.
  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. {
  18. This unit implements an asmlistitem class for the Alpha architecture.
  19. }
  20. unit cpubase;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cutils,cclasses,globals,aasmbase,cpuinfo,cginfo;
  25. type
  26. { all registers }
  27. TRegister = (R_NO, { R_NO is Mandatory, signifies no register }
  28. R_0,R_1,R_2,R_3,R_4,R_5,R_6,R_7,R_8,R_9,
  29. R_10,R_11,R_12,R_13,R_14,R_15,R_16,R_17,R_18,R_19,
  30.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LVER,A_INSBL,A_INSLH,
  52. A_INSLL,A_INSQH,A_INSQL,A_INSWH,A_INSWL,A_ITOFF,
  53. A_ITOFS,A_ITOFT,A_JMP,A_JSR,A_JSR_COROUTINE,A_LDA,
  54. A_LDAH,A_LDBU,A_LDWU,A_LDF,A_LDG,A_LDL,
  55. A_LDL_L,A_LDQ,A_LDQ_L,A_LDQ_U,A_LDS,A_LDT,
  56. A_MAXSB8,A_MAXSW4,A_MAXUB8,A_MAXUW4,A_MB,A_MF_FPCR,
  57. A_MINSB8,A_MINSW4,A_MINUB8,A_MINUW4,A_MSKBL,A_MSKLH,
  58. A_MSKLL,A_MSKQH,A_MSKQL,A_MSKWH,A_MSKWL,A_MT_FPCR,
  59. A_MULF,A_MULG,A_MULL,A_MULQ,
  60. A_MULS,A_MULT,A_ORNOT,A_PERR,A_PKLB,A_PKWB,
  61. A_RC,A_RET,A_RPCC,A_RS,A_S4ADDL,A_S4ADDQ,
  62. A_S4SUBL,A_S4SUBQ,A_S8ADDL,A_S8ADDQ,A_S8SUBL,A_S8SUBQ,
  63. A_SEXTB,A_SEXTW,A_SLL,A_SQRTF,A_SQRTG,A_SQRTS,
  64. A_SQRTT,A_SRA,A_SRL,A_STB,A_STF,A_STG,
  65. A_STS,A_STL,A_STL_C,A_STQ,A_STQ_C,A_STQ_U,
  66. A_STT,A_STW,A_SUBF,A_SUBG,A_SUBL,
  67. A_SUBQ,A_SUBS,A_SUBT,A_TRAPB,A_UMULH,
  68. A_UNPKBL,A_UNPKBW,A_WH64,A_WMB,A_XOR,A_ZAP,
  69. A_ZAPNOT
  70. { Psuedo code understood by the gnu assembler }
  71. ,A_LDGP);
  72. const
  73. firstop = low(tasmop);
  74. lastop = high(tasmop);
  75. std_reg2str : array[tregister] of string[4] = (
  76. '',
  77. '','','','','','','','','','',
  78. '','','','','','','','','','',
  79. '','','','','','','','','','',
  80. '','',
  81. '','','','','','','','','','',
  82. '','','','','','','','','','',
  83. '','','','','','','','','','',
  84. '',''
  85. );
  86. type
  87. TAsmCond =
  88. (
  89. C_None,C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  90. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,C_NS,C_NZ,C_O,C_P,
  91. C_PE,C_PO,C_S,C_Z
  92. );
  93. TRegisterset = Set of TRegister;
  94. tregister64 = tregister;
  95. Const
  96. Firstreg = R_0;
  97. LastReg = R_F31;
  98. {*****************************************************************************
  99. Default generic sizes
  100. *****************************************************************************}
  101. { Defines the default address size for a processor, }
  102. OS_ADDR = OS_64;
  103. { the natural int size for a processor, }
  104. OS_INT = OS_64;
  105. { the maximum float size for a processor, }
  106. OS_FLOAT = OS_F80;
  107. { the size of a vector register for a processor }
  108. OS_VECTOR = OS_M64;
  109. stack_pointer_reg = R_30;
  110. frame_pointer_reg = R_15;
  111. self_pointer_reg = R_16;
  112. accumulator = R_0;
  113. {the return_result_reg, is used inside the called function to store its return
  114. value when that is a scalar value otherwise a pointer to the address of the
  115. result is placed inside it}
  116. return_result_reg = accumulator;
  117. {the function_result_reg contains the function result after a call to a scalar
  118. function othewise it contains a pointer to the returned result}
  119. function_result_reg = accumulator;
  120. fpu_result_reg = R_F0;
  121. global_pointer = R_29;
  122. return_pointer = R_26;
  123. { it is used to pass the offset to the destructor helper routine }
  124. vmt_offset_reg = R_1;
  125. { low and high of the available maximum width integer general purpose }
  126. { registers }
  127. LoGPReg = R_0;
  128. HiGPReg = R_31;
  129. { low and high of every possible width general purpose register (same as
  130. above on most architctures apart from the 80x86) }
  131. LoReg = R_0;
  132. HiReg = R_31;
  133. { Constant defining possibly all registers which might require saving }
  134. ALL_REGISTERS = [firstreg..lastreg];
  135. general_registers = [R_0..R_31];
  136. availabletempregsint = [R_0..R_14,R_16..R_25,R_28];
  137. availabletempregsfpu = [R_F0..R_F30];
  138. availabletempregsmm = [];
  139. intregs = [R_0..R_31];
  140. usableregsint = [];
  141. c_countusableregsint = 26;
  142. maxfpuregs = 32;
  143. fpuregs = [R_F0..R_F31];
  144. usableregsfpu = [];
  145. c_countusableregsfpu = 31;
  146. mmregs = [];
  147. usableregsmm = [];
  148. c_countusableregsmm = 0;
  149. max_operands = 4;
  150. registers_saved_on_cdecl = [R_9..R_14,R_F2..R_F9];
  151. firstsaveintreg = R_NO;
  152. lastsaveintreg = R_NO;
  153. firstsavefpureg = R_NO;
  154. lastsavefpureg = R_NO;
  155. firstsavemmreg = R_NO;
  156. lastsavemmreg = R_NO;
  157. maxvarregs = 6;
  158. varregs : Array [1..maxvarregs] of Tregister =
  159. (R_9,R_10,R_11,R_12,R_13,R_14);
  160. maxfpuvarregs = 8;
  161. { Registers which are defined as scratch and no need to save across
  162. routine calls or in assembler blocks.
  163. }
  164. max_scratch_regs = 2;
  165. scratch_regs : array[1..max_scratch_regs] of tregister = (R_1,R_2);
  166. {*****************************************************************************
  167. GDB Information
  168. *****************************************************************************}
  169. { Register indexes for stabs information, when some
  170. parameters or variables are stored in registers.
  171. }
  172. stab_regindex : array[tregister] of shortint =
  173. (0,
  174. 0,0,0,0,0,0,0,0,0,0,
  175. 0,0,0,0,0,0,0,0,0,0,
  176. 0,0,0,0,0,0,0,0,0,0,
  177. 0,0,
  178. 0,0,0,0,0,0,0,0,0,0,
  179. 0,0,0,0,0,0,0,0,0,0,
  180. 0,0,0,0,0,0,0,0,0,0,
  181. 0,0
  182. );
  183. {*****************************************************************************
  184. Flags
  185. *****************************************************************************}
  186. type
  187. { The Alpha doesn't have flags but some generic code depends on this type. }
  188. TResFlags = (F_NO);
  189. { reference record }
  190. pparareference = ^tparareference;
  191. tparareference = packed record
  192. index : tregister;
  193. offset : longint;
  194. end;
  195. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  196. TReference = record
  197. offset : aword;
  198. symbol : tasmsymbol;
  199. base : tregister;
  200. { The index isn't used by the alpha port, but some generic code depends on it }
  201. index : tregister;
  202. is_immediate : boolean;
  203. offsetfixup : word; {needed for inline}
  204. options : trefoptions;
  205. { the boundary to which the reference is surely aligned }
  206. alignment : byte;
  207. end;
  208. PReference = ^TReference;
  209. TLoc=(
  210. LOC_INVALID, { added for tracking problems}
  211. LOC_CONSTANT, { constant value }
  212. LOC_JUMP, { boolean results only, jump to false or true label }
  213. LOC_FLAGS, { boolean results only, flags are set }
  214. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  215. LOC_REFERENCE, { in memory value }
  216. LOC_REGISTER, { in a processor register }
  217. LOC_CREGISTER, { Constant register which shouldn't be modified }
  218. LOC_FPUREGISTER, { FPU stack }
  219. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  220. LOC_SSEREGISTER,
  221. LOC_CSSEREGISTER,
  222. LOC_CMMREGISTER,
  223. LOC_MMREGISTER
  224. );
  225. { tparamlocation describes where a parameter for a procedure is stored.
  226. References are given from the caller's point of view. The usual
  227. TLocation isn't used, because contains a lot of unnessary fields.
  228. }
  229. tparalocation = packed record
  230. size : TCGSize;
  231. loc : TLoc;
  232. sp_fixup : longint;
  233. case TLoc of
  234. LOC_REFERENCE : (reference : tparareference);
  235. { segment in reference at the same place as in loc_register }
  236. LOC_REGISTER,LOC_CREGISTER : (
  237. case longint of
  238. 1 : (register,register64.reghi : tregister);
  239. { overlay a register64.reglo }
  240. 2 : (register64.reglo : tregister);
  241. { overlay a 64 Bit register type }
  242. 3 : (reg64 : tregister64);
  243. 4 : (register64 : tregister64);
  244. );
  245. end;
  246. tlocation = packed record
  247. loc : TLoc;
  248. size : TCGSize;
  249. case TLoc of
  250. LOC_CONSTANT : (
  251. case longint of
  252. 1 : (value : AWord);
  253. { can't do this, this layout depends on the host cpu. Use }
  254. { lo(valueqword)/hi(valueqword) instead (JM) }
  255. { 2 : (valuelow, valuehigh:AWord); }
  256. { overlay a complete 64 Bit value }
  257. 3 : (valueqword : qword);
  258. );
  259. LOC_CREFERENCE,
  260. LOC_REFERENCE : (reference : treference);
  261. { segment in reference at the same place as in loc_register }
  262. LOC_REGISTER,LOC_CREGISTER : (
  263. case longint of
  264. 1 : (register,register64.reghi,segment : tregister);
  265. { overlay a register64.reglo }
  266. 2 : (register64.reglo : tregister);
  267. { overlay a 64 Bit register type }
  268. 3 : (reg64 : tregister64);
  269. 4 : (register64 : tregister64);
  270. );
  271. end;
  272. {*****************************************************************************
  273. Operands
  274. *****************************************************************************}
  275. { Types of operand }
  276. toptype=(top_none,top_reg,top_ref,top_const,top_symbol);
  277. toper=record
  278. ot : longint;
  279. case typ : toptype of
  280. top_none : ();
  281. top_reg : (reg:tregister);
  282. top_ref : (ref:preference);
  283. top_const : (val:longint);
  284. top_symbol : (sym:tasmsymbol;symofs:longint);
  285. end;
  286. const
  287. { Registers which must be saved when calling a routine declared as
  288. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  289. saved should be the ones as defined in the target ABI and / or GCC.
  290. This value can be deduced from the CALLED_USED_REGISTERS array in the
  291. GCC source.
  292. }
  293. std_saved_registers = [];
  294. { Required parameter alignment when calling a routine declared as
  295. stdcall and cdecl. The alignment value should be the one defined
  296. by GCC or the target ABI.
  297. The value of this constant is equal to the constant
  298. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  299. }
  300. std_param_align = 8;
  301. { offsets for the integer and floating point registers }
  302. INT_REG = 0;
  303. FLOAT_REG = 32;
  304. { operator qualifiers }
  305. OQ_CHOPPED_ROUNDING = $01; { /C }
  306. OQ_ROUNDING_MODE_DYNAMIC = $02; { /D }
  307. OQ_ROUND_TOWARD_MINUS_INFINITY = $04; { /M }
  308. OQ_INEXACT_RESULT_ENABLE = $08; { /I }
  309. OQ_SOFTWARE_COMPLETION_ENABLE = $10; { /S }
  310. OQ_FLOATING_UNDERFLOW_ENABLE = $20; { /U }
  311. OQ_INTEGER_OVERFLOW_ENABLE = $40; { /V }
  312. {*****************************************************************************
  313. Opcode propeties (needed for optimizer)
  314. *****************************************************************************}
  315. {$ifndef NOOPT}
  316. Type
  317. {What an instruction can change}
  318. TInsChange = (Ch_None);
  319. {$endif}
  320. { resets all values of ref to defaults }
  321. procedure reset_reference(var ref : treference);
  322. { set mostly used values of a new reference }
  323. function new_reference(base : tregister;offset : longint) : preference;
  324. function newreference(const r : treference) : preference;
  325. procedure disposereference(var r : preference);
  326. function reg2str(r : tregister) : string;
  327. {*****************************************************************************
  328. Init/Done
  329. *****************************************************************************}
  330. procedure InitCpu;
  331. procedure DoneCpu;
  332. implementation
  333. uses
  334. verbose;
  335. function reg2str(r : tregister) : string;
  336. begin
  337. if r in [R_0..R_31] then
  338. reg2str:='R'+tostr(longint(r)-longint(R_0))
  339. else if r in [R_F0..R_F31] then
  340. reg2str:='F'+tostr(longint(r)-longint(R_F0))
  341. else internalerror(38991);
  342. end;
  343. procedure reset_reference(var ref : treference);
  344. begin
  345. FillChar(ref,sizeof(treference),0);
  346. end;
  347. function new_reference(base : tregister;offset : longint) : preference;
  348. var
  349. r : preference;
  350. begin
  351. new(r);
  352. FillChar(r^,sizeof(treference),0);
  353. r^.offset:=offset;
  354. r^.alignment:=8;
  355. new_reference:=r;
  356. end;
  357. function newreference(const r : treference) : preference;
  358. var
  359. p : preference;
  360. begin
  361. new(p);
  362. p^:=r;
  363. newreference:=p;
  364. end;
  365. procedure disposereference(var r : preference);
  366. begin
  367. dispose(r);
  368. r:=Nil;
  369. end;
  370. {*****************************************************************************
  371. Init/Done
  372. *****************************************************************************}
  373. procedure InitCpu;
  374. begin
  375. end;
  376. procedure DoneCpu;
  377. begin
  378. end;
  379. end.