summary refs log tree commit diff stats
path: root/nim/magicsys.pas
blob: f4e4beafe1e712daa6e4d7b517afecb55400b3fe (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
//
//
//           The Nimrod Compiler
//        (c) Copyright 2009 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit magicsys;

// Built-in types and compilerprocs are registered here.

interface

{$include 'config.inc'}

uses
  nsystem,
  ast, astalgo, nhashes, msgs, platform, nversion, ntime, idents, rodread;

var // magic symbols in the system module:
  SystemModule: PSym;

procedure registerSysType(t: PType);
function getSysType(const kind: TTypeKind): PType;

function getCompilerProc(const name: string): PSym;
procedure registerCompilerProc(s: PSym);

procedure InitSystem(var tab: TSymTab);
procedure FinishSystem(const tab: TStrTable);

function getSysSym(const name: string): PSym;

implementation

var
  gSysTypes: array [TTypeKind] of PType;
  compilerprocs: TStrTable;

procedure registerSysType(t: PType);
begin
  if gSysTypes[t.kind] = nil then gSysTypes[t.kind] := t;
end;

function newSysType(kind: TTypeKind; size: int): PType;
begin
  result := newType(kind, systemModule);
  result.size := size;
  result.align := size;
end;

function getSysSym(const name: string): PSym;
begin
  result := StrTableGet(systemModule.tab, getIdent(name));
  if result = nil then rawMessage(errSystemNeeds, name);
  if result.kind = skStub then loadStub(result); 
end;

function sysTypeFromName(const name: string): PType;
begin
  result := getSysSym(name).typ;
end;

function getSysType(const kind: TTypeKind): PType;
begin
  result := gSysTypes[kind];
  if result = nil then begin
    case kind of
      tyInt:     result := sysTypeFromName('int');
      tyInt8:    result := sysTypeFromName('int8');
      tyInt16:   result := sysTypeFromName('int16');
      tyInt32:   result := sysTypeFromName('int32');
      tyInt64:   result := sysTypeFromName('int64');
      tyFloat:   result := sysTypeFromName('float');
      tyFloat32: result := sysTypeFromName('float32');
      tyFloat64: result := sysTypeFromName('float64');
      tyBool:    result := sysTypeFromName('bool');
      tyChar:    result := sysTypeFromName('char');
      tyString:  result := sysTypeFromName('string');
      tyCstring: result := sysTypeFromName('cstring');
      tyPointer: result := sysTypeFromName('pointer');
      tyNil: result := newSysType(tyNil, ptrSize);
      else InternalError('request for typekind: ' + typeKindToStr[kind]);
    end;  
    gSysTypes[kind] := result;
  end;
  if result.kind <> kind then 
    InternalError('wanted: ' + typeKindToStr[kind] 
      +{&} ' got: ' +{&} typeKindToStr[result.kind]);
  if result = nil then InternalError('type not found: ' + typeKindToStr[kind]);
end;

function getCompilerProc(const name: string): PSym;
var
  ident: PIdent;
begin
  ident := getIdent(name, getNormalizedHash(name));
  result := StrTableGet(compilerprocs, ident);
  if result = nil then begin
    result := StrTableGet(rodCompilerProcs, ident);
    if result <> nil then begin 
      strTableAdd(compilerprocs, result);
      if result.kind = skStub then loadStub(result);
    end;
    // A bit hacky that this code is needed here, but it is the easiest 
    // solution in order to avoid special cases for sfCompilerProc in the
    // rodgen module. Another solution would be to always recompile the system
    // module. But I don't want to do that as that would mean less testing of
    // the new symbol file cache (and worse performance).
  end;
end;

procedure registerCompilerProc(s: PSym);
begin
  strTableAdd(compilerprocs, s);
end;
(*
function FindMagic(const tab: TStrTable; m: TMagic; const s: string): PSym;
var
  ti: TIdentIter;
begin
  result := InitIdentIter(ti, tab, getIdent(s));
  while result <> nil do begin
    if (result.magic = m) then exit;
    result := NextIdentIter(ti, tab)
  end
end;

function NewMagic(kind: TSymKind; const name: string;
  const info: TLineInfo): PSym;
begin
  result := newSym(kind, getIdent(name), SystemModule);
  Include(result.loc.Flags, lfNoDecl);
  result.info := info;
end;

function newMagicType(const info: TLineInfo; kind: TTypeKind;
  magicSym: PSym): PType;
begin
  result := newType(kind, SystemModule);
  result.sym := magicSym;
end;

procedure setSize(t: PType; size: int);
begin
  t.align := size;
  t.size := size;
end;

procedure addMagicSym(var tab: TSymTab; sym: PSym; sys: PSym);
begin
  SymTabAdd(tab, sym);
  StrTableAdd(sys.tab, sym); // add to interface
  include(sym.flags, sfInInterface);
end;

var
  fakeInfo: TLineInfo;

procedure addIntegral(var tab: TSymTab; kind: TTypeKind; const name: string;
                      size: int);
var
  t: PSym;
begin
  t := newMagic(skType, name, fakeInfo);
  t.typ := newMagicType(fakeInfo, kind, t);
  setSize(t.typ, size);
  addMagicSym(tab, t, SystemModule);
  gSysTypes[kind] := t.typ;
end;

procedure addMagicTAnyEnum(var tab: TSymTab);
var
  s: PSym;
begin
  s := newMagic(skType, 'TAnyEnum', fakeInfo);
  s.typ := newMagicType(fakeInfo, tyAnyEnum, s);
  SymTabAdd(tab, s);
end;
*)
procedure InitSystem(var tab: TSymTab);
begin (*
  if SystemModule = nil then InternalError('systemModule == nil');
  fakeInfo := newLineInfo('system.nim', 1, 1);
  // symbols with compiler magic are pretended to be in system at line 1

  // TAnyEnum:
  addMagicTAnyEnum(tab);

  // nil:
  gSysTypes[tyNil] := newMagicType(fakeInfo, tyNil, nil);
  SetSize(gSysTypes[tyNil], ptrSize);
  // no need to add it to symbol table since it is a reserved word

  // boolean type:
  addIntegral(tab, tyBool, 'bool', 1);

  // false:
  c := NewMagic(skConst, 'false', fakeInfo);
  c.typ := gSysTypes[tyBool];
  c.ast := newIntNode(nkIntLit, ord(false));
  c.ast.typ := gSysTypes[tyBool];
  addMagicSym(tab, c, systemModule);

  // true:
  c := NewMagic(skConst, 'true', fakeInfo);
  c.typ := gSysTypes[tyBool];
  c.ast := newIntNode(nkIntLit, ord(true));
  c.ast.typ := gSysTypes[tyBool];
  addMagicSym(tab, c, systemModule);

  addIntegral(tab, tyFloat32, 'float32', 4);
  addIntegral(tab, tyFloat64, 'float64', 8);
  addIntegral(tab, tyInt8,    'int8',    1);
  addIntegral(tab, tyInt16,   'int16',   2);
  addIntegral(tab, tyInt32,   'int32',   4);
  addIntegral(tab, tyInt64,   'int64',   8);

  if cpu[targetCPU].bit = 64 then begin
    addIntegral(tab, tyFloat128, 'float128', 16);
    addIntegral(tab, tyInt, 'int', 8);
    addIntegral(tab, tyFloat, 'float', 8);
  end
  else if cpu[targetCPU].bit = 32 then begin
    addIntegral(tab, tyInt, 'int', 4);
    addIntegral(tab, tyFloat, 'float', 8);
  end
  else begin // 16 bit cpu:
    addIntegral(tab, tyInt, 'int', 2);
    addIntegral(tab, tyFloat, 'float', 4);
  end;

  // char type:
  addIntegral(tab, tyChar, 'char', 1);

  // string type:
  addIntegral(tab, tyString, 'string', ptrSize);
  typ := gSysTypes[tyString];
  addSon(typ, gSysTypes[tyChar]);
  
  // pointer type:
  addIntegral(tab, tyPointer, 'pointer', ptrSize);


  addIntegral(tab, tyCString, 'cstring', ptrSize);
  typ := gSysTypes[tyCString];
  addSon(typ, gSysTypes[tyChar]);

  gSysTypes[tyEmptySet] := newMagicType(fakeInfo, tyEmptySet, nil);

  intSetBaseType := newMagicType(fakeInfo, tyRange, nil);
  addSon(intSetBaseType, gSysTypes[tyInt]); // base type
  setSize(intSetBaseType, int(gSysTypes[tyInt].size));
  intSetBaseType.n := newNodeI(nkRange, fakeInfo);
  addSon(intSetBaseType.n, newIntNode(nkIntLit, 0));
  addSon(intSetBaseType.n, newIntNode(nkIntLit, nversion.MaxSetElements-1));
  intSetBaseType.n.sons[0].info := fakeInfo;
  intSetBaseType.n.sons[1].info := fakeInfo;
  intSetBaseType.n.sons[0].typ := gSysTypes[tyInt];
  intSetBaseType.n.sons[1].typ := gSysTypes[tyInt]; *)
end;

procedure FinishSystem(const tab: TStrTable);
begin (*
  notSym := findMagic(tab, mNot, 'not');
  if (notSym = nil) then
    rawMessage(errSystemNeeds, 'not');

  countUpSym := StrTableGet(tab, getIdent('countup'));
  if (countUpSym = nil) then
    rawMessage(errSystemNeeds, 'countup'); *)
end;

initialization
  initStrTable(compilerprocs);
end.