Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

694 lines
18 KiB

  1. /*++
  2. Copyright (c) 1995-1999 Microsoft Corporation
  3. Module Name:
  4. gen.h
  5. Abstract:
  6. Types shared between the Wx86 tools
  7. Author:
  8. ??-???-?? Unknown
  9. Revision History:
  10. --*/
  11. // Increment this number whenever the format of winincs.ppm changes
  12. #define VM_TOOL_VERSION_BASE 0x80000006
  13. // Make the 64-bit PPM file format incompatible to prevent badness
  14. #if _WIN64
  15. #define VM_TOOL_VERSION (VM_TOOL_VERSION_BASE | 0x01000000)
  16. #else
  17. #define VM_TOOL_VERSION (VM_TOOL_VERSION_BASE)
  18. #endif
  19. // Make the compiler more struct.
  20. #pragma warning(3:4033) // function must return a value
  21. //#pragma warning(3:4701) // local may be used w/o init
  22. #pragma warning(3:4702) // Unreachable code
  23. #pragma warning(3:4705) // Statement has no effect
  24. extern const char *ErrMsgPrefix; // string to put in front of all error
  25. // messages so that BUILD can find them.
  26. // This is something like:
  27. // "NMAKE : U8600: 'GENTHNK' "
  28. struct _KnownTypesInfo;
  29. typedef enum _TokenType {
  30. TK_NONE, // 0
  31. TK_IDENTIFIER, // 1
  32. TK_NUMBER, // 2
  33. TK_PLUS, // 3
  34. TK_MINUS, // 4
  35. TK_STAR, // 5
  36. TK_DIVIDE, // 6
  37. TK_LSQUARE, // 7
  38. TK_RSQUARE, // 8
  39. TK_LBRACE, // 9
  40. TK_RBRACE, // a
  41. TK_LPAREN, // b
  42. TK_RPAREN, // c
  43. TK_VARGS, // d
  44. TK_CONST, // e
  45. TK_VOLATILE, // f
  46. TK_REGISTER, // 10
  47. TK_EXTERN, // 11
  48. TK_CDECL, // 12
  49. TK_STDCALL, // 13
  50. TK_TYPEDEF, // 14
  51. TK_STATIC, // 15
  52. TK_COMMA, // 16
  53. TK_SEMI, // 17
  54. TK_STRUCT, // 18
  55. TK_UNION, // 19
  56. TK_ENUM, // 1a
  57. TK_INLINE, // 1b
  58. TK_COLON, // 1c
  59. TK_ASSIGN, // 1d
  60. TK_DOT, // 1e
  61. TK_LSHIFT, // 1f
  62. TK_RSHIFT, // 20
  63. TK_LESS, // 21
  64. TK_GREATER, // 22
  65. TK_UNALIGNED, // 23
  66. TK_DECLSPEC, // 24
  67. TK_RESTRICT, // 25
  68. TK_FASTCALL, // 26
  69. TK_IN, // 27
  70. TK_OUT, // 28
  71. TK_INOUT, // 29
  72. TK_BITWISE_AND, // 30
  73. TK_BITWISE_OR, // 31
  74. TK_LOGICAL_AND, // 32
  75. TK_LOGICAL_OR, // 33
  76. TK_MOD, // 34
  77. TK_XOR, // 35
  78. TK_NOT, // 36
  79. TK_TILDE, // 37
  80. TK_STRING, // 38
  81. TK_SIZEOF, // 39
  82. TK_TEMPLATE, // 40
  83. TK___W64, // 41
  84. TK_EOS // end-of-statement
  85. } TOKENTYPE, *PTOKENTYPE;
  86. typedef struct _cvmheapinfo {
  87. ULONG_PTR uBaseAddress;
  88. ULONG_PTR uReserveSize;
  89. ULONG_PTR uRegionSize;
  90. ULONG_PTR uUncomitted;
  91. ULONG_PTR uUnReserved;
  92. ULONG_PTR uAvailable;
  93. } CVMHEAPINFO;
  94. typedef struct _memberinfo {
  95. struct _memberinfo *pmeminfoNext; // ptr to next member
  96. DWORD dwOffset; // offset in structure of member
  97. char *sName; // member name
  98. char *sType; // type name
  99. struct _KnownTypesInfo *pkt; // Info for this type
  100. int IndLevel; // levels of indirection
  101. struct _KnownTypesInfo *pktCache; // used by MemberTypes() in genthnk
  102. BOOL bIsBitfield; // Determines if this is a bitfield
  103. int BitsRequired; // Number of bits required for bitfield
  104. BOOL bIsPtr64; // Pointer is a 64 bit pointer
  105. BOOL bIsArray; // This member is an array
  106. int ArrayElements; // Number of elements in the array
  107. } MEMBERINFO, *PMEMBERINFO;
  108. typedef struct _funcinfo {
  109. struct _funcinfo *pfuncinfoNext;
  110. BOOL fIsPtr64; // TRUE if this is a __ptr64
  111. TOKENTYPE tkDirection; // TK_IN, TK_OUT, TK_INOUT or TK_NONE
  112. TOKENTYPE tkPreMod; // TK_CONST, TK_VOLATILE, or TK_NONE
  113. TOKENTYPE tkSUE; // TK_STRUCT/UNION/ENUM, or TK_NONE
  114. char *sType; // name of the type
  115. struct _KnownTypesInfo *pkt; // Info for this type
  116. TOKENTYPE tkPrePostMod; // TK_CONST, TK_VOLATILE, or TK_NONE
  117. int IndLevel; // indirection level
  118. TOKENTYPE tkPostMod; // TK_CONST, TK_VOLATILE, or TK_NONE
  119. char *sName; // name of the argment
  120. } FUNCINFO, *PFUNCINFO;
  121. #if _WIN64
  122. // The sizes must be bigger since the MEMBERINFO structs themselves are bigger
  123. #define FUNCMEMBERSIZE (40*1024) // storage for members or MEMINFO list
  124. #define MEMBERMETHODSSIZE 8192 // storage for names of methods
  125. #else
  126. #define FUNCMEMBERSIZE (20*1024) // storage for members or MEMINFO list
  127. #define MEMBERMETHODSSIZE 4096 // storage for names of methods
  128. #endif
  129. typedef enum _TypeKind {
  130. TypeKindEmpty = 0, // Members[] is unused
  131. TypeKindStruct, // TYPESINFO.Members is array of MEMBERINFO
  132. TypeKindFunc // TYPESINFO.Members is array of FUNCINFO
  133. } TYPEKIND;
  134. #define DIR_INOUT 0
  135. #define DIR_IN 1
  136. #define DIR_OUT 2
  137. #define SIZEOFPOINTER 4 // standard size for 32 bit pointer
  138. #define SIZEOFPOINTER64 8 // standard size for 64 bit pointer
  139. // The colors
  140. typedef enum {RED, BLACK} COL;
  141. typedef struct _KnownTypesInfo {
  142. // elements used by the Red-Black tree code, along with TypeName
  143. struct _KnownTypesInfo *RBParent;
  144. struct _KnownTypesInfo *RBLeft;
  145. struct _KnownTypesInfo *RBRight;
  146. COL RBColor;
  147. struct _KnownTypesInfo *Next;
  148. ULONG Flags;
  149. int IndLevel;
  150. int RetIndLevel;
  151. int Size;
  152. int iPackSize;
  153. char *BasicType;
  154. char *BaseName;
  155. char *FuncRet;
  156. char *FuncMod;
  157. char *TypeName;
  158. char *Methods;
  159. char *IMethods;
  160. char *BaseType;
  161. GUID gGuid;
  162. DWORD dwVTBLSize;
  163. DWORD dwVTBLOffset;
  164. int TypeId;
  165. int LineNumber;
  166. DWORD dwScopeLevel;
  167. struct _KnownTypesInfo *pktBase; // a cache, used by genthnk
  168. struct _KnownTypesInfo *pktRet; // a cache, used by genthnk
  169. int SizeMembers; // size of Members[], in bytes
  170. char *Members;
  171. char *FileName;
  172. PMEMBERINFO pmeminfo;
  173. PFUNCINFO pfuncinfo;
  174. DWORD dwArrayElements;
  175. DWORD dwBaseSize;
  176. struct _KnownTypesInfo *pTypedefBase;
  177. DWORD dwCurrentPacking;
  178. char Names[1];
  179. } KNOWNTYPES, *PKNOWNTYPES;
  180. typedef struct _RBTree {
  181. PKNOWNTYPES pRoot;
  182. PKNOWNTYPES pLastNodeInserted;
  183. } RBTREE, *PRBTREE;
  184. typedef struct _DefaultBasicTypes {
  185. char *BasicType;
  186. }DEFBASICTYPES, *PDEFBASICTYPES;
  187. typedef struct _TypesInfo {
  188. ULONG Flags;
  189. int IndLevel; // indirection level
  190. int Size; // size of the type in bytes
  191. int iPackSize; // packing size
  192. char BasicType[MAX_PATH];
  193. char BaseName[MAX_PATH];
  194. char FuncRet[MAX_PATH];
  195. char FuncMod[MAX_PATH];
  196. char TypeName[MAX_PATH]; // typedef or struc name
  197. TYPEKIND TypeKind; // how to interpret Members[] data
  198. PFUNCINFO pfuncinfo; // if TypeKind==TypeKindFunc, ptr to first FUNCINFO
  199. int RetIndLevel; // if TypeKind==TypeKindFunc, indlevel of return type for function
  200. DWORD dwMemberSize; // #bytes used in Members array
  201. char Members[FUNCMEMBERSIZE]; // stores either MEMBERINFOs or FUNCINFOs
  202. // Added to support automatic retrival of COM objects
  203. // If a class or struct is found with virtual methods, an extra VTLB member
  204. // is created at the top.
  205. // Note: a class has a VTLB if virtual methods are found or base class
  206. // has virtual methods
  207. // A type is a COM object if it is IUnknown or it derives from a COM object
  208. GUID gGuid; // Guid for this object if
  209. DWORD dwVTBLSize; // Total size of the VTBL
  210. DWORD dwVTBLOffset; // Offset of VTLB from parent
  211. char Methods[MEMBERMETHODSSIZE]; // Names of methods
  212. char IMethods[MEMBERMETHODSSIZE]; // Names of methods not inherited
  213. char BaseType[MAX_PATH]; // Name of the base class
  214. //////////////////////////////////////////////////////////////////////
  215. // Added to support reordering of definations later
  216. //////////////////////////////////////////////////////////////////////
  217. int TypeId; //is actually a defination ID
  218. char FileName[MAX_PATH];
  219. int LineNumber;
  220. DWORD dwCurrentPacking; // Packing level when structure defined
  221. DWORD dwScopeLevel;
  222. DWORD dwArrayElements; // If this is an array, the number of elements
  223. DWORD dwBaseSize; // Base size before it is multiplied for the array
  224. PKNOWNTYPES pTypedefBase;
  225. } TYPESINFO, *PTYPESINFO;
  226. #define BTI_DLLEXPORT 1 // the function decl had __declspec(dllimport)
  227. #define BTI_CONTAINSFUNCPTR 2 // the type contains a function pointer
  228. #define BTI_PTR64 4 // the type is a __ptr64
  229. #define BTI_HASGUID 8 // A guid has been found for this type
  230. #define BTI_ISCOM 16 // This is a COM object
  231. #define BTI_DISCARDABLE 32 // Type is overwriteable
  232. #define BTI_VIRTUALONLY 64 // Contains only virtual methods
  233. #define BTI_ANONYMOUS 128 // Type is anonymous
  234. #define BTI_POINTERDEP 256 // Type is dependent on the standard pointer size
  235. #define BTI_NOTDERIVED 512 // Type is not derived, but a placeholder
  236. #define BTI_ISARRAY 1024 // Element is an array
  237. #define BTI_UNSIGNED 2048 // Used only on default derived types
  238. // Signals that the type is unsigned
  239. #define BTI_INT64DEP 4096 // this is a 8byte integer value that
  240. // might be union as well
  241. // contiguous allocation in a buffer
  242. typedef struct _bufallocinfo {
  243. BYTE *pb; // ptr to buffer pool
  244. DWORD dwSize; // size of buffer pool
  245. DWORD dwLen; // current length of buffer pool
  246. } BUFALLOCINFO;
  247. typedef struct _TokenMatch {
  248. TOKENTYPE Tk;
  249. char *MatchString;
  250. } TOKENMATCH, *PTOKENMATCH;
  251. extern char *TokenString[];
  252. extern TOKENMATCH KeywordList[];
  253. typedef struct _Token {
  254. TOKENTYPE TokenType;
  255. union _TokenName {
  256. char *Name; // filled in only for TokenType==TK_IDENTIFIER or TK_STRING
  257. long Value; // filled in only for TokenType==TK_NUMBER
  258. };
  259. DWORD dwValue; //unsigned version of Value
  260. } TOKEN, *PTOKEN;
  261. #define MAX_CHARS_IN_LINE 4096
  262. #define MAX_TOKENS_IN_STATEMENT 4096
  263. extern TOKEN Tokens[MAX_TOKENS_IN_STATEMENT];
  264. extern int CurrentTokenIndex;
  265. void
  266. ResetLexer(
  267. void
  268. );
  269. char *
  270. LexOneLine(
  271. char *p,
  272. BOOL fStopAtStatement,
  273. BOOL *pfLexDone
  274. );
  275. BOOL
  276. UnlexToText(
  277. char *dest,
  278. int destlen,
  279. int StartToken,
  280. int EndToken
  281. );
  282. void
  283. DumpLexerOutput(
  284. int FirstToken
  285. );
  286. void
  287. HandlePreprocessorDirective(
  288. char *Line
  289. );
  290. TOKENTYPE
  291. ConsumeDirectionOpt(
  292. void
  293. );
  294. TOKENTYPE
  295. ConsumeConstVolatileOpt(
  296. void
  297. );
  298. PMEMBERINFO
  299. AllocMemInfoAndLink(
  300. BUFALLOCINFO *pbufallocinfo,
  301. PMEMBERINFO pmeminfo
  302. );
  303. PFUNCINFO
  304. AllocFuncInfoAndLink(
  305. BUFALLOCINFO *pbufallocinfo,
  306. PFUNCINFO pfuncinfo
  307. );
  308. DWORD
  309. SizeOfMultiSz(
  310. char *c
  311. );
  312. BOOL
  313. CatMultiSz(
  314. char *dest,
  315. char *source,
  316. DWORD dwMaxSize
  317. );
  318. BOOL
  319. AppendToMultiSz(
  320. char *dest,
  321. char *source,
  322. DWORD dwMaxSize
  323. );
  324. BOOL IsInMultiSz(
  325. const char *multisz,
  326. const char *element
  327. );
  328. BOOL
  329. ConvertStringToGuid(
  330. const char *pString,
  331. GUID *pGuid
  332. );
  333. //
  334. // Inline code
  335. #define iswhitespace(c) ((c == ' ') || (c == '\t'))
  336. //
  337. // initialize BUFALLOCINFO structure
  338. _inline void BufAllocInit(BUFALLOCINFO *pbufallocinfo,
  339. BYTE *pb, DWORD dwSize, DWORD dwLen)
  340. {
  341. pbufallocinfo->pb = pb;
  342. pbufallocinfo->dwSize = dwSize;
  343. pbufallocinfo->dwLen = dwLen;
  344. }
  345. //
  346. // allocate memory from buffer
  347. _inline void *BufAllocate(BUFALLOCINFO *pbufallocinfo, DWORD dwLen)
  348. {
  349. void *pv = NULL;
  350. DWORD dwNewLen;
  351. // Pad to quadword alignment, like malloc does, so RISC builds don't
  352. // take alignment faults.
  353. dwLen = (dwLen+7) & ~7;
  354. dwNewLen = pbufallocinfo->dwLen + dwLen;
  355. if (dwNewLen < pbufallocinfo->dwSize)
  356. {
  357. pv = &pbufallocinfo->pb[pbufallocinfo->dwLen];
  358. pbufallocinfo->dwLen = dwNewLen;
  359. }
  360. return(pv);
  361. }
  362. //
  363. // determine if we could allocate from buffer pool
  364. _inline BOOL BufCanAllocate(BUFALLOCINFO *pbufallocinfo, DWORD dwLen)
  365. {
  366. return( (pbufallocinfo->dwLen + dwLen) < pbufallocinfo->dwSize);
  367. }
  368. //
  369. // get pointer to current free area
  370. _inline void *BufPointer(BUFALLOCINFO *pbufallocinfo)
  371. {
  372. return(&pbufallocinfo->pb[pbufallocinfo->dwLen]);
  373. }
  374. //
  375. // get remaining space in buffer
  376. _inline DWORD BufGetFreeSpace(BUFALLOCINFO *pbufallocinfo)
  377. {
  378. return pbufallocinfo->dwSize - pbufallocinfo->dwLen;
  379. }
  380. _inline char *SkipWhiteSpace(char *s)
  381. {
  382. while (iswhitespace(*s) && (*s != 0)) {
  383. s++;
  384. }
  385. return(s);
  386. }
  387. __inline void
  388. ConsumeToken(
  389. void
  390. )
  391. {
  392. if (Tokens[CurrentTokenIndex].TokenType != TK_EOS) {
  393. CurrentTokenIndex++;
  394. }
  395. }
  396. __inline PTOKEN
  397. CurrentToken(
  398. void
  399. )
  400. {
  401. return &Tokens[CurrentTokenIndex];
  402. }
  403. //
  404. // function prototypes
  405. char *SkipKeyWord(char *pSrc, char *pKeyWord);
  406. BOOL IsSeparator(char ch);
  407. BOOL IsTokenSeparator(void);
  408. size_t CopyToken(char *pDst, char *pSrc, size_t Size);
  409. char *GetNextToken(char *pSrc);
  410. void DumpKnownTypes(PKNOWNTYPES pKnownTypes, FILE *fp);
  411. void DumpTypesInfo(PTYPESINFO pTypesInfo, FILE *fp);
  412. void FreeTypesList(PRBTREE HeadList);
  413. void __cdecl ErrMsg(char *pch, ...);
  414. void __cdecl ExitErrMsg(BOOL bSysError, char *pch, ...);
  415. void __cdecl DbgPrintf(char *pch, ...);
  416. char *ReadEntireFile(HANDLE hFile, DWORD *pBytesRead);
  417. HANDLE CreateTempFile(VOID);
  418. BOOL
  419. ParseTypes(
  420. PRBTREE pTypesList,
  421. PTYPESINFO pTypesInfo,
  422. PKNOWNTYPES *ppKnownTypes
  423. );
  424. PFUNCINFO
  425. RelocateTypesInfo(
  426. char *dest,
  427. PTYPESINFO src
  428. );
  429. void ParseIndirection(
  430. DWORD *pIndLevel,
  431. DWORD *pdwSize,
  432. DWORD *pFlags,
  433. PTOKENTYPE tkPrePostMod,
  434. PTOKENTYPE tkPostMod
  435. );
  436. PKNOWNTYPES
  437. GetNameFromTypesList(
  438. PRBTREE pHeadList,
  439. char *pTypeName
  440. );
  441. PDEFBASICTYPES
  442. GetDefBasicType(
  443. char *pBasicType
  444. );
  445. PKNOWNTYPES
  446. AddToTypesList(
  447. PRBTREE pHeadList,
  448. PTYPESINFO pTypesInfo
  449. );
  450. BOOL
  451. AddOpenFile(
  452. char *FileName,
  453. FILE *fp,
  454. HANDLE hFile
  455. );
  456. void
  457. DelOpenFile(
  458. FILE *fp,
  459. HANDLE hFile
  460. );
  461. void
  462. CloseOpenFileList(
  463. BOOL DeleteFiles
  464. );
  465. BOOL
  466. ConsoleControlHandler(
  467. DWORD dwCtrlType
  468. );
  469. //
  470. // global vars
  471. extern char szVARGS[];
  472. extern char szNULL[];
  473. extern char szCONST[];
  474. extern char szVOLATILE[];
  475. extern char szREGISTER[];
  476. extern char szEXTERN[];
  477. extern char szCDECL[];
  478. extern char sz_CDECL[];
  479. extern char szSTDCALL[];
  480. extern char sz__FASTCALL[];
  481. extern char szUNALIGNED[];
  482. extern char szTYPEDEF[];
  483. extern char szCHAR[];
  484. extern char szINT[];
  485. extern char szLONG[];
  486. extern char szSHORT[];
  487. extern char szDOUBLE[];
  488. extern char szENUM[];
  489. extern char szFLOAT[];
  490. extern char szSTRUCT[];
  491. extern char szUNION[];
  492. extern char szVOID[];
  493. extern char szINT64[];
  494. extern char sz_INT64[];
  495. extern char szFUNC[];
  496. extern char szSIGNED[];
  497. extern char szUNSIGNED[];
  498. extern char szFUNCTIONS[];
  499. extern char szSTRUCTURES[];
  500. extern char szTYPEDEFS[];
  501. extern char szPragma[];
  502. extern char szPack[];
  503. extern char szPush[];
  504. extern char szPop[];
  505. extern char szSTATIC[];
  506. extern char szUNSIGNEDCHAR[];
  507. extern char szUNSIGNEDSHORT[];
  508. extern char szUNSIGNEDLONG[];
  509. extern CHAR szINOUT[];
  510. extern CHAR szIN[];
  511. extern CHAR szOUT[];
  512. extern CHAR szVTBL[];
  513. extern char szGUID[];
  514. extern BOOLEAN bDebug;
  515. extern BOOLEAN bExitClean;
  516. extern PVOID (*fpTypesListMalloc)(ULONG Len);
  517. PKNOWNTYPES GetBasicType(
  518. char *sTypeName,
  519. PRBTREE TypeDefsList,
  520. PRBTREE StructsList);
  521. void ReplaceInTypesList(PKNOWNTYPES pKnownTypes, PTYPESINFO pTypesInfo);
  522. HANDLE CreateAllocCvmHeap(ULONG_PTR uBaseAddress,
  523. ULONG_PTR uReserveSize,
  524. ULONG_PTR uRegionSize,
  525. ULONG_PTR uUncomitted,
  526. ULONG_PTR uUnReserved,
  527. ULONG_PTR uAvailable);
  528. PVOID GetCvmHeapBaseAddress(HANDLE hCvmHeap);
  529. PVOID
  530. AllocCvm(HANDLE hCvmHeap,
  531. ULONG_PTR Size
  532. );
  533. void DeleteAllocCvmHeap(HANDLE hCvmHeap);
  534. // This structure is the first thing allocated within the CvmHeap. It contains
  535. // the roots of all data stored within the heap.
  536. typedef struct _CvmHeapHeader {
  537. ULONG Version;
  538. ULONG_PTR BaseAddress;
  539. RBTREE FuncsList;
  540. RBTREE StructsList;
  541. RBTREE TypeDefsList;
  542. KNOWNTYPES NIL;
  543. } CVMHEAPHEADER, *PCVMHEAPHEADER;
  544. PVOID GetCvmHeapAvailable(HANDLE hCvmHeap);
  545. // from redblack.c:
  546. VOID
  547. RBInsert(
  548. PRBTREE proot,
  549. PKNOWNTYPES x
  550. );
  551. PKNOWNTYPES
  552. RBFind(
  553. PRBTREE proot,
  554. PVOID addr
  555. );
  556. PKNOWNTYPES
  557. RBDelete(
  558. PRBTREE proot,
  559. PKNOWNTYPES z
  560. );
  561. VOID
  562. RBInitTree(
  563. PRBTREE proot
  564. );
  565. extern PKNOWNTYPES NIL;
  566. //
  567. // Use these allocators instead of malloc/free
  568. //
  569. PVOID GenHeapAlloc(INT_PTR Len);
  570. void GenHeapFree(PVOID pv);
  571. BOOL
  572. IsDefinedPointerDependent(
  573. char *pName
  574. );
  575. PCHAR
  576. IsDefinedPtrToPtrDependent(
  577. IN char *pName
  578. );
  579. BOOL
  580. ClosePpmFile(
  581. BOOL bExitFailure
  582. );
  583. PCVMHEAPHEADER
  584. MapPpmFile(
  585. char *sPpmfile,
  586. BOOL bExitFailure
  587. );
  588. char *GetHostPointerName(BOOL bIsPtr64);
  589. char *GetHostBasicTypeName(PKNOWNTYPES pkt);
  590. char *GetHostTypeName(PKNOWNTYPES pkt, char *pBuffer);