入出力時のエラー条件を変更
[YACASL2.git] / include / assemble.h
1 #ifndef YACASL2_ASSEMBLE_INCLUDED
2 #define YACASL2_ASSEMBLE_INCLUDED
3
4 #include <stdbool.h>
5 #include "struct.h"
6 #include "word.h"
7
8 /**
9  * CASL IIの仕様
10  */
11 enum {
12     LABELSIZE = 8,         /**<ラベルの最大文字数 */
13     OPDSIZE = 40,          /**<オペラントの最大数。CASL IIシミュレータの制限 */
14 };
15
16 /**
17  * YACASL2の制限
18  */
19 enum {
20     LINESIZE = 1024,       /**<行の最大文字数 */
21     TOKENSIZE = 256,       /**<トークンの最大文字数 */
22 };
23
24 /**
25  * アセンブルモード
26  */
27 typedef struct {
28     bool src;             /**<ソースを表示する場合はtrue */
29     bool label;           /**<ラベル表を表示する場合はtrue */
30     bool onlylabel;       /**<ラベル表を表示して終了する場合はtrue */
31     bool asdetail;        /**<アセンブラ詳細結果を表示する場合はtrue */
32     bool onlyassemble;    /**<アセンブルだけを行う場合はtrue */
33 } ASMODE;
34
35 extern ASMODE asmode;
36
37 /**
38  * アセンブル時の、現在およびリテラルのアドレスとプログラム入口名
39  */
40 typedef struct {
41     WORD ptr;     /**<現在のアドレス */
42     WORD lptr;    /**<リテラル(=付きの値)のアドレス */
43     char *prog;   /**<他のプログラムで参照する入口名 */
44 } ASPTR;
45
46 extern ASPTR *asptr;
47
48 /**
49   * アセンブラ命令を表す番号
50   */
51 typedef enum {
52     START = 01,
53     END = 02,
54     DS = 03,
55     DC = 04,
56 } ASCMDID;
57
58 /**
59   * アセンブラ命令を表す配列
60   */
61 typedef struct {
62     ASCMDID cmdid;              /**<アセンブル命令のID */
63     int opdc_min;               /**<最小オペランド数 */
64     int opdc_max;               /**<最大オペランド数 */
65     char *cmd;                  /**<コマンド名 */
66 } ASCMD;
67
68 /**
69  * マクロ命令を表す番号
70  */
71 typedef enum {
72     IN = 011,
73     OUT = 012,
74     RPUSH = 013,
75     RPOP = 014,
76 } MACROCMDID;
77
78 /**
79  * マクロ命令を表す配列
80  */
81 typedef struct {
82     MACROCMDID cmdid;              /**<マクロ命令のID */
83     int opdc_min;                  /**<最小オペランド数 */
84     int opdc_max;                  /**<最大オペランド数 */
85     char *cmd;                     /**<コマンド名 */
86 } MACROCMD;
87
88 /**
89  * ラベル配列
90  */
91 typedef struct {
92     char *prog;                 /**<プログラム  */
93     char *label;                /**<ラベル */
94     WORD adr;                   /**<アドレス */
95 } LABELARRAY;
96
97 /**
98  * ラベル表
99  */
100 typedef struct _LABELTAB {
101     struct _LABELTAB *next;     /**<リスト次項目へのポインタ */
102     char *prog;                 /**<プログラム名  */
103     char *label;                /**<ラベル名 */
104     WORD adr;                   /**<アドレス */
105 } LABELTAB;
106
107 enum {
108     /**
109      * ラベル表のサイズ
110      */
111     LABELTABSIZE = 251,
112 };
113
114 /**
115  * アセンブラが、1回目か2回目か
116  */
117 typedef enum {
118     FIRST = 0,
119     SECOND = 1,
120 } PASS;
121
122 /**
123  * ラベルのエラーをエラーリストに追加
124  */
125 void addcerrlist_label();
126
127 /**
128  * プログラム名とラベルに対応するアドレスをラベル表から検索する
129  */
130 WORD getlabel(const char *prog, const char *label);
131
132 /**
133  * プログラム名、ラベル、アドレスをラベル表に追加する
134  */
135 bool addlabel(const char *prog, const char *label, WORD word);
136
137 /**
138  * ラベル表を表示する
139  */
140 void printlabel();
141
142 /**
143  * ラベル表を解放する
144  */
145 void freelabel();
146
147 /**
148  * オペランド
149  */
150 typedef struct {
151     int opdc;                   /**<オペランド数 */
152     char *opdv[OPDSIZE];        /**<オペランド配列 */
153 } OPD;
154
155 /**
156  * 命令行
157  */
158 typedef struct {
159     char *label;                /**<ラベル */
160     char *cmd;                  /**<コマンド */
161     OPD *opd;                   /**<オペランド */
162 } CMDLINE;
163
164 /**
165  * トークン取得のエラーを追加
166  */
167 void addcerrlist_tok();
168
169 /**
170  * 空白またはタブで区切られた1行から、トークンを取得する
171  */
172 CMDLINE *linetok(const char *line);
173
174 /**
175  * アセンブルエラーをエラーリストに追加
176  */
177 void addcerrlist_assemble();
178
179 /**
180  * 指定された名前のファイルをアセンブル
181  * 1回目ではラベルを登録し、2回目ではラベルからアドレスを読み込む
182  * アセンブル完了時はtrue、エラー発生時はfalseを返す
183  */
184 bool assemblefile(const char *file, PASS pass);
185
186 /**
187  * 引数で指定したファイルにアセンブル結果を書込
188  */
189 void outassemble(const char *file);
190
191 #endif            /* YACASL2_ASSEMBLE_INCLUDEDの終端 */