summaryrefslogtreecommitdiff
path: root/misc/pascal/pascal/pgen.c
blob: 25d4d956b40f40bbd036e33e93dc7ba4457bbffe (plain) (blame)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/**********************************************************************
 * pgen.c
 * P-Code generation logic
 *
 *   Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 **********************************************************************/

/**********************************************************************
 * Included Files
 **********************************************************************/

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "config.h"   /* Configuration */
#include "keywords.h" /* Standard types */
#include "pasdefs.h"  /* Common types */
#include "ptdefs.h"   /* Token / symbol table definitions */
#include "podefs.h"   /* Logical opcode definitions */
#include "pedefs.h"   /* error code definitions */

#include "pas.h"      /* Global variables */
#include "poff.h"     /* For POFF file format */
#include "pofflib.h"  /* For poff*() functions*/
#include "pinsn.h"    /* (DEBUG only) */
#include "perr.h"     /* error() */

#include "pproc.h"    /* for actualParameterSize */
#include "pgen.h"     /* (to verify prototypes in this file) */

/**********************************************************************
 * Pre-processor Definitions
 **********************************************************************/

#define UNDEFINED_LEVEL (-1)
#define INVALID_PCODE   (-1)

#define LEVEL_DEFINED(l)  ((int32_t)(l) >= 0)
#define PCODE_VALID(p)    ((int32_t)(p) >= 0)

/**********************************************************************
 * Global Variables
 **********************************************************************/

/**********************************************************************
 * Private Variables
 **********************************************************************/

static int32_t  g_currentStackLevelReference  = UNDEFINED_LEVEL;
static uint32_t g_nStackLevelReferenceChanges = 0;

/***********************************************************************
 * Private Function Prototypes
 ***********************************************************************/

/***********************************************************************
 * Private Functions
 ***********************************************************************/

/***********************************************************************/
/* Generate a stack reference opcode to a global variable residing at
 * static nesting level zero.
 */

static void
pas_GenerateLevel0StackReference(enum pcode_e eOpCode, STYPE *pVar)
{
  /* Sanity checking.  Double check nesting level and also since this is
   * a level zero reference, then the offset must be positive
   */

  if ((pVar->sLevel != 0) || (pVar->sParm.v.offset < 0))
    {
      error(eHUH);
    }
  else
    {
      /* Generate the P-code */

      insn_GenerateDataOperation(eOpCode, pVar->sParm.v.offset);

      /* If the variable is undefined, also generate a relocation
       * record.
       */

      if ((pVar->sParm.v.flags & SVAR_EXTERNAL) != 0)
        {
          (void)poffAddRelocation(poffHandle, RLT_LDST,
                                  pVar->sParm.v.symIndex, 0);
        }
    }
}

/***********************************************************************/
/* There are some special P-codes for accessing stack data at static
 * nesting level 0.  Check if the specified opcode is one of those.  If
 * so, return the mapped opcode.  Otherwise, return INVALID_PCODE.
 */

static int32_t
pas_GetLevel0Opcode(enum pcode_e eOpCode)
{
  switch (eOpCode)
    {
    case opLDS:   return opLD;
    case opLDSH:  return opLDH;
    case opLDSB:  return opLDB;
    case opLDSM:  return opLDM;
    case opSTS:   return opST;
    case opSTSB:  return opSTB;
    case opSTSM:  return opSTM;
    case opLDSX:  return opLDX;
    case opLDSXB: return opLDXB;
    case opLDSXM: return opLDXM;
    case opSTSX:  return opSTX;
    case opSTSXB: return opSTXB;
    case opSTSXM: return opSTXM;
    case opLAS:   return opLA;
    case opLASX:  return opLAX;
    default:      return INVALID_PCODE;
    }
}

/***********************************************************************/
/* A new static nesting level has been encountered.  Check if we need
 * to reset the level stack pointer (LSP) register (assuming that the
 * architecutre has one).
 */

static void
pas_SetLevelStackPointer(uint32_t dwLevel)
{
  if (dwLevel != g_currentStackLevelReference)
    {
      /* Set the level stack pointer (LSP) register */

      insn_SetStackLevel(dwLevel);

      /* Remember the setting so that we do not reset the LSP until
       * the level changes (or it is invalidated).
       */

      g_currentStackLevelReference = dwLevel;
      g_nStackLevelReferenceChanges++;
    }
}

/***********************************************************************
 * Public Functions
 ***********************************************************************/

/***********************************************************************/
/* Return the current setting of the level stack pointer (LSP) register
 * -- assuming that the underlying architecure may have one.
 */

int32_t pas_GetCurrentStackLevel(void)
{
  return g_currentStackLevelReference;
}

/***********************************************************************/
/* Invalidate the current stack level register setting.  This will cause
 * us to reset the LSP when the next stack level reference is encountered.
 */

void pas_InvalidateCurrentStackLevel(void)
{
  g_currentStackLevelReference = UNDEFINED_LEVEL;
  g_nStackLevelReferenceChanges++;
}

/***********************************************************************/
/* Set the stack level pointer to known value.  This is done when in
 * while and for loop processing.  The value of the LSP will be that
 * as sampled at the top of the lop not necessarily the value at the
 * bottom of the loop.
 */

void pas_SetCurrentStackLevel(int32_t dwLsp)
{
  g_currentStackLevelReference = dwLsp;
  g_nStackLevelReferenceChanges++;
}

/***********************************************************************/
/* Get the number of changes made to the level stack pointer.  This is
 * useful by compiler logic to determine if the stack level pointer was
 * ever changed by any logic path.
 */

uint32_t pas_GetNStackLevelChanges(void)
{
  return g_nStackLevelReferenceChanges;
}

/***********************************************************************/
/* Generate the most simple of all P-codes */

void pas_GenerateSimple(enum pcode_e eOpCode)
{
  insn_GenerateSimple(eOpCode);
}

/***********************************************************************/
/* Generate a P-code with a single data argument */

void pas_GenerateDataOperation(enum pcode_e eOpCode, int32_t dwData)
{
  insn_GenerateDataOperation(eOpCode, dwData);
}

/***********************************************************************/
/* This function is called just before a multiple register operation is
 * is generated.  This should generate logic to specify the size of the
 * multiple register operation (in bytes, not registers). This may translate
 * into different operations on different architectures.  Typically,
 * this would generate a push of the size onto the stack or, perhaps,
 * setting of a dedicated count register.
 */

void pas_GenerateDataSize(int32_t dwDataSize)
{
  insn_GenerateDataSize(dwDataSize);
}

/***********************************************************************/
/* Generate a floating point operation */

void pas_GenerateFpOperation(uint8_t fpOpcode)
{
  insn_GenerateFpOperation(fpOpcode);
}

/***********************************************************************/
/* Generate an IO operation */

void pas_GenerateIoOperation(uint16_t ioOpcode, uint16_t fileNumber)
{
  insn_GenerateIoOperation(ioOpcode, fileNumber);
}

/***********************************************************************/
/* Generate a pseudo call to a built-in, standard pascal function */

void pas_BuiltInFunctionCall(uint16_t libOpcode)
{
  insn_BuiltInFunctionCall(libOpcode);
}

/***********************************************************************/
/* Generate a reference to data on the data stack using the specified
 * level and offset.
 */

void pas_GenerateLevelReference(enum pcode_e eOpCode, uint16_t wLevel,
                                int32_t dwOffset)
{
  /* Is this variable declared at level 0 (i.e., it has global scope)
   * that is being offset via a nesting level?
   */

  if (wLevel == 0)
    {
      int32_t level0Opcode = pas_GetLevel0Opcode(eOpCode);
      if (PCODE_VALID(level0Opcode))
        {
          insn_GenerateDataOperation(level0Opcode, dwOffset);
          return;
        }
    }

  /* We get here if the reference is at some static nesting level
   * other that zero OR if there is no special PCode to reference
   * data at static nesting level 0 for this operation.
   *
   * Check if we have to change the level stack pointer (LSP) register
   * (assuming that the architecture has one).
   */

  pas_SetLevelStackPointer(wLevel);

  /* Then generate the opcode passing the level in the event that the
   * architecture does not have an LSP.
   */

  insn_GenerateLevelReference(eOpCode, wLevel, dwOffset);
}

/***********************************************************************/
/* Generate a stack reference opcode, handling references to undefined
 * stack offsets.
 */

void pas_GenerateStackReference(enum pcode_e eOpCode, STYPE *pVar)
{
  /* Is this variable declared at level 0 (i.e., it has global scope)
   * that is being offset via a nesting level?
   */

  if (pVar->sLevel == 0)
    {
      int32_t level0Opcode = pas_GetLevel0Opcode(eOpCode);
      if (PCODE_VALID(level0Opcode))
        {
          pas_GenerateLevel0StackReference(level0Opcode, pVar);
          return;
        }
    }

  /* We get here if the reference is at some static nesting level
   * other that zero OR if there is no special PCode to reference
   * data at static nesting level 0 for this operation.
   *
   * Check if we have to change the level stack pointer (LSP) register
   * (assuming that the architecture has one).
   */

  pas_SetLevelStackPointer(pVar->sLevel);

  /* Generate the P-Code at the defined offset and with the specified
   * static level offset (in case that the architecture does not have
   * an LSP)
   */

  insn_GenerateLevelReference(eOpCode, (level - pVar->sLevel),
                              pVar->sParm.v.offset);
}

/***********************************************************************/
/* Generate a procedure call and an associated relocation record if the
 * called procedure is external.
 */

void
pas_GenerateProcedureCall(STYPE *pProc)
{
  /* sLevel is the level at which the procedure was declared.  We need
   * to set the SLP to this value prior to the call (on some architectures
   * where the SLP is pushed onto the stack by the procedure
   * call).
   */

  pas_SetLevelStackPointer(pProc->sLevel);

  /* Then generate the procedure call (passing the level again for those
   * architectures that do not support the SLP.
   */

  insn_GenerateProcedureCall(pProc->sLevel, pProc->sParm.p.label);

  /* If the variable is undefined, also generate a relocation
   * record.
   */

#if 0 /* Not yet */
  if ((pVar->sParm.p.flags & SVAR_EXTERNAL) != 0)
    {
      /* For now */
# error "Don't know what last parameter should be"
      (void)poffAddRelocation(poffHandle, RLT_PCAL,
                              pVar->sParm.p.symIndex,
                              0);
    }
#endif

  /* Any logic after the procedure/function call return must assume
   * that the last level reference is unknown.
   */

  pas_InvalidateCurrentStackLevel();
}

/***********************************************************************/

void pas_GenerateLineNumber(uint16_t wIncludeNumber, uint32_t dwLineNumber)
{
  insn_GenerateLineNumber(wIncludeNumber, dwLineNumber);
}

/***********************************************************************/

void pas_GenerateDebugInfo(STYPE *pProc, uint32_t dwReturnSize)
{
  int i;

  /* Allocate a container to pass the proc information to the library */

  uint32_t nparms                      = pProc->sParm.p.nParms;
  poffLibDebugFuncInfo_t *pContainer = poffCreateDebugInfoContainer(nparms);

  /* Put the proc information into the container */

  pContainer->value   = pProc->sParm.p.label;
  pContainer->retsize = dwReturnSize;
  pContainer->nparms  = nparms;

  /* Add the argument information to the container */

   for (i = 0; i < nparms; i++)
     {
       pContainer->argsize[i] = actualParameterSize(pProc, i+1);
     }

   /* Add the contained information to the library */

   poffAddDebugFuncInfo(poffHandle, pContainer);

   /* Release the container */

   poffReleaseDebugFuncContainer(pContainer);
}

/***********************************************************************/
/* Generate description of a level 0 stack variable that can be
 * exported by a unit.
 */

void pas_GenerateStackExport(STYPE *pVar)
{
  poffLibSymbol_t symbol;

#if CONFIG_DEBUG
  /* Get the parent type of the variable */

  STYPE *typePtr = pVar->sParm.v.parent;

  /* Perform some sanity checking:
   * - Must have a parent type
   * - Must not be declared external
   * - Must be declared at static nesting level zero
   */

  if ((!typePtr) ||
      ((pVar->sParm.v.flags & SVAR_EXTERNAL) != 0) ||
      (pVar->sLevel != 0))
    {
      error(eSYMTABINTERNAL);
    }
#endif

  /* Create the symbol structure */

  symbol.type  = STT_DATA;
  symbol.align = STA_8BIT; /* for now */
  symbol.flags = STF_NONE;
  symbol.name  = pVar->sName;
  symbol.value = pVar->sParm.v.offset;
  symbol.size  = pVar->sParm.v.size;

  /* Add the symbol to the symbol table */

  (void)poffAddSymbol(poffHandle, &symbol);
}

/***********************************************************************/
/* Generate description of a level 0 stack variable that must be
 * imported by a program or unit from a unit.
 */

void pas_GenerateStackImport(STYPE *pVar)
{
  poffLibSymbol_t symbol;

#if CONFIG_DEBUG
  /* Get the parent type of the variable */

  STYPE *typePtr = pVar->sParm.v.parent;

  /* Perform some sanity checking
   * - Must have a parent type
   * - Must be declared external
   * - Must be declared at static nesting level zero
   */

  if ((!typePtr) ||
      ((pVar->sParm.v.flags & SVAR_EXTERNAL) == 0) ||
      (pVar->sLevel != 0))
    {
      error(eSYMTABINTERNAL);
    }
#endif

  /* Create the symbol structure */

  symbol.type  = STT_DATA;
  symbol.align = STA_8BIT; /* for now */
  symbol.flags = STF_UNDEFINED;
  symbol.name  = pVar->sName;
  symbol.value = pVar->sParm.v.offset; /* for now */
  symbol.size  = pVar->sParm.v.size;

  /* Add the symbol to the symbol table */

  pVar->sParm.v.symIndex = poffAddSymbol(poffHandle, &symbol);
}

/***********************************************************************/
/* Generate description of a level 0 procedure or function that can be
 * exported by a unit.
 */

void pas_GenerateProcExport(STYPE *pProc)
{
  poffLibSymbol_t symbol;

#if CONFIG_DEBUG
  /* Get the parent type of the function (assuming it is a function) */

  STYPE *typePtr = pProc->sParm.p.parent;

  /* Perform some sanity checking */

  /* Check for a function reference which must have a valid parent type */

  if ((pProc->sKind == sFUNC) && (typePtr != NULL));

  /* Check for a procedure reference which must not have a valid type */

  else  if ((pProc->sKind == sPROC) && (typePtr == NULL));

  /* Anything else is an error */

  else
    error(eSYMTABINTERNAL);

  /* The function / procedure should NOT be declared external and
   * only procedures declared at static nesting level zero can
   * be exported.
   */

  if (((pProc->sParm.p.flags & SPROC_EXTERNAL) != 0) ||
      (pProc->sLevel != 0))
    error(eSYMTABINTERNAL);
#endif

  /* Everthing looks okay.  Create the symbol structure */

  if (pProc->sKind == sPROC)
    symbol.type  = STT_PROC;
  else
    symbol.type  = STT_FUNC;

  symbol.align = STA_NONE;
  symbol.flags = STF_NONE;
  symbol.name  = pProc->sName;
  symbol.value = pProc->sParm.p.label;
  symbol.size  = 0;

  /* Add the symbol to the symbol table */

  (void)poffAddSymbol(poffHandle, &symbol);
}

/***********************************************************************/
/* Generate description of a level 0 procedure or function that must be
 * imported by a program or unit from a unit.
 */

void pas_GenerateProcImport(STYPE *pProc)
{
  poffLibSymbol_t symbol;

#if CONFIG_DEBUG
  /* Get the parent type of the function (assuming it is a function) */

  STYPE *typePtr = pProc->sParm.p.parent;

  /* Perform some sanity checking */

  /* Check for a function reference which must have a valid parent type */

  if ((pProc->sKind == sFUNC) && (typePtr != NULL));

  /* Check for a procedure reference which must not have a valid type */

  else  if ((pProc->sKind == sPROC) && (typePtr == NULL));

  /* Anything else is an error */

  else
    error(eSYMTABINTERNAL);

  /* The function / procedure should also be declared external and
   * only procedures declared at static nesting level zero can
   * be exported.
   */

  if (((pProc->sParm.p.flags & SPROC_EXTERNAL) == 0) ||
      (pProc->sLevel != 0))
    error(eSYMTABINTERNAL);
#endif

  /* Everthing looks okay.  Create the symbol structure */

  if (pProc->sKind == sPROC)
    symbol.type  = STT_PROC;
  else
    symbol.type  = STT_FUNC;

  symbol.align = STA_NONE;
  symbol.flags = STF_UNDEFINED;
  symbol.name  = pProc->sName;
  symbol.value = pProc->sParm.p.label;
  symbol.size  = 0;

  /* Add the symbol to the symbol table */

  pProc->sParm.p.symIndex = poffAddSymbol(poffHandle, &symbol);
}