1 /*****************************************************************************/
5 /* Objectcode management for the ca65 macroassembler */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
47 /*****************************************************************************/
49 /*****************************************************************************/
53 void Emit0 (unsigned char OPC)
54 /* Emit an instruction with a zero sized operand */
56 Fragment* F = GenFragment (FRAG_LITERAL, 1);
62 void Emit1 (unsigned char OPC, ExprNode* Value)
63 /* Emit an instruction with an one byte argument */
68 if (IsEasyConst (Value, &V)) {
70 /* Must be in byte range */
71 if (!IsByteRange (V)) {
72 Error ("Range error (%ld not in [0..255])", V);
75 /* Create a literal fragment */
76 F = GenFragment (FRAG_LITERAL, 2);
78 F->V.Data[1] = (unsigned char) V;
86 /* Emit the argument as an expression */
87 F = GenFragment (FRAG_EXPR, 1);
94 void Emit2 (unsigned char OPC, ExprNode* Value)
95 /* Emit an instruction with a two byte argument */
100 if (IsEasyConst (Value, &V)) {
102 /* Must be in byte range */
103 if (!IsWordRange (V)) {
104 Error ("Range error (%ld not in [0..65535])", V);
107 /* Create a literal fragment */
108 F = GenFragment (FRAG_LITERAL, 3);
110 F->V.Data[1] = (unsigned char) V;
111 F->V.Data[2] = (unsigned char) (V >> 8);
116 /* Emit the opcode */
119 /* Emit the argument as an expression */
120 F = GenFragment (FRAG_EXPR, 2);
127 void Emit3 (unsigned char OPC, ExprNode* Expr)
128 /* Emit an instruction with a three byte argument */
136 void EmitSigned (ExprNode* Expr, unsigned Size)
137 /* Emit a signed expression with the given size */
139 Fragment* F = GenFragment (FRAG_SEXPR, Size);
145 void EmitPCRel (unsigned char OPC, ExprNode* Expr, unsigned Size)
146 /* Emit an opcode with a PC relative argument of one or two bytes */
149 EmitSigned (Expr, Size);
154 void EmitData (const void* D, unsigned Size)
155 /* Emit data into the current segment */
157 /* Make a useful pointer from Data */
158 const unsigned char* Data = D;
160 /* Create lots of fragments for the data */
164 /* Determine the length of the next fragment */
166 if (Len > sizeof (F->V.Data)) {
167 Len = sizeof (F->V.Data);
170 /* Create a new fragment */
171 F = GenFragment (FRAG_LITERAL, Len);
174 memcpy (F->V.Data, Data, Len);
185 void EmitStrBuf (const StrBuf* Data)
186 /* Emit a string into the current segment */
188 /* Use EmitData to output the data */
189 EmitData (SB_GetConstBuf (Data), SB_GetLen (Data));
194 void EmitByte (ExprNode* Expr)
200 if (IsEasyConst (Expr, &V)) {
201 /* Must be in byte range */
202 if (!IsByteRange (V)) {
203 Error ("Range error (%ld not in [0..255])", V);
206 /* Create a literal fragment */
207 F = GenFragment (FRAG_LITERAL, 1);
208 F->V.Data[0] = (unsigned char) V;
211 /* Emit the argument as an expression */
212 F = GenFragment (FRAG_EXPR, 1);
219 void EmitWord (ExprNode* Expr)
225 if (IsEasyConst (Expr, &V)) {
226 /* Must be in byte range */
227 if (!IsWordRange (V)) {
228 Error ("Range error (%ld not in [0..65535])", V);
231 /* Create a literal fragment */
232 F = GenFragment (FRAG_LITERAL, 2);
233 F->V.Data[0] = (unsigned char) V;
234 F->V.Data[1] = (unsigned char) (V >> 8);
237 /* Emit the argument as an expression */
238 Fragment* F = GenFragment (FRAG_EXPR, 2);
245 void EmitFarAddr (ExprNode* Expr)
246 /* Emit a 24 bit expression */
248 /* Create a new fragment */
249 Fragment* F = GenFragment (FRAG_EXPR, 3);
257 void EmitDWord (ExprNode* Expr)
260 /* Create a new fragment */
261 Fragment* F = GenFragment (FRAG_EXPR, 4);
269 void EmitFill (unsigned long Count)
270 /* Emit Count fill bytes */
273 /* Calculate the size of the next chunk */
274 unsigned Chunk = (Count > 0xFFFF)? 0xFFFF : (unsigned) Count;
278 GenFragment (FRAG_FILL, Chunk);