1/*
2
3PCL6 Disassembler
4
5Copyright (c) 2003 Haiku.
6
7Author:
8	Michael Pfeiffer
9
10Permission is hereby granted, free of charge, to any person obtaining a copy of
11this software and associated documentation files (the "Software"), to deal in
12the Software without restriction, including without limitation the rights to
13use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14of the Software, and to permit persons to whom the Software is furnished to do
15so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28*/
29
30#ifndef _DISASM_H
31#define _DISASM_H
32
33#include "jetlib.h"
34#include <SupportDefs.h>
35#include <List.h>
36
37class InputStream {
38	uint8 fBuffer; // one byte only
39	int8 fBufferSize;
40	uint32 fPos;
41
42public:
43	InputStream() : fBufferSize(0), fPos(0) { }
44	virtual ~InputStream() {};
45	virtual int RawRead(void* buffer, int size) = 0;
46	int Read(void* buffer, int size);
47	int Pos() const { return fPos; }
48	bool PutUByte(uint8 v);
49	bool ReadUByte(uint8& v);
50	bool ReadUInt16(uint16& v);
51	bool ReadUInt32(uint32& v);
52	bool ReadSInt16(int16& v);
53	bool ReadSInt32(int32& v);
54	bool ReadReal32(float& v);
55};
56
57class File : public InputStream {
58	FILE* fFile;
59public:
60	File(const char* filename);
61	~File();
62	bool InitCheck() { return fFile != NULL; }
63	int RawRead(void* buffer, int size);
64};
65
66typedef struct {
67	uint8 value;
68	const char* name;
69} AttrValue;
70
71#define NUM_OF_ELEMS(array, type) (sizeof(array) / sizeof(type))
72
73class Disasm {
74public:
75	Disasm(InputStream* stream) : fStream(stream), fVerbose(false) { };
76	void SetVerbose(bool v) { fVerbose = v; }
77	void Print();
78
79private:
80	InputStream* fStream;
81	BList fArgs;
82	bool fVerbose;
83
84	void Error(const char* text);
85	bool Expect(const char* text);
86	bool SkipTo(const char* text);
87	bool ReadNumber(int32& n);
88	bool ParsePJL();
89
90	void PushArg(struct ATTRIBUTE* attr) { fArgs.AddItem(attr); }
91	int  NumOfArgs() { return fArgs.CountItems(); }
92	struct ATTRIBUTE* ArgAt(int i) { return (struct ATTRIBUTE*)fArgs.ItemAt(i); }
93	void DeleteAttr(struct ATTRIBUTE* attr);
94	void PrintAttr(struct ATTRIBUTE* attr);
95	void ClearAttrs();
96
97	bool DecodeOperator(uint8 byte);
98	struct ATTRIBUTE* ReadData(uint8 byte);
99	bool PushData(uint8 byte);
100	bool ReadArrayLength(uint32& length);
101	bool ReadArray(uint8 type, uint32 length, struct ATTRIBUTE* attr);
102	const char* AttributeName(uint8 id);
103	void GenericPrintAttribute(uint8 id);
104	void PrintAttributeValue(uint8 id, const AttrValue* table, int tableSize);
105	bool DecodeAttribute(uint8 byte);
106	bool DecodeEmbedData(uint8 byte);
107	bool ParsePCL6();
108
109	bool IsOperator(uint8 byte) const { return byte >= 0x41 && byte <= 0xb9; }
110	bool IsDataType(uint8 byte) const { return byte >= 0xc0 && byte <= 0xef; }
111	bool IsAttribute(uint8 byte) const { return byte >= 0xf8 && byte <= 0xf9; }
112	bool IsEmbedData(uint8 byte) const { return byte >= 0xfa && byte <= 0xfb; }
113	bool IsWhiteSpace(uint8 byte) const { return byte == 0 || byte >= 0x09 && byte <= 0x0d || byte == 0x20; }
114	bool IsEsc(uint8 byte) const { return byte == 0x1b; }
115};
116
117#endif
118