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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
|
/*-------------------------------------------------------------------------
SDCCmem.c - 8051 memory management routines
Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding!
-------------------------------------------------------------------------*/
#include "common.h"
#include "dbuf_string.h"
#include "SDCCbtree.h"
/* memory segments */
memmap *xstack = NULL; /* xternal stack data */
memmap *istack = NULL; /* internal stack */
memmap *code = NULL; /* code segment */
memmap *data = NULL; /* internal data upto 128 */
memmap *initialized = NULL; /* initialized data, such as initialized, nonzero globals or local statics. */
memmap *initializer = NULL; /* a copz of the values for the initialized data from initialized in code space */
memmap *pdata = NULL; /* paged external data */
memmap *xdata = NULL; /* external data */
memmap *xidata = NULL; /* the initialized xdata */
memmap *xinit = NULL; /* the initializers for xidata */
memmap *idata = NULL; /* internal data upto 256 */
memmap *bit = NULL; /* bit addressable space */
memmap *statsg = NULL; /* the constant data segment */
memmap *c_abs = NULL; /* constant absolute data */
memmap *x_abs = NULL; /* absolute xdata/pdata */
memmap *i_abs = NULL; /* absolute idata upto 256 */
memmap *d_abs = NULL; /* absolute data upto 128 */
memmap *sfr = NULL; /* register space */
memmap *reg = NULL; /* register space */
memmap *sfrbit = NULL; /* sfr bit space */
memmap *generic = NULL; /* is a generic pointer */
memmap *overlay = NULL; /* overlay segment */
memmap *eeprom = NULL; /* eeprom location */
memmap *home = NULL; /* Unswitchable code bank */
namedspacemap *namedspacemaps = 0; /* memory segments for named address spaces */
/* this is a set of sets each set containing
symbols in a single overlay */
set *ovrSetSets = NULL;
int fatalError = 0; /* fatal error flag */
/*-----------------------------------------------------------------*/
/* allocMap - allocates a memory map */
/*-----------------------------------------------------------------*/
memmap *
allocMap (char rspace, /* sfr space */
char farmap, /* far or near segment */
char paged, /* can this segment be paged */
char direct, /* directly addressable */
char bitaddr, /* bit addressable space */
char codemap, /* this is code space */
unsigned sloc, /* starting location */
const char *name, /* 8 character name */
char dbName, /* debug name */
int ptrType /* pointer type for this space */
)
{
memmap *map;
if (!name)
return NULL;
if (!(map = Safe_alloc (sizeof (memmap))))
{
werror (E_OUT_OF_MEM, __FILE__, sizeof (memmap));
exit (1);
}
memset (map, ZERO, sizeof (memmap));
map->regsp = rspace;
map->fmap = farmap;
map->paged = paged;
map->direct = direct;
map->bitsp = bitaddr;
map->codesp = codemap;
map->sloc = sloc;
map->sname = name;
map->dbName = dbName;
map->ptrType = ptrType;
map->syms = NULL;
dbuf_init(&map->oBuf, 4096);
return map;
}
/*-----------------------------------------------------------------*/
/* initMem - allocates and initializes all the segments */
/*-----------------------------------------------------------------*/
void
initMem ()
{
/* allocate all the segments */
/* xternal stack segment ;
SFRSPACE - NO
FAR-SPACE - YES
PAGED - YES
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'A'
POINTER-TYPE - FPOINTER
*/
xstack = allocMap (0, 1, 1, 0, 0, 0, options.xstack_loc, XSTACK_NAME, 'A', PPOINTER);
/* internal stack segment ;
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'B'
POINTER-TYPE - POINTER
*/
istack = allocMap (0, 0, 0, 0, 0, 0, options.stack_loc, ISTACK_NAME, 'B', POINTER);
/* code segment ;
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - YES
DEBUG-NAME - 'C'
POINTER-TYPE - CPOINTER
*/
code = allocMap (0, 1, 0, 0, 0, 1, options.code_loc, CODE_NAME, 'C', CPOINTER);
/* home segment ;
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - YES
DEBUG-NAME - 'C'
POINTER-TYPE - CPOINTER
*/
home = allocMap (0, 1, 0, 0, 0, 1, options.code_loc, HOME_NAME, 'C', CPOINTER);
/* Static segment (code for variables );
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - YES
DEBUG-NAME - 'D'
POINTER-TYPE - CPOINTER
*/
statsg = allocMap (0, 1, 0, 0, 0, 1, 0, STATIC_NAME, 'D', CPOINTER);
/* Constant Absolute Data segment (for variables );
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - YES
DEBUG-NAME - 'D'
POINTER-TYPE - CPOINTER
*/
c_abs = allocMap (0, 1, 0, 0, 0, 1, 0, CABS_NAME, 'D', CPOINTER);
/* Data segment - internal storage segment ;
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - YES
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'E'
POINTER-TYPE - POINTER
*/
data = allocMap (0, 0, 0, 1, 0, 0, options.data_loc, DATA_NAME, 'E', POINTER);
initialized = allocMap (0, 0, 0, 1, 0, 0, options.data_loc, INITIALIZED_NAME, 'E', POINTER);
initializer = allocMap (0, 0, 0, 1, 0, 1, options.code_loc, INITIALIZER_NAME, 'C', CPOINTER);
/* Absolute internal storage segment ;
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - YES
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'E'
POINTER-TYPE - POINTER
*/
d_abs = allocMap (0, 0, 0, 1, 0, 0, options.data_loc, IABS_NAME, 'E', POINTER);
/* overlay segment - same as internal storage segment ;
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - YES
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'E'
POINTER-TYPE - POINTER
*/
if (OVERLAY_NAME)
overlay = allocMap (0, 0, 0, 1, 0, 0, options.data_loc, DATA_NAME, 'E', POINTER);
/* Xternal paged segment ;
SFRSPACE - NO
FAR-SPACE - NO
PAGED - YES
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'P'
POINTER-TYPE - PPOINTER
*/
pdata = allocMap (0, 0, 1, 0, 0, 0, options.xstack_loc, PDATA_NAME, 'P', PPOINTER);
/* Xternal Data segment -
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'F'
POINTER-TYPE - FPOINTER
*/
xdata = allocMap (0, 1, 0, 0, 0, 0, options.xdata_loc, XDATA_NAME, 'F', FPOINTER);
xidata = allocMap (0, 1, 0, 0, 0, 0, 0, XIDATA_NAME, 'F', FPOINTER);
xinit = allocMap (0, 1, 0, 0, 0, 1, 0, XINIT_NAME, 'C', CPOINTER);
/* Absolute external storage segment ;
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'F'
POINTER-TYPE - FPOINTER
*/
x_abs = allocMap (0, 1, 0, 0, 0, 0, options.xdata_loc, XABS_NAME, 'F', FPOINTER);
/* Indirectly addressed internal data segment
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'G'
POINTER-TYPE - IPOINTER
*/
idata = allocMap (0, 0, 0, 0, 0, 0, options.idata_loc, IDATA_NAME, 'G', IPOINTER);
/* Indirectly addressed absolute internal segment
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'E'
POINTER-TYPE - IPOINTER
*/
i_abs = allocMap (0, 0, 0, 0, 0, 0, options.data_loc, IABS_NAME, 'E', IPOINTER);
/* Bit space ;
SFRSPACE - NO
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - YES
BIT-ACCESS - YES
CODE-ACCESS - NO
DEBUG-NAME - 'H'
POINTER-TYPE - _NONE_
*/
bit = allocMap (0, 0, 0, 1, 1, 0, 0, BIT_NAME, 'H', 0);
/* Special function register space :-
SFRSPACE - YES
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - YES
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'I'
POINTER-TYPE - _NONE_
*/
sfr = allocMap (1, 0, 0, 1, 0, 0, 0, REG_NAME, 'I', 0);
/* Register space ;
SFRSPACE - YES
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - ' '
POINTER-TYPE - _NONE_
*/
reg = allocMap (1, 0, 0, 0, 0, 0, 0, REG_NAME, ' ', 0);
/* SFR bit space
SFRSPACE - YES
FAR-SPACE - NO
PAGED - NO
DIRECT-ACCESS - YES
BIT-ACCESS - YES
CODE-ACCESS - NO
DEBUG-NAME - 'J'
POINTER-TYPE - _NONE_
*/
sfrbit = allocMap (1, 0, 0, 1, 1, 0, 0, REG_NAME, 'J', 0);
/* EEPROM space
SFRSPACE - NO
FAR-SPACE - YES
PAGED - NO
DIRECT-ACCESS - NO
BIT-ACCESS - NO
CODE-ACCESS - NO
DEBUG-NAME - 'K'
POINTER-TYPE - EEPPOINTER
*/
eeprom = allocMap (0, 1, 0, 0, 0, 0, 0, REG_NAME, 'K', EEPPOINTER);
/* the unknown map */
generic = allocMap (1, 0, 0, 1, 1, 0, 0, REG_NAME, ' ', GPOINTER);
}
/*-----------------------------------------------------------------*/
/* allocIntoSeg - puts a symbol into a memory segment */
/*-----------------------------------------------------------------*/
void
allocIntoSeg (symbol *sym)
{
memmap *segment;
if (SPEC_ADDRSPACE (sym->etype))
{
namedspacemap *nm;
for (nm = namedspacemaps; nm; nm = nm->next)
if (!strcmp (nm->name, SPEC_ADDRSPACE (sym->etype)->name))
break;
if (!nm)
{
nm = Safe_alloc (sizeof (namedspacemap));
nm->name = Safe_alloc (strlen(SPEC_ADDRSPACE (sym->etype)->name) + 1);
strcpy (nm->name, SPEC_ADDRSPACE (sym->etype)->name);
nm->is_const = (SPEC_ADDRSPACE (sym->etype)->type && SPEC_CONST (SPEC_ADDRSPACE (sym->etype)->type));
nm->map = nm->is_const ?
allocMap (0, 1, 0, 0, 0, 1, options.code_loc, SPEC_ADDRSPACE (sym->etype)->name, 'C', CPOINTER) :
allocMap (0, 0, 0, 1, 0, 0, options.data_loc, SPEC_ADDRSPACE (sym->etype)->name, 'E', POINTER);
nm->next = namedspacemaps;
namedspacemaps = nm;
}
addSet (&nm->map->syms, sym);
return;
}
segment = SPEC_OCLS (sym->etype);
addSet (&segment->syms, sym);
if (segment == pdata)
sym->iaccess = 1;
}
/*-----------------------------------------------------------------*/
/* deleteFromSeg - deletes a symbol from segment used when a var */
/* first declared as "extern" then no extern */
/*-----------------------------------------------------------------*/
void deleteFromSeg(symbol *sym)
{
if (SPEC_OCLS(sym->etype))
{
memmap *segment = SPEC_OCLS (sym->etype);
deleteSetItem(&segment->syms, sym);
}
}
/*-----------------------------------------------------------------*/
/* defaultOClass - set the output segment based on SCLASS */
/*-----------------------------------------------------------------*/
bool
defaultOClass (symbol *sym)
{
switch (SPEC_SCLS (sym->etype))
{
case S_SFR:
SPEC_OCLS (sym->etype) = sfr;
break;
case S_SBIT:
SPEC_OCLS (sym->etype) = sfrbit;
break;
case S_CODE:
if (sym->_isparm)
return FALSE;
/* if code change to constant */
if (sym->ival && SPEC_ABSA (sym->etype))
{
SPEC_OCLS(sym->etype) = c_abs;
}
else
{
SPEC_OCLS (sym->etype) = statsg;
}
break;
case S_XDATA:
/* absolute initialized global */
if (sym->ival && SPEC_ABSA (sym->etype))
{
SPEC_OCLS(sym->etype) = x_abs;
}
/* or should we move this to the initialized data segment? */
else if (port->genXINIT && sym->ival && (sym->level==0))
{
SPEC_OCLS(sym->etype) = xidata;
}
else
{
SPEC_OCLS (sym->etype) = xdata;
}
break;
case S_DATA:
/* Absolute initialized global */
if (sym->ival && SPEC_ABSA (sym->etype))
{
SPEC_OCLS (sym->etype) = d_abs;
}
/* Other initialized global */
else if (sym->ival && port->mem.initialized_name && sym->level == 0)
{
SPEC_OCLS (sym->etype) = initialized;
}
else
{
SPEC_OCLS (sym->etype) = data;
}
break;
case S_IDATA:
/* absolute initialized global */
if (sym->ival && SPEC_ABSA (sym->etype))
{
SPEC_OCLS(sym->etype) = i_abs;
}
else
{
SPEC_OCLS (sym->etype) = idata;
}
sym->iaccess = 1;
break;
case S_PDATA:
SPEC_OCLS (sym->etype) = pdata;
sym->iaccess = 1;
break;
case S_BIT:
SPEC_OCLS (sym->etype) = bit;
break;
case S_EEPROM:
SPEC_OCLS (sym->etype) = eeprom;
break;
default:
return FALSE;
}
return TRUE;
}
/*-----------------------------------------------------------------*/
/* allocDefault - assigns the output segment based on SCLASS */
/*-----------------------------------------------------------------*/
bool
allocDefault (struct symbol * sym)
{
if (defaultOClass (sym))
{
allocIntoSeg (sym);
return TRUE;
}
return FALSE;
}
/*-----------------------------------------------------------------*/
/* allocGlobal - assigns the output segment to a global var */
/*-----------------------------------------------------------------*/
void
allocGlobal (symbol * sym)
{
/* symbol name is internal name */
if (!sym->level) /* local statics can come here */
SNPRINTF (sym->rname, sizeof(sym->rname),
"%s%s", port->fun_prefix, sym->name);
/* add it to the operandKey reset */
if (!isinSet (operKeyReset, sym))
{
addSet(&operKeyReset, sym);
}
/* if this is a literal e.g. enumerated type */
/* put it in the data segment & do nothing */
if (IS_LITERAL (sym->etype))
{
SPEC_OCLS (sym->etype) = data;
return;
}
/* if this is a function then assign code space */
if (IS_FUNC (sym->type))
{
SPEC_OCLS (sym->etype) = code;
/* if this is an interrupt service routine
then put it in the interrupt service array */
if (FUNC_ISISR (sym->type) && !options.noiv &&
(FUNC_INTNO (sym->type) != INTNO_UNSPEC))
{
if (interrupts[FUNC_INTNO (sym->type)])
werror (E_INT_DEFINED,
FUNC_INTNO (sym->type),
interrupts[FUNC_INTNO (sym->type)]->name);
else
interrupts[FUNC_INTNO (sym->type)] = sym;
/* automagically extend the maximum interrupts */
if (FUNC_INTNO (sym->type) >= maxInterrupts && FUNC_INTNO (sym->type)!=INTNO_TRAP)
maxInterrupts = FUNC_INTNO (sym->type) + 1;
}
/* if it is not compiler defined */
if (!sym->cdef)
allocIntoSeg (sym);
return;
}
/* if this is a bit variable and no storage class */
if (bit && IS_SPEC(sym->type) && SPEC_NOUN (sym->type) == V_BIT)
{
SPEC_OCLS (sym->type) = bit;
allocIntoSeg (sym);
return;
}
if (!TARGET_IS_PIC16 || sym->level)
/* register storage class ignored changed to FIXED */
if (SPEC_SCLS (sym->etype) == S_REGISTER)
SPEC_SCLS (sym->etype) = S_FIXED;
/* if it is fixed, then allocate depending on the */
/* current memory model, same for automatics */
if (SPEC_SCLS (sym->etype) == S_FIXED ||
(TARGET_IS_PIC16 && (SPEC_SCLS (sym->etype) == S_REGISTER) && (sym->level == 0)) ||
SPEC_SCLS (sym->etype) == S_AUTO)
{
if (port->mem.default_globl_map != xdata)
{
if (sym->ival && SPEC_ABSA (sym->etype))
{
/* absolute initialized global */
SPEC_OCLS (sym->etype) = x_abs;
}
else if (sym->ival && sym->level == 0 && port->mem.initialized_name)
{
SPEC_OCLS (sym->etype) = initialized;
}
else
{
/* set the output class */
SPEC_OCLS (sym->etype) = port->mem.default_globl_map;
}
/* generate the symbol */
allocIntoSeg (sym);
return;
}
else
{
SPEC_SCLS (sym->etype) = S_XDATA;
}
}
allocDefault (sym);
return;
}
/*-----------------------------------------------------------------*/
/* allocParms - parameters are always passed on stack */
/*-----------------------------------------------------------------*/
void
allocParms (value * val)
{
value *lval;
int pNum = 1;
for (lval = val; lval; lval = lval->next, pNum++)
{
/* check the declaration */
checkDecl (lval->sym, 0);
/* if this a register parm then allocate
it as a local variable by adding it
to the first block we see in the body */
if (IS_REGPARM (lval->etype))
continue;
/* mark it as my parameter */
lval->sym->ismyparm = 1;
lval->sym->localof = currFunc;
/* if automatic variables r 2b stacked */
if (options.stackAuto || IFFUNC_ISREENT (currFunc->type))
{
if (lval->sym)
lval->sym->onStack = 1;
/* choose which stack 2 use */
/* use xternal stack */
if (options.useXstack)
{
/* PENDING: stack direction support */
SPEC_OCLS (lval->etype) = SPEC_OCLS (lval->sym->etype) = xstack;
SPEC_STAK (lval->etype) = SPEC_STAK (lval->sym->etype) = lval->sym->stack =
xstackPtr - getSize (lval->type);
xstackPtr -= getSize (lval->type);
}
else
{ /* use internal stack */
SPEC_OCLS (lval->etype) = SPEC_OCLS (lval->sym->etype) = istack;
if (port->stack.direction > 0)
{
SPEC_STAK (lval->etype) = SPEC_STAK (lval->sym->etype) = lval->sym->stack =
stackPtr - (FUNC_REGBANK (currFunc->type) ? port->stack.bank_overhead : 0) -
getSize (lval->type) -
(FUNC_ISISR (currFunc->type) ? port->stack.isr_overhead : 0);
stackPtr -= getSize (lval->type);
}
else
{
/* This looks like the wrong order but it turns out OK... */
/* PENDING: isr, bank overhead, ... */
SPEC_STAK (lval->etype) = SPEC_STAK (lval->sym->etype) = lval->sym->stack =
stackPtr +
(FUNC_ISISR (currFunc->type) ? port->stack.isr_overhead : 0) +
0;
stackPtr += getSize (lval->type);
}
}
allocIntoSeg (lval->sym);
}
else
{ /* allocate them in the automatic space */
/* generate a unique name */
SNPRINTF (lval->sym->rname, sizeof(lval->sym->rname),
"%s%s_PARM_%d", port->fun_prefix, currFunc->name, pNum);
strncpyz (lval->name, lval->sym->rname, sizeof(lval->name));
/* if declared in specific storage */
if (allocDefault (lval->sym))
{
SPEC_OCLS (lval->etype) = SPEC_OCLS (lval->sym->etype);
continue;
}
/* otherwise depending on the memory model */
SPEC_OCLS (lval->etype) = SPEC_OCLS (lval->sym->etype) =
port->mem.default_local_map;
if (options.model == MODEL_SMALL)
{
/* note here that we put it into the overlay segment
first, we will remove it from the overlay segment
after the overlay determination has been done */
if (!options.noOverlay)
{
SPEC_OCLS (lval->etype) = SPEC_OCLS (lval->sym->etype) =
overlay;
}
}
else if (options.model == MODEL_MEDIUM)
{
SPEC_SCLS (lval->etype) = S_PDATA;
}
else
{
SPEC_SCLS (lval->etype) = S_XDATA;
}
allocIntoSeg (lval->sym);
}
}
return;
}
/*-----------------------------------------------------------------*/
/* deallocParms - parameters are always passed on stack */
/*-----------------------------------------------------------------*/
void
deallocParms (value * val)
{
value *lval;
for (lval = val; lval; lval = lval->next)
{
/* unmark is myparm */
lval->sym->ismyparm = 0;
/* delete it from the symbol table */
deleteSym (SymbolTab, lval->sym, lval->sym->name);
if (!lval->sym->isref)
{
lval->sym->allocreq = 0;
werror (W_NO_REFERENCE,
currFunc ? currFunc->name : "(unknown)",
"function argument", lval->sym->name);
}
/* move the rname if any to the name for both val & sym */
/* and leave a copy of it in the symbol table */
if (lval->sym->rname[0])
{
char buffer[SDCC_NAME_MAX];
symbol * argsym = lval->sym;
strncpyz (buffer, lval->sym->rname, sizeof(buffer));
lval->sym = copySymbol (lval->sym);
strncpyz (lval->sym->rname, buffer, sizeof(lval->sym->rname));
strncpyz (lval->sym->name, buffer, sizeof(lval->sym->name));
/* need to keep the original name for inlining to work */
/*strncpyz (lval->name, buffer, sizeof(lval->name)); */
addSym (SymbolTab, lval->sym, lval->sym->name,
lval->sym->level, lval->sym->block, 1);
lval->sym->_isparm = 1;
if (!isinSet (operKeyReset, lval->sym))
{
addSet(&operKeyReset, lval->sym);
}
/* restore the original symbol */
lval->sym = argsym;
}
}
return;
}
/*-----------------------------------------------------------------*/
/* allocLocal - allocate local variables */
/*-----------------------------------------------------------------*/
void
allocLocal (symbol * sym)
{
/* generate an unique name */
SNPRINTF (sym->rname, sizeof(sym->rname),
"%s%s_%s_%d_%d",
port->fun_prefix,
currFunc->name, sym->name, sym->level, sym->block);
sym->islocal = 1;
sym->localof = currFunc;
/* if this is a static variable */
if (IS_STATIC (sym->etype))
{
allocGlobal (sym);
sym->allocreq = 1;
return;
}
/* if volatile then */
if (IS_VOLATILE (sym->etype))
sym->allocreq = 1;
/* this is automatic */
/* if it's to be placed on the stack */
if (options.stackAuto || reentrant)
{
sym->onStack = 1;
if (options.useXstack)
{
/* PENDING: stack direction for xstack */
SPEC_OCLS (sym->etype) = xstack;
SPEC_STAK (sym->etype) = sym->stack = (xstackPtr + 1);
xstackPtr += getSize (sym->type);
}
else
{
SPEC_OCLS (sym->etype) = istack;
if (port->stack.direction > 0)
{
SPEC_STAK (sym->etype) = sym->stack = (stackPtr + 1);
stackPtr += getSize (sym->type);
}
else
{
stackPtr -= getSize (sym->type);
SPEC_STAK (sym->etype) = sym->stack = stackPtr;
}
}
allocIntoSeg (sym);
return;
}
/* else depending on the storage class specified */
/* if this is a function then assign code space */
if (IS_FUNC (sym->type))
{
SPEC_OCLS (sym->etype) = code;
return;
}
/* if this is a bit variable and no storage class */
if (bit && IS_SPEC(sym->type) && SPEC_NOUN (sym->type) == V_BIT)
{
SPEC_SCLS (sym->type) = S_BIT;
SPEC_OCLS (sym->type) = bit;
allocIntoSeg (sym);
return;
}
if ((SPEC_SCLS (sym->etype) == S_DATA) || (SPEC_SCLS (sym->etype) == S_REGISTER))
{
SPEC_OCLS (sym->etype) = (options.noOverlay ? data : overlay);
allocIntoSeg (sym);
return;
}
if (allocDefault (sym))
{
return;
}
/* again note that we have put it into the overlay segment
will remove and put into the 'data' segment if required after
overlay analysis has been done */
if (options.model == MODEL_SMALL)
{
SPEC_OCLS (sym->etype) =
(options.noOverlay ? port->mem.default_local_map : overlay);
}
else
{
SPEC_OCLS (sym->etype) = port->mem.default_local_map;
}
allocIntoSeg (sym);
}
/*-----------------------------------------------------------------*/
/* deallocLocal - deallocates the local variables */
/*-----------------------------------------------------------------*/
void
deallocLocal (symbol * csym)
{
symbol *sym;
for (sym = csym; sym; sym = sym->next)
{
if (sym->_isparm)
continue;
/* if it is on the stack */
if (sym->onStack)
{
if (options.useXstack)
xstackPtr -= getSize (sym->type);
else
stackPtr -= getSize (sym->type);
}
/* if not used give a warning */
if (!sym->isref && !IS_STATIC (sym->etype))
werror (W_NO_REFERENCE,
currFunc ? currFunc->name : "(unknown)",
"local variable", sym->name);
/* now delete it from the symbol table */
deleteSym (SymbolTab, sym, sym->name);
}
}
/*-----------------------------------------------------------------*/
/* overlay2data - moves declarations from the overlay seg to data */
/*-----------------------------------------------------------------*/
void
overlay2data ()
{
symbol *sym;
for (sym = setFirstItem (overlay->syms); sym;
sym = setNextItem (overlay->syms))
{
SPEC_OCLS (sym->etype) = data;
allocIntoSeg (sym);
}
setToNull ((void *) &overlay->syms);
}
/*-----------------------------------------------------------------*/
/* overlay2Set - will add all symbols from the overlay segment to */
/* the set of sets containing the overlable symbols */
/*-----------------------------------------------------------------*/
void
overlay2Set ()
{
symbol *sym;
set *oset = NULL;
for (sym = setFirstItem (overlay->syms); sym;
sym = setNextItem (overlay->syms))
{
addSet (&oset, sym);
}
setToNull ((void *) &overlay->syms);
addSet (&ovrSetSets, oset);
}
/*-----------------------------------------------------------------*/
/* allocVariables - creates decl & assign storage class for a v */
/*-----------------------------------------------------------------*/
int
allocVariables (symbol * symChain)
{
symbol *sym;
symbol *csym;
int stack = 0;
int saveLevel = 0;
/* go thru the symbol chain */
for (sym = symChain; sym; sym = sym->next)
{
/* if this is a typedef then add it */
/* to the typedef table */
if (IS_TYPEDEF (sym->etype))
{
/* check if the typedef already exists */
csym = findSym (TypedefTab, NULL, sym->name);
if (csym && csym->level == sym->level)
werror (E_DUPLICATE_TYPEDEF, sym->name);
SPEC_EXTR (sym->etype) = 0;
addSym (TypedefTab, sym, sym->name, sym->level, sym->block, 0);
continue; /* go to the next one */
}
/* make sure it already exists */
csym = findSymWithLevel (SymbolTab, sym);
if (!csym || (csym && csym->level != sym->level))
csym = sym;
/* check the declaration */
checkDecl (csym, 0);
/* if this is a function or a pointer to a */
/* function then do args processing */
if (funcInChain (csym->type))
{
processFuncArgs (csym);
}
/* if this is an extern variable then change */
/* the level to zero temporarily */
if (IS_EXTERN (csym->etype) || IS_FUNC (csym->type))
{
saveLevel = csym->level;
csym->level = 0;
}
/* if this is a literal then it is an enumerated */
/* type so need not allocate it space for it */
if (IS_LITERAL (sym->etype))
continue;
/* generate the actual declaration */
if (csym->level)
{
allocLocal (csym);
if (csym->onStack)
stack += getSize (csym->type);
}
else
allocGlobal (csym);
/* restore the level */
if (IS_EXTERN (csym->etype) || IS_FUNC (csym->type))
csym->level = saveLevel;
}
return stack;
}
#define BTREE_STACK 1
/*-----------------------------------------------------------------*/
/* redoStackOffsets :- will reassign the values for stack offsets */
/*-----------------------------------------------------------------*/
void
redoStackOffsets (void)
{
symbol *sym;
int sPtr = 0;
int xsPtr = -1;
/* after register allocation is complete we know
which variables will need to be assigned space
on the stack. We will eliminate those variables
which do not have the allocReq flag thus reducing
the stack space */
for (sym = setFirstItem (istack->syms); sym; sym = setNextItem (istack->syms))
{
int size = getSize (sym->type);
/* nothing to do with parameters so continue */
if ((sym->_isparm && !IS_REGPARM (sym->etype)))
continue;
if (BTREE_STACK)
{
/* Remove them all, and let btree_alloc() below put them back in more efficiently. */
currFunc->stack -= size;
SPEC_STAK (currFunc->etype) -= size;
if(IS_AGGREGATE (sym->type) || sym->allocreq)
btree_add_symbol (sym);
}
/* Do it the old way - compared to the btree approach we waste space when allocating
variables that had their address taken, unions and aggregates. */
else
{
/* if allocation not required then subtract
size from overall stack size & continue */
if (!IS_AGGREGATE (sym->type) && !sym->allocreq)
{
currFunc->stack -= size;
SPEC_STAK (currFunc->etype) -= size;
continue;
}
if (port->stack.direction > 0)
{
SPEC_STAK (sym->etype) = sym->stack = (sPtr + 1);
sPtr += size;
}
else
{
sPtr -= size;
SPEC_STAK (sym->etype) = sym->stack = sPtr;
}
}
}
if (BTREE_STACK && elementsInSet (istack->syms))
{
btree_alloc ();
btree_clear ();
}
/* do the same for the external stack */
if (!xstack)
return;
for (sym = setFirstItem (xstack->syms); sym; sym = setNextItem (xstack->syms))
{
int size = getSize (sym->type);
/* nothing to do with parameters so continue */
if ((sym->_isparm && !IS_REGPARM (sym->etype)))
continue;
if (IS_AGGREGATE (sym->type))
{
SPEC_STAK (sym->etype) = sym->stack = (xsPtr + 1);
xsPtr += size;
continue;
}
/* if allocation not required then subtract
size from overall stack size & continue */
if (!sym->allocreq)
{
currFunc->xstack -= size;
SPEC_STAK (currFunc->etype) -= size;
continue;
}
SPEC_STAK (sym->etype) = sym->stack = (xsPtr + 1);
xsPtr += size;
}
}
#define SP_BP(sp, bp) (options.omitFramePtr ? sp : bp)
#define SYM_BP(sym) (SPEC_OCLS (sym->etype)->paged ? SP_BP("_spx", "_bpx") : SP_BP("sp", "_bp"))
/*-----------------------------------------------------------------*/
/* printAllocInfoSeg- print the allocation for a given section */
/*-----------------------------------------------------------------*/
static int
printAllocInfoSeg (memmap * map, symbol * func, struct dbuf_s *oBuf)
{
symbol *sym;
int flg = FALSE;
if (!map || !map->syms)
return 0;
for (sym = setFirstItem (map->syms); sym;
sym = setNextItem (map->syms))
{
if (sym->level == 0)
continue;
if (sym->localof != func)
continue;
dbuf_printf (oBuf, ";%-25s Allocated ", sym->name);
flg = TRUE;
/* if assigned to registers */
if (!sym->allocreq && sym->reqv)
{
int i;
sym = OP_SYMBOL (sym->reqv);
if (!sym->isspilt || sym->remat)
{
dbuf_append_str (oBuf, "to registers ");
for (i = 0; i < 4 && sym->regs[i]; i++)
dbuf_printf (oBuf, "%s ", port->getRegName (sym->regs[i]));
dbuf_append_char (oBuf, '\n');
continue;
}
else
{
sym = sym->usl.spillLoc;
}
}
/* if on stack */
if (sym->onStack)
{
int stack_offset = 0;
if (options.omitFramePtr)
{
if (SPEC_OCLS (sym->etype)->paged)
stack_offset = func->xstack;
else
stack_offset = func->stack;
}
dbuf_printf (oBuf, "to stack - %s %+d\n", SYM_BP (sym), sym->stack - stack_offset);
continue;
}
/* otherwise give rname */
dbuf_printf (oBuf, "with name '%s'\n", sym->rname);
}
return flg;
}
/*-----------------------------------------------------------------*/
/* canOverlayLocals - returns true if the local variables can overlayed */
/*-----------------------------------------------------------------*/
static bool
canOverlayLocals (eBBlock ** ebbs, int count)
{
int i;
/* if staticAuto is in effect or the current function
being compiled is reentrant or the overlay segment
is empty or no overlay option is in effect then */
if (options.noOverlay ||
options.stackAuto ||
(currFunc &&
(IFFUNC_ISREENT (currFunc->type) ||
FUNC_ISISR (currFunc->type))) ||
elementsInSet (overlay->syms) == 0)
{
return FALSE;
}
/* if this is a forces overlay */
if (IFFUNC_ISOVERLAY(currFunc->type)) return TRUE;
/* otherwise do thru the blocks and see if there
any function calls if found then return false */
for (i = 0; i < count; i++)
{
iCode *ic;
for (ic = ebbs[i]->sch; ic; ic = ic->next)
if (ic)
{
if (ic->op == CALL)
{
sym_link *ftype = operandType(IC_LEFT(ic));
/* builtins only can use overlays */
if (!IFFUNC_ISBUILTIN(ftype)) return FALSE;
}
else if (ic->op == PCALL)
{
return FALSE;
}
}
}
/* no function calls found return TRUE */
return TRUE;
}
/*-----------------------------------------------------------------*/
/* doOverlays - move the overlay segment to appropriate location */
/*-----------------------------------------------------------------*/
void
doOverlays (eBBlock ** ebbs, int count)
{
if (!overlay)
return;
/* check if the parameters and local variables
of this function can be put in the overlay segment
This check is essentially to see if the function
calls any other functions if yes then we cannot
overlay */
if (canOverlayLocals (ebbs, count))
/* if we can then put the parameters &
local variables in the overlay set */
overlay2Set ();
else
/* otherwise put them into data where
they belong */
overlay2data ();
}
/*-----------------------------------------------------------------*/
/* printAllocInfo - prints allocation information for a function */
/*-----------------------------------------------------------------*/
void
printAllocInfo (symbol * func, struct dbuf_s * oBuf)
{
#define BREAKLINE ";------------------------------------------------------------\n"
int cnt = 0;
set *ovrset;
set *tempOverlaySyms;
if (!func)
return;
/* must be called after register allocation is complete */
dbuf_append_str (oBuf, BREAKLINE);
dbuf_printf (oBuf, ";Allocation info for local variables in function '%s'\n", func->name);
dbuf_append_str (oBuf, BREAKLINE);
cnt += printAllocInfoSeg (xstack, func, oBuf);
cnt += printAllocInfoSeg (istack, func, oBuf);
cnt += printAllocInfoSeg (code, func, oBuf);
cnt += printAllocInfoSeg (data, func, oBuf);
cnt += printAllocInfoSeg (xdata, func, oBuf);
cnt += printAllocInfoSeg (idata, func, oBuf);
cnt += printAllocInfoSeg (sfr, func, oBuf);
cnt += printAllocInfoSeg (sfrbit, func, oBuf);
tempOverlaySyms = overlay->syms;
/* search the set of overlay sets for local variables/parameters */
for (ovrset = setFirstItem (ovrSetSets); ovrset;
ovrset = setNextItem (ovrSetSets))
{
overlay->syms = ovrset;
cnt += printAllocInfoSeg (overlay, func, oBuf);
}
overlay->syms = tempOverlaySyms;
if (cnt)
dbuf_append_str (oBuf, BREAKLINE);
}
|