-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.c
More file actions
148 lines (121 loc) · 2.68 KB
/
Copy pathcodegen.c
File metadata and controls
148 lines (121 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/////////////////////////////////////////////////////////////////////////////
//
// /
// - P / E C E -
// / mobile equipment
//
// System Programs
//
//
// PIECE TOOLS : mucc : Ver 1.00
//
// Copyright (C)2001 AUQAPLUS Co., Ltd. / OeRSTED, Inc. all rights reserved.
//
// Coded by MIO.H (OeRSTED)
//
// Comments:
//
// ミュージック・データ・コンバーター
//
// v1.00 2001.11.09 MIO.H
//
// コード・ジェネレータ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mucc.h"
#include "hash.h"
#include "codegen.h"
#include "reserved.h"
#define MAXFWAD 2000 // 前方参照ラベルの最大個数
static unsigned char cgbuff[65536];
static int cgp = 0;
typedef struct tagFWAD {
HASHDATA *phd;
unsigned short adr;
unsigned short lno;
} FWAD;
static FWAD fwad[MAXFWAD];
static int fap = 0;
void cgout( int adr, int dat )
{
if ( adr >= cgp ) fatal( "Output buffer over" );
cgbuff[adr] = dat;
}
void cgout1( int a )
{
if ( cgp >= sizeof(cgbuff) ) fatal( "Output buffer over" );
cgbuff[cgp++] = a;
}
void cgout2( int a )
{
cgout1( a );
cgout1( a >> 8 );
}
void cgoutlen( int len )
{
if ( len < 127 ) cgout1( len );
else if ( len < 256 ) { cgout1( CCD_LEX ); cgout1( len ); }
else if ( len < 127+256 ) { cgout1( CCD_LEX ); cgout1( len-256 ); }
else error( "length over" );
}
void cgadrs( HASHDATA *phd )
{
//printf( "%d 0x%04x\n", phd->type, phd->value );
if ( phd->type == TYPE_LABEL0 ) {
if ( fap < MAXFWAD ) {
FWAD *pf = fwad + fap++;
pf->adr = cgp;
pf->phd = phd;
}
else fatal( "Too many forward label" );
}
cgout2( phd->value );
}
int cgnowadrs( void )
{
return cgp;
}
int cgfwad( void )
{
int i;
// 前方参照ラベルの解決
for ( i = 0; i < fap; i++ ) {
FWAD *pf = fwad + i;
HASHDATA *phd = pf->phd;
if ( phd->type == TYPE_LABEL ) {
cgbuff[pf->adr] = (unsigned char)phd->value;
cgbuff[pf->adr+1] = (unsigned char)(phd->value>>8);
}
else if ( phd->type == TYPE_LABEL0 ) {
if ( phd->value ) error( "Unknown label '%s'", phd->str );
}
else {
fatal( "Unknown label '%s'", phd->str );
}
}
return 0;
}
static int cgbin( FILE *fp )
{
return fwrite( cgbuff, 1, cgp, fp ) == (unsigned )cgp;
}
static int cgsrc( FILE *fp )
{
int i;
for ( i = 0; i < cgp; i++ ) {
fprintf( fp, "0x%02x,", cgbuff[i] );
if ( (i&7)==7 ) fprintf( fp, "\r\n" );
}
return 0;
}
int cgend( FILE *fp, int type )
{
cgfwad();
return type ? cgsrc( fp ) : cgbin( fp );
}
void cgreport( void )
{
fprintf( stderr, "Total %d(0x%0x) bytes\n", cgp, cgp );
}