1 using System;
2 using System.Collections;
3 using CodeFormatter = System.Text.CodeFormatter;
4
5 namespace cmsx.intermediate
6 {
7 public class GlobalVariable : Value
8 {
9 public nothrow GlobalVariable(Type* type_, const string& name_, ConstantValue* initializer_, int line_, Location* homeLocation_, bool once_) :
10 base(), name(name_), initializer(initializer_), homeLocation(homeLocation_), once(once_)
11 {
12 base->type = type_;
13 base->line = line_;
14 }
15 public override nothrow Location* HomeLocation() const
16 {
17 return homeLocation;
18 }
19 public override nothrow string Name() const
20 {
21 return name;
22 }
23 public void Validate()
24 {
25 try
26 {
27 if (initializer != null)
28 {
29 initializer->Validate(type, "1");
30 }
31 }
32 catch (const Exception& ex)
33 {
34 throw Exception(ex.Message() + " at line " + ToString(line));
35 }
36 }
37 public void GenerateDeclaration(MachineCode& machineCode)
38 {
39 if (initializer != null)
40 {
41 if (once)
42 {
43 MachineInstruction* linkOnceInst = machineCode.GetInstruction(cmsx.assembly.LINKONCE, null);
44 linkOnceInst->AddOperand(machineCode.context.GetSymbolOperand(name));
45 }
46 else
47 {
48 MachineInstruction* externInst = machineCode.GetInstruction(cmsx.assembly.EXTERN, null);
49 externInst->AddOperand(machineCode.context.GetSymbolOperand(name));
50 }
51 }
52 }
53 public void GenerateDefinition(MachineCode& machineCode, Context& context)
54 {
55 if (initializer != null)
56 {
57 MachineInstruction* structInst = machineCode.GetInstruction(cmsx.assembly.STRUCT, name);
58 MachineInstruction* octaInst = machineCode.GetInstruction(cmsx.assembly.OCTA, null);
59 string dataLabel = name + "_data";
60 octaInst->AddOperand(machineCode.context.GetSymbolOperand(dataLabel));
61 initializer->GenerateCode(machineCode, dataLabel, context);
62 MachineInstruction* endSInst = machineCode.GetInstruction(cmsx.assembly.ENDS, name);
63 }
64 }
65 public void Print(CodeFormatter& formatter)
66 {
67 formatter.Write(type->Name());
68 if (once)
69 {
70 formatter.Write(" once");
71 }
72 formatter.Write(" ");
73 formatter.Write(name);
74 if (initializer != null)
75 {
76 formatter.Write(" = ");
77 formatter.Write(initializer->Name());
78 }
79 else
80 {
81 formatter.Write(";");
82 }
83 }
84 public string name;
85 public ConstantValue* initializer;
86 public Location* homeLocation;
87 public bool once;
88 }
89 }