1 /*****************************************************************************/
5 /* Goto and label handling for the cc65 C compiler */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 void GotoStatement (void)
59 /* Process a goto statement. */
64 /* Label name must follow */
65 if (CurTok.Tok == TOK_IDENT) {
67 /* Add a new label symbol if we don't have one until now */
68 SymEntry* Entry = AddLabelSym (CurTok.Ident, SC_REF | SC_GOTO);
70 /* Jump to the label */
71 g_jump (Entry->V.L.Label);
73 /* Eat the label name */
76 } else if (CurTok.Tok == TOK_STAR && IS_Get (&Standard) >= STD_CC65) {
77 SymEntry *arr, *idx, *cur;
86 /* arr[foo], we only support simple foo for now */
87 if (CurTok.Tok == TOK_IDENT &&
88 (arr = FindSym (CurTok.Ident))) {
92 if (!IsTypeArray (arr->Type) || SizeOf (arr->Type) == 0 ||
93 !(arr->Flags & SC_STATIC) ||
94 SizeOf (GetElementType(arr->Type)) != 2) {
95 Error ("Expected a static array");
96 } else if (GetElementCount (arr->Type) > 127) {
97 Error ("Only arrays with <= 127 labels are supported, got %lu",
98 GetElementCount (arr->Type));
103 if (CurTok.Tok == TOK_ICONST) {
107 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
108 AddCodeLine ("ldx #$%02X", val * 2);
109 AddCodeLine ("jmp (.loword(%s),x)", arr->AsmName);
111 AddCodeLine ("ldy #$%02X", val * 2);
112 AddCodeLine ("lda %s,y", arr->AsmName);
113 AddCodeLine ("ldx %s+1,y", arr->AsmName);
114 AddCodeLine ("jmp callax");
116 } else if (CurTok.Tok == TOK_IDENT &&
117 (idx = FindSym (CurTok.Ident))) {
119 LoadExpr (CF_NONE, &desc);
120 AddCodeLine ("asl a");
122 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
124 AddCodeLine ("jmp (.loword(%s),x)", arr->AsmName);
127 AddCodeLine ("lda %s,y", arr->AsmName);
128 AddCodeLine ("ldx %s+1,y", arr->AsmName);
129 AddCodeLine ("jmp callax");
132 Error ("Only simple expressions are supported for computed goto");
137 /* Loop over all target labels, specifying this as a jump point.
138 ** It's not exact - if there's multiple gotos, the last will be used,
139 ** but it's only needed so the optimizer does not remove the labels.
141 I = CS_GetEntryCount (CS->Code) - 1;
142 E = CS_GetEntry (CS->Code, I);
144 tab = GetLabelSymTab ();
148 if ((cur->Flags & (SC_LABEL|SC_GOTO_IND)) == (SC_LABEL|SC_GOTO_IND)) {
149 cur->V.L.IndJumpFrom = E;
154 } else { /* It was not TOK_IDENT, or we couldn't find the symbol */
155 Error ("Array name expected");
159 Error ("Label name expected");
166 /* Define a label. */
168 /* Add a label symbol */
169 SymEntry* Entry = AddLabelSym (CurTok.Ident, SC_DEF);
171 /* Emit the jump label */
172 CodeLabel* L = CS_AddLabel (CS->Code, LocalLabelName (Entry->V.L.Label));
173 if (Entry->V.L.IndJumpFrom) {
174 CollAppend (&L->JumpFrom, Entry->V.L.IndJumpFrom);
177 /* Eat the ident and colon */