set_scope_numbers.c 2.79 KB
Newer Older
clean's avatar
clean committed
1

2
3
#include "compiledefines.h"
#include "types.t"
clean's avatar
clean committed
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
#include "system.h"
#include "settings.h"
#include "syntaxtr.t"
#include "comsupport.h"
#include "buildtree.h"
#include "codegen_types.h"
#include "codegen1.h"
#include "codegen2.h"
#include "statesgen.h"
#include "checker.h"
#include "instructions.h"
#include "optimisations.h"
#include "sa.h"

#include "set_scope_numbers.h"

#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)

static int scope;

static void set_node_id_scope_numbers (NodeDefP node_defs,int local_scope)
{
	NodeDefP node_def_p;
	
	for_l (node_def_p,node_defs,def_next)
		node_def_p->def_id->nid_scope=local_scope;
}

static void set_root_scope_numbers (NodeP node_p,int local_scope)
{
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
34
35
36
37
38
39
40
41
/* RWS added switch nodes */
	switch (node_p->node_kind)
	{
		case IfNode:
			{
				int new_local_scope;
				ArgP arg_p;
		
John van Groningen's avatar
John van Groningen committed
42
43
				node_p->node_if_scope=scope;

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
44
45
				new_local_scope=scope+2;
				scope+=3;
clean's avatar
clean committed
46
		
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
				arg_p=node_p->node_arguments;
				set_root_scope_numbers (arg_p->arg_node,local_scope);
				
				++scope;
				arg_p=arg_p->arg_next;
				set_root_scope_numbers (arg_p->arg_node,new_local_scope);
				set_node_id_scope_numbers (node_p->node_then_node_defs,new_local_scope);	
				
				++scope;
				arg_p=arg_p->arg_next;
				set_root_scope_numbers (arg_p->arg_node,new_local_scope);
				set_node_id_scope_numbers (node_p->node_else_node_defs,new_local_scope);
			}
			break;
		case SwitchNode:
			{
				ArgP arg_p;
John van Groningen's avatar
John van Groningen committed
64
65
66
				int old_scope;
				
				old_scope=scope;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
67
68
				
				for_l (arg_p,node_p->node_arguments,arg_next){
John van Groningen's avatar
John van Groningen committed
69
70
71
72
73
74
					NodeP node_p;

					scope=old_scope;

					node_p=arg_p->arg_node;
					if (node_p->node_kind==CaseNode){
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
75
76
						NodeP case_alt_node_p;
						
John van Groningen's avatar
John van Groningen committed
77
78
79
						case_alt_node_p=node_p->node_arguments->arg_node;
	/*	Cedewarrior bug	if (case_alt_node_p->node_kind==PushNode) */
						if (node_p->node_arguments->arg_node->node_kind==PushNode)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
80
81
							set_root_scope_numbers (case_alt_node_p->node_arguments->arg_next->arg_node, local_scope);
						else
John van Groningen's avatar
John van Groningen committed
82
83
84
							set_root_scope_numbers (node_p->node_arguments->arg_node, local_scope);
					} else if (node_p->node_kind==DefaultNode){
						set_root_scope_numbers (node_p->node_arguments->arg_node, local_scope);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
85
86
					} else
						ErrorInCompiler ("set_scope_numbers.c", "set_root_scope_numbers", "");
John van Groningen's avatar
John van Groningen committed
87
88

					set_node_id_scope_numbers (node_p->node_node_defs,local_scope);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
89
90
91
				}
			}
			break;
John van Groningen's avatar
John van Groningen committed
92
		case GuardNode:
John van Groningen's avatar
John van Groningen committed
93
94
95
96
97
		{	
			int old_scope;
		
 			old_scope=scope;

John van Groningen's avatar
John van Groningen committed
98
			set_root_scope_numbers (node_p->node_arguments->arg_node,local_scope);
John van Groningen's avatar
John van Groningen committed
99
100
101

			scope=old_scope;

John van Groningen's avatar
John van Groningen committed
102
103
104
			set_root_scope_numbers (node_p->node_arguments->arg_next->arg_node,local_scope);
			set_node_id_scope_numbers (node_p->node_node_defs,local_scope);
			break;
John van Groningen's avatar
John van Groningen committed
105
		}
clean's avatar
clean committed
106
107
108
109
110
111
112
113
114
115
116
	}
}

void set_scope_numbers (RuleAltS *rule_alt_p)
{
	scope=1;
	
	set_root_scope_numbers (rule_alt_p->alt_rhs_root,0);
	set_node_id_scope_numbers (rule_alt_p->alt_rhs_defs,0);
}