types.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. ***************************************************************************
  3. * dcc project general header
  4. * (C) Cristina Cifuentes, Mike van Emmerik
  5. ***************************************************************************
  6. */
  7. #pragma once
  8. #include <cassert>
  9. #include <stdint.h>
  10. #include "Enums.h"
  11. /**** Common definitions and macros ****/
  12. #define MAX 0x7FFFFFFF
  13. /* Type definitions used in the program */
  14. typedef unsigned char byte; /* 8 bits */
  15. typedef unsigned short word;/* 16 bits */
  16. typedef short int16; /* 16 bits */
  17. typedef unsigned char boolT; /* 8 bits */
  18. #define SYNTHESIZED_MIN 0x100000 /* Synthesized labs use bits 21..32 */
  19. /* These are for C library signature detection */
  20. #define SYMLEN 16 /* Length of proc symbols, incl null */
  21. #define PATLEN 23 /* Length of proc patterns */
  22. #define WILD 0xF4 /* The wild byte */
  23. /* MACROS */
  24. // Macro reads a LH word from the image regardless of host convention
  25. // Returns a 16 bit quantity, e.g. C000 is read into an Int as C000
  26. //#define LH(p) ((int16)((byte *)(p))[0] + ((int16)((byte *)(p))[1] << 8))
  27. #define LH(p) ((word)((byte *)(p))[0] + ((word)((byte *)(p))[1] << 8))
  28. /* Macro reads a LH word from the image regardless of host convention */
  29. /* Returns a signed quantity, e.g. C000 is read into an Int as FFFFC000 */
  30. #define LH_SIGNED(p) (((byte *)(p))[0] + (((char *)(p))[1] << 8))
  31. /* Macro tests bit b for type t in prog.map */
  32. #define BITMAP(b, t) (prog.map[(b) >> 2] & ((t) << (((b) & 3) << 1)))
  33. /* Macro to convert a segment, offset definition into a 20 bit address */
  34. #define opAdr(seg,off) ((seg << 4) + off)
  35. /* duVal FLAGS */
  36. struct eDuVal
  37. {
  38. eDuVal()
  39. {
  40. def=use=val=0;
  41. }
  42. enum flgs
  43. {
  44. DEF=1,
  45. USE=2,
  46. VAL=4
  47. };
  48. uint8_t def :1; //!< Variable was first defined than used
  49. uint8_t use :1; //!< Variable was first used than defined
  50. uint8_t val :1; /* Variable has an initial value. 2 cases:
  51. 1. When variable is used first (ie. global)
  52. 2. When a value is moved into the variable
  53. for the first time.
  54. */
  55. void setFlags(uint16_t x)
  56. {
  57. def = x&DEF;
  58. use = x&USE;
  59. val = x&VAL;
  60. }
  61. bool isUSE_VAL() {return use&&val;} //Use and Val
  62. };
  63. static constexpr const char * hlTypes[13] = {
  64. "", "char", "unsigned char", "int", "unsigned int",
  65. "long", "unsigned long", "record", "int *", "char *",
  66. "", "float", "double"
  67. };
  68. struct TypeContainer
  69. {
  70. hlType m_type;
  71. size_t m_size;
  72. TypeContainer(hlType t,size_t sz) : m_type(t),m_size(sz)
  73. {
  74. }
  75. static size_t typeSize(hlType t)
  76. {
  77. switch(t)
  78. {
  79. case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
  80. return 2;
  81. case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
  82. return 1;
  83. case TYPE_LONG_SIGN: case TYPE_LONG_UNSIGN:
  84. return 4;
  85. case TYPE_FLOAT:
  86. return 4;
  87. default:
  88. return ~0;
  89. }
  90. return 0;
  91. }
  92. static hlType defaultTypeForSize(size_t x)
  93. {
  94. /* Type of the symbol according to the number of bytes it uses */
  95. static hlType cbType[] = {TYPE_UNKNOWN, TYPE_BYTE_UNSIGN, TYPE_WORD_SIGN,
  96. TYPE_UNKNOWN, TYPE_LONG_SIGN};
  97. assert(x < sizeof(cbType)/sizeof(hlType));
  98. return cbType[x];
  99. }
  100. static constexpr const char *typeName(hlType t)
  101. {
  102. return hlTypes[t];
  103. }
  104. };