]> git.sur5r.net Git - cc65/blob - src/ca65/pseudo.c
cca2fbe69cc78e4b8296ac225521e10c25c7201a
[cc65] / src / ca65 / pseudo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 pseudo.c                                  */
4 /*                                                                           */
5 /*              Pseudo instructions for the ca65 macroassembler              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2005, Ullrich von Bassewitz                                      */
10 /*                Römerstraße 52                                             */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <errno.h>
41
42 /* common */
43 #include "assertdefs.h"
44 #include "bitops.h"
45 #include "cddefs.h"
46 #include "coll.h"
47 #include "symdefs.h"
48 #include "tgttrans.h"
49 #include "xmalloc.h"
50
51 /* ca65 */
52 #include "anonname.h"
53 #include "asserts.h"
54 #include "condasm.h"
55 #include "dbginfo.h"
56 #include "enum.h"
57 #include "error.h"
58 #include "expr.h"
59 #include "feature.h"
60 #include "global.h"
61 #include "incpath.h"
62 #include "instr.h"
63 #include "listing.h"
64 #include "macpack.h"
65 #include "macro.h"
66 #include "nexttok.h"
67 #include "objcode.h"
68 #include "options.h"
69 #include "pseudo.h"
70 #include "repeat.h"
71 #include "segment.h"
72 #include "sizeof.h"
73 #include "spool.h"
74 #include "struct.h"
75 #include "symbol.h"
76 #include "symtab.h"
77
78
79
80 /*****************************************************************************/
81 /*                                   Data                                    */
82 /*****************************************************************************/
83
84
85
86 /* Keyword we're about to handle */
87 static char Keyword [sizeof (SVal)+1];
88
89 /* Segment stack */
90 #define MAX_PUSHED_SEGMENTS     16
91 static Collection SegStack = STATIC_COLLECTION_INITIALIZER;
92
93
94
95 /*****************************************************************************/
96 /*                               Forwards                                    */
97 /*****************************************************************************/
98
99
100
101 static void DoUnexpected (void);
102 /* Got an unexpected keyword */
103
104 static void DoInvalid (void);
105 /* Handle a token that is invalid here, since it should have been handled on
106  * a much lower level of the expression hierarchy. Getting this sort of token
107  * means that the lower level code has bugs.
108  * This function differs to DoUnexpected in that the latter may be triggered
109  * by the user by using keywords in the wrong location. DoUnexpected is not
110  * an error in the assembler itself, while DoInvalid is.
111  */
112
113
114
115 /*****************************************************************************/
116 /*                              Helper functions                             */
117 /*****************************************************************************/
118
119
120
121 static unsigned char OptionalAddrSize (void)
122 /* If a colon follows, parse an optional address size spec and return it.
123  * Otherwise return ADDR_SIZE_DEFAULT.
124  */
125 {
126     unsigned AddrSize = ADDR_SIZE_DEFAULT;
127     if (Tok == TOK_COLON) {
128         NextTok ();
129         AddrSize = ParseAddrSize ();
130         NextTok ();
131     }
132     return AddrSize;
133 }
134
135
136
137 static void SetBoolOption (unsigned char* Flag)
138 /* Read a on/off/+/- option and set flag accordingly */
139 {
140     static const char* Keys[] = {
141         "OFF",
142         "ON",
143     };
144
145     if (Tok == TOK_PLUS) {
146         *Flag = 1;
147         NextTok ();
148     } else if (Tok == TOK_MINUS) {
149         *Flag = 0;
150         NextTok ();
151     } else if (Tok == TOK_IDENT) {
152         /* Map the keyword to a number */
153         switch (GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]))) {
154             case 0:     *Flag = 0; NextTok ();                  break;
155             case 1:     *Flag = 1; NextTok ();                  break;
156             default:    ErrorSkip ("`on' or `off' expected");   break;
157         }
158     } else if (TokIsSep (Tok)) {
159         /* Without anything assume switch on */
160         *Flag = 1;
161     } else {
162         ErrorSkip ("`on' or `off' expected");
163     }
164 }
165
166
167
168 static void ExportImport (void (*Func) (SymEntry*, unsigned char, unsigned),
169                           unsigned char DefAddrSize, unsigned Flags)
170 /* Export or import symbols */
171 {
172     SymEntry* Sym;
173     unsigned char AddrSize;
174
175     while (1) {
176
177         /* We need an identifier here */
178         if (Tok != TOK_IDENT) {
179             ErrorSkip ("Identifier expected");
180             return;
181         }
182
183         /* Find the symbol table entry, allocate a new one if necessary */
184         Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
185
186         /* Skip the name */
187         NextTok ();
188
189         /* Get an optional address size */
190         AddrSize = OptionalAddrSize ();
191         if (AddrSize == ADDR_SIZE_DEFAULT) {
192             AddrSize = DefAddrSize;
193         }
194
195         /* Call the actual import/export function */
196         Func (Sym, AddrSize, Flags);
197
198         /* More symbols? */
199         if (Tok == TOK_COMMA) {
200             NextTok ();
201         } else {
202             break;
203         }
204     }
205 }
206
207
208
209 static long IntArg (long Min, long Max)
210 /* Read an integer argument and check a range. Accept the token "unlimited"
211  * and return -1 in this case.
212  */
213 {
214     if (Tok == TOK_IDENT && strcmp (SVal, "unlimited") == 0) {
215         NextTok ();
216         return -1;
217     } else {
218         long Val = ConstExpression ();
219         if (Val < Min || Val > Max) {
220             Error ("Range error");
221             Val = Min;
222         }
223         return Val;
224     }
225 }
226
227
228
229 static void ConDes (const char* Name, unsigned Type)
230 /* Parse remaining line for constructor/destructor of the remaining type */
231 {
232     long Prio;
233
234
235     /* Find the symbol table entry, allocate a new one if necessary */
236     SymEntry* Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
237
238     /* Optional constructor priority */
239     if (Tok == TOK_COMMA) {
240         /* Priority value follows */
241         NextTok ();
242         Prio = ConstExpression ();
243         if (Prio < CD_PRIO_MIN || Prio > CD_PRIO_MAX) {
244             /* Value out of range */
245             Error ("Range error");
246             return;
247         }
248     } else {
249         /* Use the default priority value */
250         Prio = CD_PRIO_DEF;
251     }
252
253     /* Define the symbol */
254     SymConDes (Sym, ADDR_SIZE_DEFAULT, Type, (unsigned) Prio);
255 }
256
257
258
259 /*****************************************************************************/
260 /*                             Handler functions                             */
261 /*****************************************************************************/
262
263
264
265 static void DoA16 (void)
266 /* Switch the accu to 16 bit mode (assembler only) */
267 {
268     if (GetCPU() != CPU_65816) {
269         Error ("Command is only valid in 65816 mode");
270     } else {
271         /* Immidiate mode has two extension bytes */
272         ExtBytes [AM65I_IMM_ACCU] = 2;
273     }
274 }
275
276
277
278 static void DoA8 (void)
279 /* Switch the accu to 8 bit mode (assembler only) */
280 {
281     if (GetCPU() != CPU_65816) {
282         Error ("Command is only valid in 65816 mode");
283     } else {
284         /* Immidiate mode has one extension byte */
285         ExtBytes [AM65I_IMM_ACCU] = 1;
286     }
287 }
288
289
290
291 static void DoAddr (void)
292 /* Define addresses */
293 {
294     while (1) {
295         if (GetCPU() == CPU_65816) {
296             EmitWord (GenWordExpr (Expression ()));
297         } else {
298             /* Do a range check */
299             EmitWord (Expression ());
300         }
301         if (Tok != TOK_COMMA) {
302             break;
303         } else {
304             NextTok ();
305         }
306     }
307 }
308
309
310
311 static void DoAlign (void)
312 /* Align the PC to some boundary */
313 {
314     long Val;
315     long Align;
316     unsigned Bit;
317
318     /* Read the alignment value */
319     Align = ConstExpression ();
320     if (Align <= 0 || Align > 0x10000) {
321         ErrorSkip ("Range error");
322         return;
323     }
324
325     /* Optional value follows */
326     if (Tok == TOK_COMMA) {
327         NextTok ();
328         Val = ConstExpression ();
329         /* We need a byte value here */
330         if (!IsByteRange (Val)) {
331             ErrorSkip ("Range error");
332             return;
333         }
334     } else {
335         Val = -1;
336     }
337
338     /* Check if the alignment is a power of two */
339     Bit = BitFind (Align);
340     if (Align != (0x01L << Bit)) {
341         Error ("Alignment value must be a power of 2");
342     } else {
343         SegAlign (Bit, (int) Val);
344     }
345 }
346
347
348
349 static void DoASCIIZ (void)
350 /* Define text with a zero terminator */
351 {
352     unsigned Len;
353
354     while (1) {
355         /* Must have a string constant */
356         if (Tok != TOK_STRCON) {
357             ErrorSkip ("String constant expected");
358             return;
359         }
360
361         /* Get the length of the string constant */
362         Len = strlen (SVal);
363
364         /* Translate into target charset and emit */
365         TgtTranslateBuf (SVal, Len);
366         EmitData ((unsigned char*) SVal, Len);
367         NextTok ();
368         if (Tok == TOK_COMMA) {
369             NextTok ();
370         } else {
371             break;
372         }
373     }
374     Emit0 (0);
375 }
376
377
378
379 static void DoAssert (void)
380 /* Add an assertion */
381 {
382     static const char* ActionTab [] = {
383         "WARN", "WARNING",
384         "ERROR"
385     };
386
387     int      Action;
388     unsigned Msg;
389
390     /* First we have the expression that has to evaluated */
391     ExprNode* Expr = Expression ();
392     ConsumeComma ();
393
394     /* Action follows */
395     if (Tok != TOK_IDENT) {
396         ErrorSkip ("Identifier expected");
397         return;
398     }
399     Action = GetSubKey (ActionTab, sizeof (ActionTab) / sizeof (ActionTab[0]));
400     switch (Action) {
401
402         case 0:
403         case 1:
404             /* Warning */
405             Action = ASSERT_ACT_WARN;
406             break;
407
408         case 2:
409             /* Error */
410             Action = ASSERT_ACT_ERROR;
411             break;
412
413         default:
414             Error ("Illegal assert action specifier");
415     }
416     NextTok ();
417
418     /* We can have an optional message. If no message is present, use
419      * "Assertion failed".
420      */
421     if (Tok == TOK_COMMA) {
422
423         /* Skip the comma */
424         NextTok ();
425
426         /* Read the message */
427         if (Tok != TOK_STRCON) {
428             ErrorSkip ("String constant expected");
429             return;
430         }
431
432         /* Translate the message into a string id. We can then skip the input
433          * string.
434          */
435         Msg = GetStringId (SVal);
436         NextTok ();
437
438     } else {
439
440         /* Use "Assertion failed" */
441         Msg = GetStringId ("Assertion failed");
442
443     }
444
445     /* Remember the assertion */
446     AddAssertion (Expr, Action, Msg);
447 }
448
449
450
451 static void DoAutoImport (void)
452 /* Mark unresolved symbols as imported */
453 {
454     SetBoolOption (&AutoImport);
455 }
456
457
458
459 static void DoBss (void)
460 /* Switch to the BSS segment */
461 {
462     UseSeg (&BssSegDef);
463 }
464
465
466
467 static void DoByte (void)
468 /* Define bytes */
469 {
470     while (1) {
471         if (Tok == TOK_STRCON) {
472             /* A string, translate into target charset and emit */
473             unsigned Len = strlen (SVal);
474             TgtTranslateBuf (SVal, Len);
475             EmitData ((unsigned char*) SVal, Len);
476             NextTok ();
477         } else {
478             EmitByte (Expression ());
479         }
480         if (Tok != TOK_COMMA) {
481             break;
482         } else {
483             NextTok ();
484             /* Do smart handling of dangling comma */
485             if (Tok == TOK_SEP) {
486                 Error ("Unexpected end of line");
487                 break;
488             }
489         }
490     }
491 }
492
493
494
495 static void DoCase (void)
496 /* Switch the IgnoreCase option */
497 {
498     SetBoolOption (&IgnoreCase);
499     IgnoreCase = !IgnoreCase;
500 }
501
502
503
504 static void DoCharMap (void)
505 /* Allow custome character mappings */
506 {
507     long Index;
508     long Code;
509
510     /* Read the index as numerical value */
511     Index = ConstExpression ();
512     if (Index < 0 || Index > 255) {
513         /* Value out of range */
514         ErrorSkip ("Range error");
515         return;
516     }
517
518     /* Comma follows */
519     ConsumeComma ();
520
521     /* Read the character code */
522     Code = ConstExpression ();
523     if (Code < 0 || Code > 255) {
524         /* Value out of range */
525         ErrorSkip ("Range error");
526         return;
527     }
528
529     /* Set the character translation */
530     TgtTranslateSet ((unsigned) Index, (unsigned char) Code);
531 }
532
533
534
535 static void DoCode (void)
536 /* Switch to the code segment */
537 {
538     UseSeg (&CodeSegDef);
539 }
540
541
542
543 static void DoConDes (void)
544 /* Export a symbol as constructor/destructor */
545 {
546     static const char* Keys[] = {
547         "CONSTRUCTOR",
548         "DESTRUCTOR",
549         "INTERRUPTOR",
550     };
551     char Name [sizeof (SVal)];
552     long Type;
553
554     /* Symbol name follows */
555     if (Tok != TOK_IDENT) {
556         ErrorSkip ("Identifier expected");
557         return;
558     }
559     strcpy (Name, SVal);
560     NextTok ();
561
562     /* Type follows. May be encoded as identifier or numerical */
563     ConsumeComma ();
564     if (Tok == TOK_IDENT) {
565
566         /* Map the following keyword to a number, then skip it */
567         Type = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
568         NextTok ();
569
570         /* Check if we got a valid keyword */
571         if (Type < 0) {
572             Error ("Syntax error");
573             SkipUntilSep ();
574             return;
575         }
576
577     } else {
578
579         /* Read the type as numerical value */
580         Type = ConstExpression ();
581         if (Type < CD_TYPE_MIN || Type > CD_TYPE_MAX) {
582             /* Value out of range */
583             Error ("Range error");
584             return;
585         }
586
587     }
588
589     /* Parse the remainder of the line and export the symbol */
590     ConDes (Name, (unsigned) Type);
591 }
592
593
594
595 static void DoConstructor (void)
596 /* Export a symbol as constructor */
597 {
598     char Name [sizeof (SVal)];
599
600     /* Symbol name follows */
601     if (Tok != TOK_IDENT) {
602         ErrorSkip ("Identifier expected");
603         return;
604     }
605     strcpy (Name, SVal);
606     NextTok ();
607
608     /* Parse the remainder of the line and export the symbol */
609     ConDes (Name, CD_TYPE_CON);
610 }
611
612
613
614 static void DoData (void)
615 /* Switch to the data segment */
616 {
617     UseSeg (&DataSegDef);
618 }
619
620
621
622 static void DoDbg (void)
623 /* Add debug information from high level code */
624 {
625     static const char* Keys[] = {
626         "FILE",
627         "LINE",
628         "SYM",
629     };
630     int Key;
631
632
633     /* We expect a subkey */
634     if (Tok != TOK_IDENT) {
635         ErrorSkip ("Identifier expected");
636         return;
637     }
638
639     /* Map the following keyword to a number */
640     Key = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
641
642     /* Skip the subkey */
643     NextTok ();
644
645     /* Check the key and dispatch to a handler */
646     switch (Key) {
647         case 0:     DbgInfoFile ();             break;
648         case 1:     DbgInfoLine ();             break;
649         case 2:     DbgInfoSym ();              break;
650         default:    ErrorSkip ("Syntax error"); break;
651     }
652 }
653
654
655
656 static void DoDByt (void)
657 /* Output double bytes */
658 {
659     while (1) {
660         EmitWord (GenSwapExpr (Expression ()));
661         if (Tok != TOK_COMMA) {
662             break;
663         } else {
664             NextTok ();
665         }
666     }
667 }
668
669
670
671 static void DoDebugInfo (void)
672 /* Switch debug info on or off */
673 {
674     SetBoolOption (&DbgSyms);
675 }
676
677
678
679 static void DoDefine (void)
680 /* Define a one line macro */
681 {
682     MacDef (MAC_STYLE_DEFINE);
683 }
684
685
686
687 static void DoDestructor (void)
688 /* Export a symbol as destructor */
689 {
690     char Name [sizeof (SVal)];
691
692     /* Symbol name follows */
693     if (Tok != TOK_IDENT) {
694         ErrorSkip ("Identifier expected");
695         return;
696     }
697     strcpy (Name, SVal);
698     NextTok ();
699
700     /* Parse the remainder of the line and export the symbol */
701     ConDes (Name, CD_TYPE_DES);
702 }
703
704
705
706 static void DoDWord (void)
707 /* Define dwords */
708 {
709     while (1) {
710         EmitDWord (Expression ());
711         if (Tok != TOK_COMMA) {
712             break;
713         } else {
714             NextTok ();
715         }
716     }
717 }
718
719
720
721 static void DoEnd (void)
722 /* End of assembly */
723 {
724     ForcedEnd = 1;
725     NextTok ();
726 }
727
728
729
730 static void DoEndProc (void)
731 /* Leave a lexical level */
732 {
733     if (GetCurrentSymTabType () != ST_PROC) {
734         /* No local scope */
735         ErrorSkip ("No open .PROC");
736     } else {
737         SymLeaveLevel ();
738     }
739 }
740
741
742
743 static void DoEndScope (void)
744 /* Leave a lexical level */
745 {
746     if ( GetCurrentSymTabType () != ST_SCOPE) {
747         /* No local scope */
748         ErrorSkip ("No open .SCOPE");
749     } else {
750         SymLeaveLevel ();
751     }
752 }
753
754
755
756 static void DoError (void)
757 /* User error */
758 {
759     if (Tok != TOK_STRCON) {
760         ErrorSkip ("String constant expected");
761     } else {
762         Error ("User error: %s", SVal);
763         SkipUntilSep ();
764     }
765 }
766
767
768
769 static void DoExitMacro (void)
770 /* Exit a macro expansion */
771 {
772     if (!InMacExpansion ()) {
773         /* We aren't expanding a macro currently */
774         DoUnexpected ();
775     } else {
776         MacAbort ();
777     }
778 }
779
780
781
782 static void DoExport (void)
783 /* Export a symbol */
784 {
785     ExportImport (SymExport, ADDR_SIZE_DEFAULT, SF_NONE);
786 }
787
788
789
790 static void DoExportZP (void)
791 /* Export a zeropage symbol */
792 {
793     ExportImport (SymExport, ADDR_SIZE_ZP, SF_NONE);
794 }
795
796
797
798 static void DoFarAddr (void)
799 /* Define far addresses (24 bit) */
800 {
801     while (1) {
802         EmitFarAddr (Expression ());
803         if (Tok != TOK_COMMA) {
804             break;
805         } else {
806             NextTok ();
807         }
808     }
809 }
810
811
812
813 static void DoFeature (void)
814 /* Switch the Feature option */
815 {
816     /* Allow a list of comma separated keywords */
817     while (1) {
818
819         /* We expect an identifier */
820         if (Tok != TOK_IDENT) {
821             ErrorSkip ("Identifier expected");
822             return;
823         }
824
825         /* Make the string attribute lower case */
826         LocaseSVal ();
827
828         /* Set the feature and check for errors */
829         if (SetFeature (SVal) == FEAT_UNKNOWN) {
830             /* Not found */
831             ErrorSkip ("Invalid feature: `%s'", SVal);
832             return;
833         } else {
834             /* Skip the keyword */
835             NextTok ();
836         }
837
838         /* Allow more than one keyword */
839         if (Tok == TOK_COMMA) {
840             NextTok ();
841         } else {
842             break;
843         }
844     }
845 }
846
847
848
849 static void DoFileOpt (void)
850 /* Insert a file option */
851 {
852     long OptNum;
853
854     /* The option type may be given as a keyword or as a number. */
855     if (Tok == TOK_IDENT) {
856
857         /* Option given as keyword */
858         static const char* Keys [] = {
859             "AUTHOR", "COMMENT", "COMPILER"
860         };
861
862         /* Map the option to a number */
863         OptNum = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
864         if (OptNum < 0) {
865             /* Not found */
866             ErrorSkip ("File option keyword expected");
867             return;
868         }
869
870         /* Skip the keyword */
871         NextTok ();
872
873         /* Must be followed by a comma */
874         ConsumeComma ();
875
876         /* We accept only string options for now */
877         if (Tok != TOK_STRCON) {
878             ErrorSkip ("String constant expected");
879             return;
880         }
881
882         /* Insert the option */
883         switch (OptNum) {
884
885             case 0:
886                 /* Author */
887                 OptAuthor (SVal);
888                 break;
889
890             case 1:
891                 /* Comment */
892                 OptComment (SVal);
893                 break;
894
895             case 2:
896                 /* Compiler */
897                 OptCompiler (SVal);
898                 break;
899
900             default:
901                 Internal ("Invalid OptNum: %ld", OptNum);
902
903         }
904
905         /* Done */
906         NextTok ();
907
908     } else {
909
910         /* Option given as number */
911         OptNum = ConstExpression ();
912         if (!IsByteRange (OptNum)) {
913             ErrorSkip ("Range error");
914             return;
915         }
916
917         /* Must be followed by a comma */
918         ConsumeComma ();
919
920         /* We accept only string options for now */
921         if (Tok != TOK_STRCON) {
922             ErrorSkip ("String constant expected");
923             return;
924         }
925
926         /* Insert the option */
927         OptStr ((unsigned char) OptNum, SVal);
928
929         /* Done */
930         NextTok ();
931     }
932 }
933
934
935
936 static void DoForceImport (void)
937 /* Do a forced import on a symbol */
938 {
939     ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_FORCED);
940 }
941
942
943
944 static void DoGlobal (void)
945 /* Declare a global symbol */
946 {
947     ExportImport (SymGlobal, ADDR_SIZE_DEFAULT, SF_NONE);
948 }
949
950
951
952 static void DoGlobalZP (void)
953 /* Declare a global zeropage symbol */
954 {
955     ExportImport (SymGlobal, ADDR_SIZE_ZP, SF_NONE);
956 }
957
958
959
960 static void DoI16 (void)
961 /* Switch the index registers to 16 bit mode (assembler only) */
962 {
963     if (GetCPU() != CPU_65816) {
964         Error ("Command is only valid in 65816 mode");
965     } else {
966         /* Immidiate mode has two extension bytes */
967         ExtBytes [AM65I_IMM_INDEX] = 2;
968     }
969 }
970
971
972
973 static void DoI8 (void)
974 /* Switch the index registers to 16 bit mode (assembler only) */
975 {
976     if (GetCPU() != CPU_65816) {
977         Error ("Command is only valid in 65816 mode");
978     } else {
979         /* Immidiate mode has one extension byte */
980         ExtBytes [AM65I_IMM_INDEX] = 1;
981     }
982 }
983
984
985
986 static void DoImport (void)
987 /* Import a symbol */
988 {
989     ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_NONE);
990 }
991
992
993
994 static void DoImportZP (void)
995 /* Import a zero page symbol */
996 {
997     ExportImport (SymImport, ADDR_SIZE_ZP, SF_NONE);
998 }
999
1000
1001
1002 static void DoIncBin (void)
1003 /* Include a binary file */
1004 {
1005     char Name [sizeof (SVal)];
1006     long Start = 0L;
1007     long Count = -1L;
1008     long Size;
1009     FILE* F;
1010
1011     /* Name must follow */
1012     if (Tok != TOK_STRCON) {
1013         ErrorSkip ("String constant expected");
1014         return;
1015     }
1016     strcpy (Name, SVal);
1017     NextTok ();
1018
1019     /* A starting offset may follow */
1020     if (Tok == TOK_COMMA) {
1021         NextTok ();
1022         Start = ConstExpression ();
1023
1024         /* And a length may follow */
1025         if (Tok == TOK_COMMA) {
1026             NextTok ();
1027             Count = ConstExpression ();
1028         }
1029
1030     }
1031
1032     /* Try to open the file */
1033     F = fopen (Name, "rb");
1034     if (F == 0) {
1035
1036         /* Search for the file in the include directories. */
1037         char* PathName = FindInclude (Name);
1038         if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
1039             /* Not found or cannot open, print an error and bail out */
1040             ErrorSkip ("Cannot open include file `%s': %s", Name, strerror (errno));
1041         }
1042
1043         /* Free the allocated memory */
1044         xfree (PathName);
1045
1046         /* If we had an error before, bail out now */
1047         if (F == 0) {
1048             return;
1049         }
1050     }
1051
1052     /* Get the size of the file */
1053     fseek (F, 0, SEEK_END);
1054     Size = ftell (F);
1055
1056     /* If a count was not given, calculate it now */
1057     if (Count < 0) {
1058         Count = Size - Start;
1059         if (Count < 0) {
1060             /* Nothing to read - flag this as a range error */
1061             ErrorSkip ("Range error");
1062             goto Done;
1063         }
1064     } else {
1065         /* Count was given, check if it is valid */
1066         if (Start + Count > Size) {
1067             ErrorSkip ("Range error");
1068             goto Done;
1069         }
1070     }
1071
1072     /* Seek to the start position */
1073     fseek (F, Start, SEEK_SET);
1074
1075     /* Read chunks and insert them into the output */
1076     while (Count > 0) {
1077
1078         unsigned char Buf [1024];
1079
1080         /* Calculate the number of bytes to read */
1081         size_t BytesToRead = (Count > (long)sizeof(Buf))? sizeof(Buf) : (size_t) Count;
1082
1083         /* Read chunk */
1084         size_t BytesRead = fread (Buf, 1, BytesToRead, F);
1085         if (BytesToRead != BytesRead) {
1086             /* Some sort of error */
1087             ErrorSkip ("Cannot read from include file `%s': %s",
1088                        Name, strerror (errno));
1089             break;
1090         }
1091
1092         /* Insert it into the output */
1093         EmitData (Buf, BytesRead);
1094
1095         /* Keep the counters current */
1096         Count -= BytesRead;
1097     }
1098
1099 Done:
1100     /* Close the file, ignore errors since it's r/o */
1101     (void) fclose (F);
1102 }
1103
1104
1105
1106 static void DoInclude (void)
1107 /* Include another file */
1108 {
1109     /* Name must follow */
1110     if (Tok != TOK_STRCON) {
1111         ErrorSkip ("String constant expected");
1112     } else {
1113         NewInputFile (SVal);
1114     }
1115 }
1116
1117
1118
1119 static void DoInterruptor (void)
1120 /* Export a symbol as interruptor */
1121 {
1122     char Name [sizeof (SVal)];
1123
1124     /* Symbol name follows */
1125     if (Tok != TOK_IDENT) {
1126         ErrorSkip ("Identifier expected");
1127         return;
1128     }
1129     strcpy (Name, SVal);
1130     NextTok ();
1131
1132     /* Parse the remainder of the line and export the symbol */
1133     ConDes (Name, CD_TYPE_INT);
1134 }
1135
1136
1137
1138 static void DoInvalid (void)
1139 /* Handle a token that is invalid here, since it should have been handled on
1140  * a much lower level of the expression hierarchy. Getting this sort of token
1141  * means that the lower level code has bugs.
1142  * This function differs to DoUnexpected in that the latter may be triggered
1143  * by the user by using keywords in the wrong location. DoUnexpected is not
1144  * an error in the assembler itself, while DoInvalid is.
1145  */
1146 {
1147     Internal ("Unexpected token: %s", Keyword);
1148 }
1149
1150
1151
1152 static void DoLineCont (void)
1153 /* Switch the use of line continuations */
1154 {
1155     SetBoolOption (&LineCont);
1156 }
1157
1158
1159
1160 static void DoList (void)
1161 /* Enable/disable the listing */
1162 {
1163     /* Get the setting */
1164     unsigned char List;
1165     SetBoolOption (&List);
1166
1167     /* Manage the counter */
1168     if (List) {
1169         EnableListing ();
1170     } else {
1171         DisableListing ();
1172     }
1173 }
1174
1175
1176
1177 static void DoListBytes (void)
1178 /* Set maximum number of bytes to list for one line */
1179 {
1180     SetListBytes (IntArg (MIN_LIST_BYTES, MAX_LIST_BYTES));
1181 }
1182
1183
1184
1185 static void DoLocalChar (void)
1186 /* Define the character that starts local labels */
1187 {
1188     if (Tok != TOK_CHARCON) {
1189         ErrorSkip ("Character constant expected");
1190     } else {
1191         if (IVal != '@' && IVal != '?') {
1192             Error ("Invalid start character for locals");
1193         } else {
1194             LocalStart = (char) IVal;
1195         }
1196         NextTok ();
1197     }
1198 }
1199
1200
1201
1202 static void DoMacPack (void)
1203 /* Insert a macro package */
1204 {
1205     int Package;
1206
1207     /* We expect an identifier */
1208     if (Tok != TOK_IDENT) {
1209         ErrorSkip ("Identifier expected");
1210         return;
1211     }
1212
1213     /* Search for the macro package name */
1214     Package = MacPackFind (SVal);
1215     if (Package < 0) {
1216         /* Not found */
1217         ErrorSkip ("Invalid macro package");
1218         return;
1219     }
1220
1221     /* Insert the package */
1222     MacPackInsert (Package);
1223 }
1224
1225
1226
1227 static void DoMacro (void)
1228 /* Start a macro definition */
1229 {
1230     MacDef (MAC_STYLE_CLASSIC);
1231 }
1232
1233
1234
1235 static void DoNull (void)
1236 /* Switch to the NULL segment */
1237 {
1238     UseSeg (&NullSegDef);
1239 }
1240
1241
1242
1243 static void DoOrg (void)
1244 /* Start absolute code */
1245 {
1246     long PC = ConstExpression ();
1247     if (PC < 0 || PC > 0xFFFFFF) {
1248         Error ("Range error");
1249         return;
1250     }
1251     SetAbsPC (PC);
1252 }
1253
1254
1255
1256 static void DoOut (void)
1257 /* Output a string */
1258 {
1259     if (Tok != TOK_STRCON) {
1260         ErrorSkip ("String constant expected");
1261     } else {
1262         /* Output the string and be sure to flush the output to keep it in
1263          * sync with any error messages if the output is redirected to a file.
1264          */
1265         printf ("%s\n", SVal);
1266         fflush (stdout);
1267         NextTok ();
1268     }
1269 }
1270
1271
1272
1273 static void DoP02 (void)
1274 /* Switch to 6502 CPU */
1275 {
1276     SetCPU (CPU_6502);
1277 }
1278
1279
1280
1281 static void DoPC02 (void)
1282 /* Switch to 65C02 CPU */
1283 {
1284     SetCPU (CPU_65C02);
1285 }
1286
1287
1288
1289 static void DoP816 (void)
1290 /* Switch to 65816 CPU */
1291 {
1292     SetCPU (CPU_65816);
1293 }
1294
1295
1296
1297 static void DoPageLength (void)
1298 /* Set the page length for the listing */
1299 {
1300     PageLength = IntArg (MIN_PAGE_LEN, MAX_PAGE_LEN);
1301 }
1302
1303
1304
1305 static void DoPopSeg (void)
1306 /* Pop an old segment from the segment stack */
1307 {
1308     SegDef* Def;
1309
1310     /* Must have a segment on the stack */
1311     if (CollCount (&SegStack) == 0) {
1312         ErrorSkip ("Segment stack is empty");
1313         return;
1314     }
1315
1316     /* Pop the last element */
1317     Def = CollPop (&SegStack);
1318
1319     /* Restore this segment */
1320     UseSeg (Def);
1321
1322     /* Delete the segment definition */
1323     FreeSegDef (Def);
1324 }
1325
1326
1327
1328 static void DoProc (void)
1329 /* Start a new lexical scope */
1330 {
1331     char Name[sizeof(SVal)];
1332     unsigned char AddrSize;
1333
1334     if (Tok == TOK_IDENT) {
1335
1336         SymEntry* Sym;
1337
1338         /* The new scope has a name. Remember it. */
1339         strcpy (Name, SVal);
1340
1341         /* Search for the symbol, generate a new one if needed */
1342         Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
1343
1344         /* Skip the scope name */
1345         NextTok ();
1346
1347         /* Read an optional address size specifier */
1348         AddrSize = OptionalAddrSize ();
1349
1350         /* Mark the symbol as defined */
1351         SymDef (Sym, GenCurrentPC (), AddrSize, SF_LABEL);
1352
1353     } else {
1354
1355         /* A .PROC statement without a name */
1356         Warning (1, "Unnamed .PROCs are deprecated, please use .SCOPE");
1357         AnonName (Name, sizeof (Name), "PROC");
1358         AddrSize = ADDR_SIZE_DEFAULT;
1359
1360     }
1361
1362     /* Enter a new scope */
1363     SymEnterLevel (Name, ST_PROC, AddrSize);
1364 }
1365
1366
1367
1368 static void DoPSC02 (void)
1369 /* Switch to 65SC02 CPU */
1370 {
1371     SetCPU (CPU_65SC02);
1372 }
1373
1374
1375
1376 static void DoPushSeg (void)
1377 /* Push the current segment onto the segment stack */
1378 {
1379     /* Can only push a limited size of segments */
1380     if (CollCount (&SegStack) >= MAX_PUSHED_SEGMENTS) {
1381         ErrorSkip ("Segment stack overflow");
1382         return;
1383     }
1384
1385     /* Get the current segment and push it */
1386     CollAppend (&SegStack, DupSegDef (GetCurrentSegDef ()));
1387 }
1388
1389
1390
1391 static void DoReloc (void)
1392 /* Enter relocatable mode */
1393 {
1394     RelocMode = 1;
1395 }
1396
1397
1398
1399 static void DoRepeat (void)
1400 /* Repeat some instruction block */
1401 {
1402     ParseRepeat ();
1403 }
1404
1405
1406
1407 static void DoRes (void)
1408 /* Reserve some number of storage bytes */
1409 {
1410     long Count;
1411     long Val;
1412
1413     Count = ConstExpression ();
1414     if (Count > 0xFFFF || Count < 0) {
1415         ErrorSkip ("Range error");
1416         return;
1417     }
1418     if (Tok == TOK_COMMA) {
1419         NextTok ();
1420         Val = ConstExpression ();
1421         /* We need a byte value here */
1422         if (!IsByteRange (Val)) {
1423             ErrorSkip ("Range error");
1424             return;
1425         }
1426
1427         /* Emit constant values */
1428         while (Count--) {
1429             Emit0 ((unsigned char) Val);
1430         }
1431
1432     } else {
1433         /* Emit fill fragments */
1434         EmitFill (Count);
1435     }
1436 }
1437
1438
1439
1440 static void DoROData (void)
1441 /* Switch to the r/o data segment */
1442 {
1443     UseSeg (&RODataSegDef);
1444 }
1445
1446
1447
1448 static void DoScope (void)
1449 /* Start a local scope */
1450 {
1451     char Name[sizeof (SVal)];
1452     unsigned char AddrSize;
1453
1454
1455     if (Tok == TOK_IDENT) {
1456
1457         /* The new scope has a name. Remember and skip it. */
1458         strcpy (Name, SVal);
1459         NextTok ();
1460
1461     } else {
1462
1463         /* An unnamed scope */
1464         AnonName (Name, sizeof (Name), "SCOPE");
1465
1466     }
1467
1468     /* Read an optional address size specifier */
1469     AddrSize = OptionalAddrSize ();
1470
1471     /* Enter the new scope */
1472     SymEnterLevel (Name, ST_SCOPE, AddrSize);
1473
1474 }
1475
1476
1477
1478 static void DoSegment (void)
1479 /* Switch to another segment */
1480 {
1481     char Name [sizeof (SVal)];
1482     SegDef Def;
1483     Def.Name = Name;
1484
1485     if (Tok != TOK_STRCON) {
1486         ErrorSkip ("String constant expected");
1487     } else {
1488
1489         /* Save the name of the segment and skip it */
1490         strcpy (Name, SVal);
1491         NextTok ();
1492
1493         /* Check for an optional address size modifier */
1494         Def.AddrSize = OptionalAddrSize ();
1495
1496         /* Set the segment */
1497         UseSeg (&Def);
1498     }
1499 }
1500
1501
1502
1503 static void DoSetCPU (void)
1504 /* Switch the CPU instruction set */
1505 {
1506     /* We expect an identifier */
1507     if (Tok != TOK_STRCON) {
1508         ErrorSkip ("String constant expected");
1509     } else {
1510         /* Try to find the CPU */
1511         cpu_t CPU = FindCPU (SVal);
1512
1513         /* Switch to the new CPU */
1514         SetCPU (CPU);
1515
1516         /* Skip the identifier. If the CPU switch was successful, the scanner
1517          * will treat the input now correctly for the new CPU.
1518          */
1519         NextTok ();
1520     }
1521 }
1522
1523
1524
1525 static void DoSmart (void)
1526 /* Smart mode on/off */
1527 {
1528     SetBoolOption (&SmartMode);
1529 }
1530
1531
1532
1533 static void DoSunPlus (void)
1534 /* Switch to the SUNPLUS CPU */
1535 {
1536     SetCPU (CPU_SUNPLUS);
1537 }
1538
1539
1540
1541 static void DoTag (void)
1542 /* Allocate space for a struct */
1543 {
1544     SymEntry* SizeSym;
1545     long Size;
1546
1547     /* Read the struct name */
1548     SymTable* Struct = ParseScopedSymTable ();
1549
1550     /* Check the supposed struct */
1551     if (Struct == 0) {
1552         ErrorSkip ("Unknown struct");
1553         return;
1554     }
1555     if (GetSymTabType (Struct) != ST_STRUCT) {
1556         ErrorSkip ("Not a struct");
1557         return;
1558     }
1559
1560     /* Get the symbol that defines the size of the struct */
1561     SizeSym = GetSizeOfScope (Struct);
1562
1563     /* Check if it does exist and if its value is known */
1564     if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
1565         ErrorSkip ("Size of struct/union is unknown");
1566         return;
1567     }
1568
1569     /* Optional multiplicator may follow */
1570     if (Tok == TOK_COMMA) {
1571         long Multiplicator;
1572         NextTok ();
1573         Multiplicator = ConstExpression ();
1574         /* Multiplicator must make sense */
1575         if (Multiplicator <= 0) {
1576             ErrorSkip ("Range error");
1577             return;
1578         }
1579         Size *= Multiplicator;
1580     }
1581
1582     /* Emit fill fragments */
1583     EmitFill (Size);
1584 }
1585
1586
1587
1588 static void DoUnexpected (void)
1589 /* Got an unexpected keyword */
1590 {
1591     Error ("Unexpected `%s'", Keyword);
1592     SkipUntilSep ();
1593 }
1594
1595
1596
1597 static void DoWarning (void)
1598 /* User warning */
1599 {
1600     if (Tok != TOK_STRCON) {
1601         ErrorSkip ("String constant expected");
1602     } else {
1603         Warning (0, "User warning: %s", SVal);
1604         SkipUntilSep ();
1605     }
1606 }
1607
1608
1609
1610 static void DoWord (void)
1611 /* Define words */
1612 {
1613     while (1) {
1614         EmitWord (Expression ());
1615         if (Tok != TOK_COMMA) {
1616             break;
1617         } else {
1618             NextTok ();
1619         }
1620     }
1621 }
1622
1623
1624
1625 static void DoZeropage (void)
1626 /* Switch to the zeropage segment */
1627 {
1628     UseSeg (&ZeropageSegDef);
1629 }
1630
1631
1632
1633 /*****************************************************************************/
1634 /*                                Table data                                 */
1635 /*****************************************************************************/
1636
1637
1638
1639 /* Control commands flags */
1640 enum {
1641     ccNone      = 0x0000,               /* No special flags */
1642     ccKeepToken = 0x0001                /* Do not skip the current token */
1643 };
1644
1645 /* Control command table */
1646 typedef struct CtrlDesc CtrlDesc;
1647 struct CtrlDesc {
1648     unsigned    Flags;                  /* Flags for this directive */
1649     void        (*Handler) (void);      /* Command handler */
1650 };
1651
1652 #define PSEUDO_COUNT    (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
1653 static CtrlDesc CtrlCmdTab [] = {
1654     { ccNone,           DoA16           },
1655     { ccNone,           DoA8            },
1656     { ccNone,           DoAddr          },      /* .ADDR */
1657     { ccNone,           DoAlign         },
1658     { ccNone,           DoASCIIZ        },
1659     { ccNone,           DoAssert        },
1660     { ccNone,           DoAutoImport    },
1661     { ccNone,           DoUnexpected    },      /* .BANKBYTE */
1662     { ccNone,           DoUnexpected    },      /* .BLANK */
1663     { ccNone,           DoBss           },
1664     { ccNone,           DoByte          },
1665     { ccNone,           DoCase          },
1666     { ccNone,           DoCharMap       },
1667     { ccNone,           DoCode          },
1668     { ccNone,           DoUnexpected,   },      /* .CONCAT */
1669     { ccNone,           DoConDes        },
1670     { ccNone,           DoUnexpected    },      /* .CONST */
1671     { ccNone,           DoConstructor   },
1672     { ccNone,           DoUnexpected    },      /* .CPU */
1673     { ccNone,           DoData          },
1674     { ccNone,           DoDbg,          },
1675     { ccNone,           DoDByt          },
1676     { ccNone,           DoDebugInfo     },
1677     { ccNone,           DoDefine        },
1678     { ccNone,           DoUnexpected    },      /* .DEFINED */
1679     { ccNone,           DoDestructor    },
1680     { ccNone,           DoDWord         },
1681     { ccKeepToken,      DoConditionals  },      /* .ELSE */
1682     { ccKeepToken,      DoConditionals  },      /* .ELSEIF */
1683     { ccKeepToken,      DoEnd           },
1684     { ccNone,           DoUnexpected    },      /* .ENDENUM */
1685     { ccKeepToken,      DoConditionals  },      /* .ENDIF */
1686     { ccNone,           DoUnexpected    },      /* .ENDMACRO */
1687     { ccNone,           DoEndProc       },
1688     { ccNone,           DoUnexpected    },      /* .ENDREPEAT */
1689     { ccNone,           DoEndScope      },
1690     { ccNone,           DoUnexpected    },      /* .ENDSTRUCT */
1691     { ccNone,           DoUnexpected    },      /* .ENDUNION */
1692     { ccNone,           DoEnum          },
1693     { ccNone,           DoError         },
1694     { ccNone,           DoExitMacro     },
1695     { ccNone,           DoExport        },
1696     { ccNone,           DoExportZP      },
1697     { ccNone,           DoFarAddr       },
1698     { ccNone,           DoFeature       },
1699     { ccNone,           DoFileOpt       },
1700     { ccNone,           DoForceImport   },
1701     { ccNone,           DoUnexpected    },      /* .FORCEWORD */
1702     { ccNone,           DoGlobal        },
1703     { ccNone,           DoGlobalZP      },
1704     { ccNone,           DoUnexpected    },      /* .HIBYTE */
1705     { ccNone,           DoUnexpected    },      /* .HIWORD */
1706     { ccNone,           DoI16           },
1707     { ccNone,           DoI8            },
1708     { ccNone,           DoUnexpected    },      /* .IDENT */
1709     { ccKeepToken,      DoConditionals  },      /* .IF */
1710     { ccKeepToken,      DoConditionals  },      /* .IFBLANK */
1711     { ccKeepToken,      DoConditionals  },      /* .IFCONST */
1712     { ccKeepToken,      DoConditionals  },      /* .IFDEF */
1713     { ccKeepToken,      DoConditionals  },      /* .IFNBLANK */
1714     { ccKeepToken,      DoConditionals  },      /* .IFNCONST */
1715     { ccKeepToken,      DoConditionals  },      /* .IFNDEF */
1716     { ccKeepToken,      DoConditionals  },      /* .IFNREF */
1717     { ccKeepToken,      DoConditionals  },      /* .IFP02 */
1718     { ccKeepToken,      DoConditionals  },      /* .IFP816 */
1719     { ccKeepToken,      DoConditionals  },      /* .IFPC02 */
1720     { ccKeepToken,      DoConditionals  },      /* .IFPSC02 */
1721     { ccKeepToken,      DoConditionals  },      /* .IFREF */
1722     { ccNone,           DoImport        },
1723     { ccNone,           DoImportZP      },
1724     { ccNone,           DoIncBin        },
1725     { ccNone,           DoInclude       },
1726     { ccNone,           DoInterruptor   },
1727     { ccNone,           DoInvalid       },      /* .LEFT */
1728     { ccNone,           DoLineCont      },
1729     { ccNone,           DoList          },
1730     { ccNone,           DoListBytes     },
1731     { ccNone,           DoUnexpected    },      /* .LOBYTE */
1732     { ccNone,           DoUnexpected    },      /* .LOCAL */
1733     { ccNone,           DoLocalChar     },
1734     { ccNone,           DoUnexpected    },      /* .LOWORD */
1735     { ccNone,           DoMacPack       },
1736     { ccNone,           DoMacro         },
1737     { ccNone,           DoUnexpected    },      /* .MATCH */
1738     { ccNone,           DoInvalid       },      /* .MID */
1739     { ccNone,           DoNull          },
1740     { ccNone,           DoOrg           },
1741     { ccNone,           DoOut           },
1742     { ccNone,           DoP02           },
1743     { ccNone,           DoP816          },
1744     { ccNone,           DoPageLength    },
1745     { ccNone,           DoUnexpected    },      /* .PARAMCOUNT */
1746     { ccNone,           DoPC02          },
1747     { ccNone,           DoPopSeg        },
1748     { ccNone,           DoProc          },
1749     { ccNone,           DoPSC02         },
1750     { ccNone,           DoPushSeg       },
1751     { ccNone,           DoUnexpected    },      /* .REFERENCED */
1752     { ccNone,           DoReloc         },
1753     { ccNone,           DoRepeat        },
1754     { ccNone,           DoRes           },
1755     { ccNone,           DoInvalid       },      /* .RIGHT */
1756     { ccNone,           DoROData        },
1757     { ccNone,           DoScope         },
1758     { ccNone,           DoSegment       },
1759     { ccNone,           DoUnexpected    },      /* .SET */
1760     { ccNone,           DoSetCPU        },
1761     { ccNone,           DoUnexpected    },      /* .SIZEOF */
1762     { ccNone,           DoSmart         },
1763     { ccNone,           DoUnexpected    },      /* .SPRINTF */
1764     { ccNone,           DoUnexpected    },      /* .STRAT */
1765     { ccNone,           DoUnexpected    },      /* .STRING */
1766     { ccNone,           DoUnexpected    },      /* .STRLEN */
1767     { ccNone,           DoStruct        },
1768     { ccNone,           DoSunPlus       },
1769     { ccNone,           DoTag           },
1770     { ccNone,           DoUnexpected    },      /* .TCOUNT */
1771     { ccNone,           DoUnexpected    },      /* .TIME */
1772     { ccNone,           DoUnion         },
1773     { ccNone,           DoUnexpected    },      /* .VERSION */
1774     { ccNone,           DoWarning       },
1775     { ccNone,           DoWord          },
1776     { ccNone,           DoUnexpected    },      /* .XMATCH */
1777     { ccNone,           DoZeropage      },
1778 };
1779
1780
1781
1782 /*****************************************************************************/
1783 /*                                   Code                                    */
1784 /*****************************************************************************/
1785
1786
1787
1788 void HandlePseudo (void)
1789 /* Handle a pseudo instruction */
1790 {
1791     CtrlDesc* D;
1792
1793     /* Calculate the index into the table */
1794     unsigned Index = Tok - TOK_FIRSTPSEUDO;
1795
1796     /* Safety check */
1797     if (PSEUDO_COUNT != (TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1)) {
1798         Internal ("Pseudo mismatch: PSEUDO_COUNT = %u, actual count = %u\n",
1799                   (unsigned) PSEUDO_COUNT, TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1);
1800     }
1801     CHECK (Index < PSEUDO_COUNT);
1802
1803     /* Get the pseudo intruction descriptor */
1804     D = &CtrlCmdTab [Index];
1805
1806     /* Remember the instruction, then skip it if needed */
1807     if ((D->Flags & ccKeepToken) == 0) {
1808         strcpy (Keyword, SVal);
1809         NextTok ();
1810     }
1811
1812     /* Call the handler */
1813     D->Handler ();
1814 }
1815
1816
1817
1818 void SegStackCheck (void)
1819 /* Check if the segment stack is empty at end of assembly */
1820 {
1821     if (CollCount (&SegStack) != 0) {
1822         Error ("Segment stack is not empty");
1823     }
1824 }
1825
1826
1827