aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/fasm_generator/fasm_generator.c
blob: b83f3d261753beea1bb29940aad3bee70baeeea3 (plain)
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
#include "fasm_generator.h"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "compiler/tree_parser/tree_parser.h"
#include "utils/memory/memory.h"

FasmLines generateFasm(SourceCode *code) {
  ParsedTree *root = treeParser(code);
  if (root != NULL) {
    FasmLines lines = _generateFasm(root, code);

    deleteParsedTree(root);
    return lines;
  }
  const FasmLines error = {
      .lines = NULL,
      /*.size = ERROR_SIZE,*/
  };
  return error;
}

FasmLines generateFasmWithPrint(SourceCode *code) {
  ParsedTree *root = treeParser(code);
  if (root != NULL) {
    printf("----tree parsed:\n");
    printParsedTreeNode(root);
    FasmLines lines = _generateFasm(root, code);

    deleteParsedTree(root);
    return lines;
  }
  fprintf(stderr, "error in tree parser\n");
  const FasmLines error = {
      .lines = NULL,
      /*.size = ERROR_SIZE,*/
  };
  return error;
}

FasmLines _generateFasm(const ParsedTree *root, SourceCode *code) {
  const TreeScopeMetadata *metadata = root->metadata;

  FasmVariables thisVariables = {
      .variables = a404m_malloc(metadata->variables_size *
                                sizeof(*thisVariables.variables)),
      .size = metadata->variables_size,
  };

  size_t size = 0;

  for (size_t i = 0; i < metadata->variables_size; ++i) {
    const TreeDefineVariableMetadata *variable = metadata->variables[i];
    FasmVariable keyValue = {
        .variable = variable,
        .value = size,
    };
    thisVariables.variables[i] = keyValue;
    size += 8;
  }

  const size_t variables_size = 1;
  FasmVariables *variables[1] = {
      &thisVariables,
  };

  FasmLines lines = {
      .lines = a404m_malloc(0),
      /*.size = 0,*/
  };

  for (size_t i = 0; i < metadata->lines_size; ++i) {
    const ParsedTree *node = metadata->lines[i];
    if (!nodeToFasmLine(node, &lines, variables, variables_size, code)) {
      goto RETURN_ERROR;
    }
  }

  return lines;

RETURN_ERROR:
  free(lines.lines);

  const FasmLines error = {
      .lines = NULL,
      /*.size = ERROR_SIZE,*/
  };
  return error;
}

bool nodeToFasmLine(const ParsedTree *node, FasmLines *lines,
                    FasmVariables *variables[], size_t variables_size,
                    SourceCode *code) {
  switch (node->token) {
    case TREE_TOKEN_NONE:
    case TREE_TOKEN_ROOT:
      printError("Is not allowed in compiler line %s:%d", code, node->strBegin,
                 node->strBegin, __FILE_NAME__, __LINE__);
      exit(1);
    case TREE_TOKEN_GLOBAL_SCOPE:
    case TREE_TOKEN_LOCAL_SCOPE: {
      const TreeScopeMetadata *metadata = node->metadata;

      FasmVariables thisVariables = {
          .variables = a404m_malloc(metadata->variables_size *
                                    sizeof(*thisVariables.variables)),
          .size = metadata->variables_size,
      };

      size_t size = 0;

      for (size_t i = 0; i < metadata->variables_size; ++i) {
        const TreeDefineVariableMetadata *variable = metadata->variables[i];
        FasmVariable keyValue = {
            .variable = variable,
            .value = size,
            .isGlobal = node->token == TREE_TOKEN_GLOBAL_SCOPE,
        };
        thisVariables.variables[i] = keyValue;
        size += getSizeOfVariable(variable);
      }

      size_t newVariables_size = variables_size + 1;
      FasmVariables *newVariables[newVariables_size];

      for (size_t i = 0; i < variables_size; ++i) {
        newVariables[i] = variables[i];
      }
      newVariables[variables_size] = &thisVariables;

      for (size_t i = 0; i < metadata->lines_size; ++i) {
        if (!nodeToFasmLine(metadata->lines[i], lines, newVariables,
                            newVariables_size, code)) {
          return false;
        }
      }
      return true;
    }
    case TREE_TOKEN_FUNCTION_CALL: {
      const TreeFunctionCallMetadata *metadata = node->metadata;
      return true;
    }
    case TREE_TOKEN_FUNCTION: {
      const TreeFunctionMetadata *metadata = node->metadata;
      return true;
    }
    case TREE_TOKEN_IDENTIFIER: {
      const TreeIdentifierMetadata *metadata = node->metadata;

      const FasmVariable fasmVariable = getVariableFasmKeyValue(
          metadata->variable, variables, variables_size);
      if (fasmVariable.variable == NULL) {
        return false;
      }

      FasmLine line = {
          /*.label = NULL,*/
          // TODO: do it
      };

      pushFasmLine(lines, line);

      return true;
    }
    case TREE_TOKEN_DEFINE_VARIABLE:
    case TREE_TOKEN_DEFINE_CONSTANT:
    case TREE_TOKEN_VALUE_STRING:
    case TREE_TOKEN_STRUCT:
  }
  fprintf(stderr, "Bad parsed token '%d' %s:%d", node->token, __FILE_NAME__,
          __LINE__);
  exit(1);
}

void pushFasmLine(FasmLines *lines, FasmLine line) {
  const size_t size =
      a404m_malloc_usable_size(lines->lines) / sizeof(*lines->lines);
  /*if (size == lines->size) {
    lines->lines =
        a404m_realloc(lines->lines, (size * 2 + 1) * sizeof(*lines->lines));
  }
  lines->lines[lines->size] = line;
  lines->size += 1;*/
}

size_t getSizeOfVariable(const TreeDefineVariableMetadata *variable) {
  return 8;  // TODO: do it;
}

FasmVariable getVariableFasmKeyValue(const TreeDefineVariableMetadata *variable,
                                     FasmVariables *variables[],
                                     size_t variables_size) {
  for (size_t i = 0; i < variables_size; ++i) {
    for (size_t j = 0; j < variables[i]->size; ++j) {
      if (variables[i]->variables[j].variable == variable) {
        return variables[i]->variables[j];
      }
    }
  }
  const FasmVariable error = {
      .variable = NULL,
      .value = 0,
      .isGlobal = false,
  };
  return error;
}