cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. {
  2. Copyright (c) 2006 by Florian Klaempfl
  3. Contains the base types for the AVR
  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. {# Base unit for processor information. This unit contains
  18. enumerations of registers, opcodes, sizes, and other
  19. such things which are processor specific.
  20. }
  21. unit cpubase;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cutils,cclasses,
  26. globtype,globals,
  27. cpuinfo,
  28. aasmbase,
  29. cgbase
  30. ;
  31. {*****************************************************************************
  32. Assembler Opcodes
  33. *****************************************************************************}
  34. type
  35. TAsmOp=(A_None,
  36. A_ADD,A_ADC,A_AND,A_BIT,A_CALL,A_CCF,A_CP,A_CPD,A_CPDR,
  37. A_CPI,A_CPIR,A_CPL,A_DAA,A_DEC,A_DI,A_DJNZ,A_EI,A_EX,
  38. A_EXX,A_HALT,A_IM,A_IN,A_INC,A_IND,A_INDR,A_INI,A_INIR,
  39. A_JP,A_JR,A_LD,A_LDD,A_LDDR,A_LDI,A_LDIR,A_NEG,A_NOP,A_OR,
  40. A_OTDR,A_OTIR,A_OUT,A_OUTD,A_OUTI,A_POP,A_PUSH,A_RES,A_RET,A_RETI,
  41. A_RETN,A_RL,A_RLA,A_RLC,A_RLCA,A_RLD,A_RR,A_RRA,A_RRC,
  42. A_RRCA,A_RRD,A_RST,A_SBC,A_SCF,A_SET,A_SLA,A_SRA,A_SRL,A_SUB,A_XOR);
  43. { This should define the array of instructions as string }
  44. op2strtable=array[tasmop] of string[11];
  45. const
  46. { First value of opcode enumeration }
  47. firstop = low(tasmop);
  48. { Last value of opcode enumeration }
  49. lastop = high(tasmop);
  50. { call/reg instructions are not considered as jmp instructions for the usage cases of
  51. this set }
  52. jmp_instructions = [A_JP,A_JR];
  53. call_jmp_instructions = [A_CALL]+jmp_instructions;
  54. {*****************************************************************************
  55. Registers
  56. *****************************************************************************}
  57. type
  58. { Number of registers used for indexing in tables }
  59. tregisterindex=0..{$i rz80nor.inc}-1;
  60. const
  61. { Available Superregisters }
  62. {$i rz80sup.inc}
  63. { No Subregisters }
  64. R_SUBWHOLE = R_SUBNONE;
  65. { Available Registers }
  66. {$i rz80con.inc}
  67. { Integer Super registers first and last }
  68. first_int_supreg = RS_A;
  69. first_int_imreg = $20;
  70. { Float Super register first and last }
  71. first_fpu_supreg = RS_INVALID;
  72. first_fpu_imreg = 0;
  73. { MM Super register first and last }
  74. first_mm_supreg = RS_INVALID;
  75. first_mm_imreg = 0;
  76. regnumber_count_bsstart = 32;
  77. regnumber_table : array[tregisterindex] of tregister = (
  78. {$i rz80num.inc}
  79. );
  80. regstabs_table : array[tregisterindex] of shortint = (
  81. {$i rz80sta.inc}
  82. );
  83. regdwarf_table : array[tregisterindex] of shortint = (
  84. {$i rz80dwa.inc}
  85. );
  86. { registers which may be destroyed by calls }
  87. VOLATILE_INTREGISTERS = [RS_A,RS_BC,RS_DE,RS_HL];
  88. VOLATILE_FPUREGISTERS = [];
  89. type
  90. totherregisterset = set of tregisterindex;
  91. {*****************************************************************************
  92. Conditions
  93. *****************************************************************************}
  94. type
  95. TAsmCond=(C_None,
  96. C_CC,C_CS,C_EQ,C_GE,C_HC,C_HS,C_ID,C_IE,C_LO,C_LT,
  97. C_MI,C_NE,C_PL,C_SH,C_TC,C_TS,C_VC,C_VS
  98. );
  99. const
  100. cond2str : array[TAsmCond] of string[2]=('',
  101. 'cc','cs','eq','ge','hc','hs','id','ie','lo','lt',
  102. 'mi','ne','pl','sh','tc','ts','vc','vs'
  103. );
  104. uppercond2str : array[TAsmCond] of string[2]=('',
  105. 'CC','CS','EQ','GE','HC','HS','ID','IE','LO','LT',
  106. 'MI','NE','PL','SH','TC','TS','VC','VS'
  107. );
  108. {*****************************************************************************
  109. Flags
  110. *****************************************************************************}
  111. type
  112. TResFlags = (F_NotPossible,F_CC,F_CS,F_EQ,F_GE,F_LO,F_LT,
  113. F_NE,F_SH,F_VC,F_VS);
  114. {*****************************************************************************
  115. Operands
  116. *****************************************************************************}
  117. taddressmode = (AM_UNCHANGED,AM_POSTINCREMENT,AM_PREDRECEMENT);
  118. {*****************************************************************************
  119. Constants
  120. *****************************************************************************}
  121. const
  122. max_operands = 4;
  123. maxintregs = 15;
  124. maxfpuregs = 0;
  125. maxaddrregs = 0;
  126. {*****************************************************************************
  127. Operand Sizes
  128. *****************************************************************************}
  129. type
  130. topsize = (S_NO,
  131. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  132. S_IS,S_IL,S_IQ,
  133. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  134. );
  135. {*****************************************************************************
  136. Constants
  137. *****************************************************************************}
  138. const
  139. firstsaveintreg = RS_INVALID;
  140. lastsaveintreg = RS_INVALID;
  141. firstsavefpureg = RS_INVALID;
  142. lastsavefpureg = RS_INVALID;
  143. firstsavemmreg = RS_INVALID;
  144. lastsavemmreg = RS_INVALID;
  145. {*****************************************************************************
  146. Default generic sizes
  147. *****************************************************************************}
  148. { Defines the default address size for a processor, }
  149. OS_ADDR = OS_16;
  150. { the natural int size for a processor,
  151. has to match osuinttype/ossinttype as initialized in psystem,
  152. initially, this was OS_16/OS_S16 on avr, but experience has
  153. proven that it is better to make it 8 Bit thus having the same
  154. size as a register.
  155. }
  156. OS_INT = OS_8;
  157. OS_SINT = OS_S8;
  158. { the maximum float size for a processor, }
  159. OS_FLOAT = OS_F64;
  160. { the size of a vector register for a processor }
  161. OS_VECTOR = OS_M32;
  162. {*****************************************************************************
  163. Generic Register names
  164. *****************************************************************************}
  165. { Stack pointer register }
  166. NR_STACK_POINTER_REG = NR_SP;
  167. RS_STACK_POINTER_REG = RS_SP;
  168. { Frame pointer register }
  169. RS_FRAME_POINTER_REG = RS_IX;
  170. NR_FRAME_POINTER_REG = NR_IX;
  171. { Register for addressing absolute data in a position independant way,
  172. such as in PIC code. The exact meaning is ABI specific. For
  173. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  174. }
  175. NR_PIC_OFFSET_REG = NR_INVALID;
  176. { Results are returned in this register (32-bit values) }
  177. NR_FUNCTION_RETURN_REG = NR_HL;
  178. RS_FUNCTION_RETURN_REG = RS_HL;
  179. { Low part of 64bit return value }
  180. NR_FUNCTION_RETURN64_LOW_REG = NR_HL;
  181. RS_FUNCTION_RETURN64_LOW_REG = RS_HL;
  182. { High part of 64bit return value }
  183. NR_FUNCTION_RETURN64_HIGH_REG = NR_BC;
  184. RS_FUNCTION_RETURN64_HIGH_REG = RS_BC;
  185. { The value returned from a function is available in this register }
  186. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  187. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  188. { The lowh part of 64bit value returned from a function }
  189. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  190. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  191. { The high part of 64bit value returned from a function }
  192. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  193. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  194. NR_FPU_RESULT_REG = NR_NO;
  195. NR_MM_RESULT_REG = NR_NO;
  196. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  197. { Offset where the parent framepointer is pushed }
  198. PARENT_FRAMEPOINTER_OFFSET = 0;
  199. NR_DEFAULTFLAGS = NR_F;
  200. RS_DEFAULTFLAGS = RS_F;
  201. {*****************************************************************************
  202. GCC /ABI linking information
  203. *****************************************************************************}
  204. const
  205. { Registers which must be saved when calling a routine declared as
  206. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  207. saved should be the ones as defined in the target ABI and / or GCC.
  208. This value can be deduced from the CALLED_USED_REGISTERS array in the
  209. GCC source.
  210. }
  211. { on avr, gen_entry/gen_exit code saves/restores registers, so
  212. we don't need this array }
  213. saved_standard_registers : array[0..0] of tsuperregister =
  214. (RS_INVALID);
  215. { Required parameter alignment when calling a routine declared as
  216. stdcall and cdecl. The alignment value should be the one defined
  217. by GCC or the target ABI.
  218. The value of this constant is equal to the constant
  219. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  220. }
  221. std_param_align = 4;
  222. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  223. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  224. {*****************************************************************************
  225. Helpers
  226. *****************************************************************************}
  227. { Returns the tcgsize corresponding with the size of reg.}
  228. function reg_cgsize(const reg: tregister) : tcgsize;
  229. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  230. procedure inverse_flags(var f: TResFlags);
  231. function flags_to_cond(const f: TResFlags) : TAsmCond;
  232. function findreg_by_number(r:Tregister):tregisterindex;
  233. function std_regnum_search(const s:string):Tregister;
  234. function std_regname(r:Tregister):string;
  235. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  236. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  237. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  238. function condition_in(const Subset, c: TAsmCond): Boolean;
  239. function dwarf_reg(r:tregister):byte;
  240. function dwarf_reg_no_error(r:tregister):shortint;
  241. function eh_return_data_regno(nr: longint): longint;
  242. function GetHigh(const r : TRegister) : TRegister;
  243. { returns the next virtual register }
  244. function GetNextReg(const r : TRegister) : TRegister;
  245. { returns the last virtual register }
  246. function GetLastReg(const r : TRegister) : TRegister;
  247. { returns the register with the offset of ofs of a continuous set of register starting with r }
  248. function GetOffsetReg(const r : TRegister;ofs : shortint) : TRegister;
  249. { returns the register with the offset of ofs of a continuous set of register starting with r and being continued with rhi }
  250. function GetOffsetReg64(const r,rhi: TRegister;ofs : shortint): TRegister;
  251. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  252. implementation
  253. uses
  254. rgBase,verbose;
  255. const
  256. std_regname_table : TRegNameTable = (
  257. {$i rz80std.inc}
  258. );
  259. regnumber_index : array[tregisterindex] of tregisterindex = (
  260. {$i rz80rni.inc}
  261. );
  262. std_regname_index : array[tregisterindex] of tregisterindex = (
  263. {$i rz80sri.inc}
  264. );
  265. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  266. begin
  267. cgsize2subreg:=R_SUBWHOLE;
  268. end;
  269. function reg_cgsize(const reg: tregister): tcgsize;
  270. begin
  271. case getregtype(reg) of
  272. R_INTREGISTER :
  273. reg_cgsize:=OS_8;
  274. R_ADDRESSREGISTER :
  275. reg_cgsize:=OS_16;
  276. else
  277. internalerror(2011021905);
  278. end;
  279. end;
  280. procedure inverse_flags(var f: TResFlags);
  281. const
  282. inv_flags: array[TResFlags] of TResFlags =
  283. (F_NotPossible,F_CS,F_CC,F_NE,F_LT,F_SH,F_GE,
  284. F_NE,F_LO,F_VS,F_VC);
  285. begin
  286. f:=inv_flags[f];
  287. end;
  288. function flags_to_cond(const f: TResFlags) : TAsmCond;
  289. const
  290. flag_2_cond: array[F_CC..F_VS] of TAsmCond =
  291. (C_CC,C_CS,C_EQ,C_GE,C_LO,C_LT,
  292. C_NE,C_SH,C_VC,C_VS);
  293. begin
  294. if f=F_NotPossible then
  295. internalerror(2011022101);
  296. if f>high(flag_2_cond) then
  297. internalerror(200112301);
  298. result:=flag_2_cond[f];
  299. end;
  300. function findreg_by_number(r:Tregister):tregisterindex;
  301. begin
  302. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  303. end;
  304. function std_regnum_search(const s:string):Tregister;
  305. begin
  306. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  307. end;
  308. function std_regname(r:Tregister):string;
  309. var
  310. p : tregisterindex;
  311. begin
  312. p:=findreg_by_number_table(r,regnumber_index);
  313. if p<>0 then
  314. result:=std_regname_table[p]
  315. else
  316. result:=generic_regname(r);
  317. end;
  318. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  319. const
  320. inverse: array[TAsmCond] of TAsmCond=(C_None,
  321. C_CS,C_CC,C_NE,C_LT,C_HS,C_HC,C_IE,C_ID,C_SH,C_GE,
  322. C_PL,C_EQ,C_MI,C_LO,C_TS,C_TC,C_VS,C_VC);
  323. begin
  324. result := inverse[c];
  325. end;
  326. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  327. begin
  328. result := c1 = c2;
  329. end;
  330. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  331. function condition_in(const Subset, c: TAsmCond): Boolean;
  332. begin
  333. Result := {(c.cond = C_None) or} conditions_equal(Subset, c);
  334. { TODO: Can a PowerPC programmer please update this procedure to
  335. actually detect subsets? Thanks. [Kit] }
  336. end;
  337. function rotl(d : dword;b : byte) : dword;
  338. begin
  339. result:=(d shr (32-b)) or (d shl b);
  340. end;
  341. function dwarf_reg(r:tregister):byte;
  342. var
  343. reg : shortint;
  344. begin
  345. reg:=regdwarf_table[findreg_by_number(r)];
  346. if reg=-1 then
  347. internalerror(200603251);
  348. result:=reg;
  349. end;
  350. function dwarf_reg_no_error(r:tregister):shortint;
  351. begin
  352. result:=regdwarf_table[findreg_by_number(r)];
  353. end;
  354. function eh_return_data_regno(nr: longint): longint;
  355. begin
  356. result:=-1;
  357. end;
  358. function GetHigh(const r : TRegister) : TRegister;
  359. begin
  360. result:=TRegister(longint(r)+1)
  361. end;
  362. function GetNextReg(const r: TRegister): TRegister;
  363. begin
  364. result:=TRegister(longint(r)+1);
  365. end;
  366. function GetLastReg(const r: TRegister): TRegister;
  367. begin
  368. result:=TRegister(longint(r)-1);
  369. end;
  370. function GetOffsetReg(const r: TRegister;ofs : shortint): TRegister;
  371. begin
  372. result:=TRegister(longint(r)+ofs);
  373. end;
  374. function GetOffsetReg64(const r,rhi: TRegister;ofs : shortint): TRegister;
  375. begin
  376. if ofs>3 then
  377. result:=TRegister(longint(rhi)+ofs-4)
  378. else
  379. result:=TRegister(longint(r)+ofs);
  380. end;
  381. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  382. begin
  383. is_calljmp:= o in call_jmp_instructions;
  384. end;
  385. end.