LlvmGenerator.py 16.5 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    OpenGEODE - A tiny SDL Editor for TASTE

    This module generates LLVM IR code from SDL process models, allowing
    generation of a binary application without an intermediate language.
    LLVM also allows for various code verification, analysis, and optimization.

    The design is based on the Ada code generator. Check it for details.

    Copyright (c) 2012-2013 European Space Agency

    Designed and implemented by Maxime Perrotin

    Contact: maxime.perrotin@esa.int
"""

import logging
Maxime Perrotin's avatar
Maxime Perrotin committed
21
from singledispatch import singledispatch
Maxime Perrotin's avatar
Maxime Perrotin committed
22
23
24
from llvm import core, passes, ee

import ogAST
Maxime Perrotin's avatar
Maxime Perrotin committed
25
import Helper
Maxime Perrotin's avatar
Maxime Perrotin committed
26
27
28

LOG = logging.getLogger(__name__)

Maxime Perrotin's avatar
Maxime Perrotin committed
29
30
__all__ = ['generate']

Maxime Perrotin's avatar
Maxime Perrotin committed
31
32
33
34
35
36
37
38

# LLVM Global variable - Initialized when the generator is invoked
LLVM = {
    # The LLVM module, which holds all the IR code.
    'module': None,
    # Dictionary that keeps track of which values are defined in the current
    # scope and what their LLVM representation is.
    'named_values': {},
39
40
41
    # Dictionary that keeps track of the defined states and its integer
    # constant representation
    'states': {},
42
43
    # The builder used for the current function generation.
    'builder': None,
Maxime Perrotin's avatar
Maxime Perrotin committed
44
45
46
47
48
49
    # The function optimization passes manager.
    'pass_manager': None,
    # The LLVM execution engine.
    'executor': None
}

Maxime Perrotin's avatar
Maxime Perrotin committed
50
51
52
53
@singledispatch
def generate(ast):
    ''' Generate the code for an item of the AST '''
    raise TypeError('[Backend] Unsupported AST construct')
Maxime Perrotin's avatar
Maxime Perrotin committed
54

Maxime Perrotin's avatar
Maxime Perrotin committed
55
# Processing of the AST
Maxime Perrotin's avatar
Maxime Perrotin committed
56

Maxime Perrotin's avatar
Maxime Perrotin committed
57
58
59
@generate.register(ogAST.Process)
def _process(process):
    ''' Generate LLVM IR code (incomplete) '''
60

61
    process_name = str(process.processName)
Maxime Perrotin's avatar
Maxime Perrotin committed
62
63
    LOG.info('Generating LLVM IR code for process ' + str(process_name))

Maxime Perrotin's avatar
Maxime Perrotin committed
64
65
66
67
68
69
70
    # In case model has nested states, flatten everything
    Helper.flatten(process)

    # Make an maping {input: {state: transition...}} in order to easily
    # generate the lookup tables for the state machine runtime
    mapping = Helper.map_input_state(process)

Maxime Perrotin's avatar
Maxime Perrotin committed
71
    # Initialise LLVM global structure
72
    LLVM['module'] = core.Module.new(process_name)
Maxime Perrotin's avatar
Maxime Perrotin committed
73
74
    LLVM['pass_manager'] = passes.FunctionPassManager.new(LLVM['module'])
    LLVM['executor'] = ee.ExecutionEngine.new(LLVM['module'])
dbarbera's avatar
dbarbera committed
75
    LLVM['types'] = process.dataview
Maxime Perrotin's avatar
Maxime Perrotin committed
76
77
78
    # Set up the optimizer pipeline.
    # Start with registering info about how the
    # target lays out data structures.
79
80
81
82
83
84
85
86
87
88
#   LLVM['pass_manager'].add(LLVM['executor'].target_data)
#   # Do simple "peephole" optimizations and bit-twiddling optzns.
#   LLVM['pass_manager'].add(passes.PASS_INSTRUCTION_COMBINING)
#   # Reassociate expressions.
#   LLVM['pass_manager'].add(passes.PASS_REASSOCIATE)
#   # Eliminate Common SubExpressions.
#   LLVM['pass_manager'].add(passes.PASS_GVN)
#   # Simplify the control flow graph (deleting unreachable blocks, etc).
#   LLVM['pass_manager'].add(passes.PASS_CFG_SIMPLIFICATION)
#   LLVM['pass_manager'].initialize()
Maxime Perrotin's avatar
Maxime Perrotin committed
89

90
91
92
93
94
95
    # Initialize states enum
    for name in process.mapping.iterkeys():
        if not name.endswith('START'):
            cons = core.Constant.int(core.Type.int(), len(LLVM['states']))
            LLVM['states'][name] = cons

96
    # Generate state var
97
98
    LLVM['module'].add_global_variable(core.Type.int(), 'state')

99
    # Generare process-level vars
dbarbera's avatar
dbarbera committed
100
101
102
    for var_name, (var_asn1_type, def_value) in process.variables.viewitems():
        var_type = _generate_type(find_basic_type(var_asn1_type))
        var_ptr = LLVM['module'].add_global_variable(var_type, str(var_name))
103
        if def_value:
dbarbera's avatar
dbarbera committed
104
            raise NotImplementedError
105

106
    # Generate process functions
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
    runtr_func = _generate_runtr_func(process)
    _generate_startup_func(process, process_name, runtr_func)

    print LLVM['module']


def _generate_runtr_func(process):
    '''Generate code for the run_transition function'''
    func_name = 'run_transition'
    func_type = core.Type.function(core.Type.void(), [core.Type.int()])
    func = core.Function.new(LLVM['module'], func_type, func_name)

    entry_block = func.append_basic_block('entry')
    cond_block = func.append_basic_block('cond')
    body_block = func.append_basic_block('body')
    exit_block = func.append_basic_block('exit')

    builder = core.Builder.new(entry_block)
    LLVM['builder'] = builder

    # entry
    id_ptr = builder.alloca(core.Type.int(), None, 'id')
    LLVM['named_values']['id'] = id_ptr
    builder.store(func.args[0], id_ptr)
    builder.branch(cond_block)

    # cond
    builder.position_at_end(cond_block)
    id_ptr = func.args[0]
    no_tr_cons = core.Constant.int(core.Type.int(), -1)
    cond_val = builder.icmp(core.ICMP_NE, id_ptr, no_tr_cons, 'cond')
    builder.cbranch(cond_val, body_block, exit_block)

    # body
    builder.position_at_end(body_block)
    switch = builder.switch(func.args[0], exit_block)

    # transitions
    for idx, tr in enumerate(process.transitions):
        tr_block = func.append_basic_block('tr%d' % idx)
        const = core.Constant.int(core.Type.int(), idx)
        switch.add_case(const, tr_block)
        builder.position_at_end(tr_block)
        generate(tr)
        builder.branch(cond_block)

    # exit
    builder.position_at_end(exit_block)
    builder.ret_void()

    func.verify()
Maxime Perrotin's avatar
Maxime Perrotin committed
158
    LLVM['named_values'].clear()
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    return func


def _generate_startup_func(process, process_name, runtr_func):
    '''Generate code for the startup function'''
    func_name = process_name + '_startup'
    func_type = core.Type.function(core.Type.void(), [])
    func = core.Function.new(LLVM['module'], func_type, func_name)

    entry_block = func.append_basic_block('entry')
    builder = core.Builder.new(entry_block)
    LLVM['builder'] = builder

    # entry
    builder.call(runtr_func, [core.Constant.int(core.Type.int(), 0)])
Maxime Perrotin's avatar
Maxime Perrotin committed
174
175
    builder.ret_void()

176
177
    func.verify()
    return func
Maxime Perrotin's avatar
Maxime Perrotin committed
178
179
180
181


def write_statement(param, newline):
    ''' Generate the code for the special "write" operator '''
182
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
183
184
185
186
187

@generate.register(ogAST.Output)
@generate.register(ogAST.ProcedureCall)
def _call_external_function(output):
    ''' Generate the code of a set of output or procedure call statement '''
188
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
189
190
191
192
193


@generate.register(ogAST.TaskAssign)
def _task_assign(task):
    ''' A list of assignments in a task symbol '''
194
195
    for expr in task.elems:
        expression(expr)
Maxime Perrotin's avatar
Maxime Perrotin committed
196
197
198
199
200


@generate.register(ogAST.TaskInformalText)
def _task_informal_text(task):
    ''' Generate comments for informal text '''
201
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
202
203
204
205
206
207
208
209
210


@generate.register(ogAST.TaskForLoop)
def _task_forloop(task):
    '''
        Return the code corresponding to a for loop. Two forms are possible:
        for x in range ([start], stop [, step])
        for x in iterable (a SEQUENCE OF)
    '''
211
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
212

Maxime Perrotin's avatar
Maxime Perrotin committed
213
214
215
216
217
218
219
220
221
222
223
224
225
# ------ expressions --------

@singledispatch
def expression(expr):
    ''' Generate the code for Expression-classes, returning 3 things:
        - list of statements
        - useable string corresponding to the evaluation of the expression,
        - list of local declarations
        (API can differ depending on the backend)
    '''
    raise TypeError('Unsupported expression: ' + str(expr))


Maxime Perrotin's avatar
Maxime Perrotin committed
226

Maxime Perrotin's avatar
Maxime Perrotin committed
227
@expression.register(ogAST.PrimVariable)
Maxime Perrotin's avatar
Maxime Perrotin committed
228
229
def _primary_variable(prim):
    ''' Single variable reference '''
230
    return LLVM['module'].get_global_variable_named(str(prim.value[0]))
Maxime Perrotin's avatar
Maxime Perrotin committed
231
232


Maxime Perrotin's avatar
Maxime Perrotin committed
233
@expression.register(ogAST.PrimPath)
Maxime Perrotin's avatar
Maxime Perrotin committed
234
235
236
237
238
239
240
241
242
243
def _prim_path(primaryId):
    '''
        Return the string of an element list (path)
        cases: a => 'l_a' (reference to a variable)
        a_timer => 'a_timer'  (reference to a timer)
        a!b => a.b (field of a structure)
        a!b if a is a CHOICE => TypeOfa_b_get(a)
        a(Expression) => a(ExpressionSolver) (array index)
        Expression can be complex (if-then-else-fi..)
    '''
244
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
245
246


Maxime Perrotin's avatar
Maxime Perrotin committed
247
248
249
250
251
252
253
254
255
256
257
258
@expression.register(ogAST.ExprPlus)
@expression.register(ogAST.ExprMul)
@expression.register(ogAST.ExprMinus)
@expression.register(ogAST.ExprEq)
@expression.register(ogAST.ExprNeq)
@expression.register(ogAST.ExprGt)
@expression.register(ogAST.ExprGe)
@expression.register(ogAST.ExprLt)
@expression.register(ogAST.ExprLe)
@expression.register(ogAST.ExprDiv)
@expression.register(ogAST.ExprMod)
@expression.register(ogAST.ExprRem)
Maxime Perrotin's avatar
Maxime Perrotin committed
259
260
def _basic_operators(expr):
    ''' Expressions with two sides '''
261
    builder = LLVM['builder']
262
263
    lefttmp = expression(expr.left)
    righttmp = expression(expr.right)
264
265

    # load the value of the expression if it is a pointer
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    if lefttmp.type.kind == core.TYPE_POINTER:
        lefttmp = builder.load(lefttmp, 'lefttmp')
    if righttmp.type.kind == core.TYPE_POINTER:
        righttmp = builder.load(righttmp, 'lefttmp')

    if lefttmp.type.kind != righttmp.type.kind:
        raise NotImplementedError

    if lefttmp.type.kind == core.TYPE_INTEGER:
        if expr.operand == '+':
            return builder.add(lefttmp, righttmp, 'addtmp')
        elif expr.operand == '-':
            return builder.sub(lefttmp, righttmp, 'subtmp')
        elif expr.operand == '*':
            return builder.mul(lefttmp, righttmp, 'multmp')
        elif expr.operand == '/':
            return builder.sdiv(lefttmp, righttmp, 'divtmp')
        elif expr.operand == 'mod':
284
285
286
287
288
289
            # l mod r == (((l rem r) + r) rem r)
            remtmp = builder.srem(lefttmp, righttmp)
            addtmp = builder.add(remtmp, righttmp)
            return builder.srem(addtmp, righttmp, 'modtmp')
        elif expr.operand == 'rem':
            return builder.srem(lefttmp, righttmp, 'remtmp')
290
291
292
293
294
295
296
297
298
299
300
301
        else:
            raise NotImplementedError
    elif lefttmp.type.kind == core.TYPE_DOUBLE:
        if expr.operand == '+':
            return builder.fadd(lefttmp, righttmp, 'addtmp')
        elif expr.operand == '-':
            return builder.fsub(lefttmp, righttmp, 'subtmp')
        elif expr.operand == '*':
            return builder.fmul(lefttmp, righttmp, 'multmp')
        elif expr.operand == '/':
            return builder.fdiv(lefttmp, righttmp, 'divtmp')
        elif expr.operand == 'mod':
302
303
304
305
306
307
            # l mod r == (((l rem r) + r) rem r)
            remtmp = builder.frem(lefttmp, righttmp)
            addtmp = builder.fadd(remtmp, righttmp)
            return builder.frem(addtmp, righttmp, 'modtmp')
        elif expr.operand == 'rem':
            return builder.frem(lefttmp, righttmp, 'remtmp')
308
309
        else:
            raise NotImplementedError
310
    else:
311
        raise NotImplementedError
312

Maxime Perrotin's avatar
Maxime Perrotin committed
313

314
315
316
317
318
319
320
321
322
323
@expression.register(ogAST.ExprAssign)
def _assign(expr):
    '''Assign expression'''
    builder = LLVM['builder']
    left = expression(expr.left)
    right = expression(expr.right)

    builder.store(right, left)
    return left

Maxime Perrotin's avatar
Maxime Perrotin committed
324

Maxime Perrotin's avatar
Maxime Perrotin committed
325
326
327
@expression.register(ogAST.ExprOr)
@expression.register(ogAST.ExprAnd)
@expression.register(ogAST.ExprXor)
Maxime Perrotin's avatar
Maxime Perrotin committed
328
329
def _bitwise_operators(expr):
    ''' Logical operators '''
dbarbera's avatar
dbarbera committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
    builder = LLVM['builder']
    func = builder.basic_block.function

    lefttmp = expression(expr.left)
    righttmp = expression(expr.right)

    ty = find_basic_type(expr.exprType)
    if ty.kind != 'BooleanType':
        raise NotImplementedError

    # load the value of the expression if it is a pointer
    if lefttmp.type.kind == core.TYPE_POINTER:
        lefttmp = builder.load(lefttmp, 'lefttmp')
    if righttmp.type.kind == core.TYPE_POINTER:
        righttmp = builder.load(righttmp, 'lefttmp')

    if expr.operand == '&&':
        return builder.and_(lefttmp, righttmp, 'ortmp')
    elif expr.operand == '||':
        return builder.or_(lefttmp, righttmp, 'ortmp')
    else:
        return builder.xor(lefttmp, righttmp, 'xortmp')

Maxime Perrotin's avatar
Maxime Perrotin committed
353
354


Maxime Perrotin's avatar
Maxime Perrotin committed
355
@expression.register(ogAST.ExprAppend)
Maxime Perrotin's avatar
Maxime Perrotin committed
356
357
def _append(expr):
    ''' Generate code for the APPEND construct: a // b '''
358
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
359
360


Maxime Perrotin's avatar
Maxime Perrotin committed
361
@expression.register(ogAST.ExprIn)
Maxime Perrotin's avatar
Maxime Perrotin committed
362
363
def _expr_in(expr):
    ''' IN expressions: check if item is in a SEQUENCE OF '''
364
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
365
366


Maxime Perrotin's avatar
Maxime Perrotin committed
367
@expression.register(ogAST.PrimEnumeratedValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
368
369
def _enumerated_value(primary):
    ''' Generate code for an enumerated value '''
370
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
371
372


Maxime Perrotin's avatar
Maxime Perrotin committed
373
@expression.register(ogAST.PrimChoiceDeterminant)
Maxime Perrotin's avatar
Maxime Perrotin committed
374
375
def _choice_determinant(primary):
    ''' Generate code for a choice determinant (enumerated) '''
376
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
377
378


Maxime Perrotin's avatar
Maxime Perrotin committed
379
@expression.register(ogAST.PrimInteger)
380
381
382
383
384
def _integer(primary):
    ''' Generate code for a raw integer value  '''
    return core.Constant.int(core.Type.int(), primary.value[0])


Maxime Perrotin's avatar
Maxime Perrotin committed
385
@expression.register(ogAST.PrimReal)
386
387
def _real(primary):
    ''' Generate code for a raw real value  '''
dbarbera's avatar
dbarbera committed
388
    return core.Constant.real(core.Type.double(), primary.value[0])
389
390


Maxime Perrotin's avatar
Maxime Perrotin committed
391
@expression.register(ogAST.PrimBoolean)
392
393
def _boolean(primary):
    ''' Generate code for a raw boolean value  '''
dbarbera's avatar
dbarbera committed
394
395
396
397
    if primary.value[0].lower() == 'true':
        return core.Constant.int(core.Type.int(1), 1)
    else:
        return core.Constant.int(core.Type.int(1), 0)
Maxime Perrotin's avatar
Maxime Perrotin committed
398
399


Maxime Perrotin's avatar
Maxime Perrotin committed
400
@expression.register(ogAST.PrimEmptyString)
Maxime Perrotin's avatar
Maxime Perrotin committed
401
402
def _empty_string(primary):
    ''' Generate code for an empty SEQUENCE OF: {} '''
403
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
404
405


Maxime Perrotin's avatar
Maxime Perrotin committed
406
@expression.register(ogAST.PrimStringLiteral)
Maxime Perrotin's avatar
Maxime Perrotin committed
407
408
def _string_literal(primary):
    ''' Generate code for a string (Octet String) '''
409
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
410
411


Maxime Perrotin's avatar
Maxime Perrotin committed
412
@expression.register(ogAST.PrimConstant)
Maxime Perrotin's avatar
Maxime Perrotin committed
413
414
def _constant(primary):
    ''' Generate code for a reference to an ASN.1 constant '''
415
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
416
417


Maxime Perrotin's avatar
Maxime Perrotin committed
418
@expression.register(ogAST.PrimMantissaBaseExp)
Maxime Perrotin's avatar
Maxime Perrotin committed
419
420
def _mantissa_base_exp(primary):
    ''' Generate code for a Real with Mantissa-base-Exponent representation '''
421
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
422
423


Maxime Perrotin's avatar
Maxime Perrotin committed
424
@expression.register(ogAST.PrimIfThenElse)
Maxime Perrotin's avatar
Maxime Perrotin committed
425
426
def _if_then_else(ifThenElse):
    ''' Return string and statements for ternary operator '''
427
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
428
429


Maxime Perrotin's avatar
Maxime Perrotin committed
430
@expression.register(ogAST.PrimSequence)
Maxime Perrotin's avatar
Maxime Perrotin committed
431
432
def _sequence(seq):
    ''' Return Ada string for an ASN.1 SEQUENCE '''
433
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
434
435


Maxime Perrotin's avatar
Maxime Perrotin committed
436
@expression.register(ogAST.PrimSequenceOf)
Maxime Perrotin's avatar
Maxime Perrotin committed
437
438
def _sequence_of(seqof):
    ''' Return Ada string for an ASN.1 SEQUENCE OF '''
439
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
440
441


Maxime Perrotin's avatar
Maxime Perrotin committed
442
@expression.register(ogAST.PrimChoiceItem)
Maxime Perrotin's avatar
Maxime Perrotin committed
443
444
def _choiceitem(choice):
    ''' Return the Ada code for a CHOICE expression '''
445
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
446
447
448
449
450


@generate.register(ogAST.Decision)
def _decision(dec):
    ''' generate the code for a decision '''
451
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
452
453
454
455
456
457
458


@generate.register(ogAST.Label)
def _label(tr):
    ''' Transition following labels are generated in a separate section
        for visibility reasons (see Ada scope)
    '''
459
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
460
461
462
463
464


@generate.register(ogAST.Transition)
def _transition(tr):
    ''' generate the code for a transition '''
465
466
467
468
469
470
    for action in tr.actions:
        generate(action)
        if isinstance(action, ogAST.Label):
            return
    if tr.terminator:
        _generate_terminator(tr.terminator)
471
472


473
474
475
476
def _generate_terminator(term):
    builder = LLVM['builder']
    id_ptr = LLVM['named_values']['id']
    if term.label:
477
        raise NotImplementedError
478
479
480
481
482
483
484
485
486
487
    if term.kind == 'next_state':
        state = term.inputString.lower()
        if state.strip() != '-':
            next_id_cons = core.Constant.int(core.Type.int(), term.next_id)
            builder.store(next_id_cons, id_ptr)
            if term.next_id == -1:
                state_ptr = LLVM['module'].get_global_variable_named('state')
                state_id_cons = LLVM['states'][state]
                builder.store(state_id_cons, state_ptr)
        else:
488
            raise NotImplementedError
489
    elif term.kind == 'join':
490
        raise NotImplementedError
491
    elif term.kind == 'stop':
492
        raise NotImplementedError
493
    elif term.kind == 'return':
494
        raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
495
496
497
498
499


@generate.register(ogAST.Floating_label)
def _floating_label(label):
    ''' Generate the code for a floating label (Ada label + transition) '''
500
    raise NotImplementedError
Maxime Perrotin's avatar
Maxime Perrotin committed
501
502
503
504
505


@generate.register(ogAST.Procedure)
def _inner_procedure(proc):
    ''' Generate the code for a procedure '''
506
    raise NotImplementedError
dbarbera's avatar
dbarbera committed
507
508
509


def _generate_type(ty):
510
    '''Generate the equivalent LLVM type of a ASN.1 type'''
dbarbera's avatar
dbarbera committed
511
512
513
514
515
516
    basic_ty = find_basic_type(ty)
    if basic_ty.kind == 'IntegerType':
        return core.Type.int()
    elif basic_ty.kind == 'BooleanType':
        return core.Type.int(1)
    elif basic_ty.kind == 'RealType':
dbarbera's avatar
dbarbera committed
517
        return core.Type.double()
dbarbera's avatar
dbarbera committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    else:
        raise NotImplementedError


# TODO: Refactor this into the helper module
def find_basic_type(a_type):
    ''' Return the ASN.1 basic type of a_type '''
    basic_type = a_type
    while basic_type.kind == 'ReferenceType':
        # Find type with proper case in the data view
        for typename in LLVM['types'].viewkeys():
            if typename.lower() == basic_type.ReferencedTypeName.lower():
                basic_type = LLVM['types'][typename].type
                break
    return basic_type