~ubuntu-branches/debian/experimental/nuitka/experimental

« back to all changes in this revision

Viewing changes to nuitka/codegen/CodeGeneration.py

  • Committer: Package Import Robot
  • Author(s): Kay Hayen
  • Date: 2014-10-05 19:28:20 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20141005192820-s06oca9rty517iy8
Tags: 0.5.5+ds-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
538
538
 
539
539
def generateFunctionCreationCode(to_name, function_body, defaults, kw_defaults,
540
540
                                  annotations, defaults_first, emit, context):
 
541
    # This is about creating functions, which is detail ridden stuff,
 
542
    # pylint: disable=R0914
 
543
 
541
544
    assert function_body.needsCreation(), function_body
542
545
 
543
546
    parameters = function_body.getParameters()
609
612
        source_ref          = function_body.getSourceReference(),
610
613
        function_doc        = function_body.getDoc(),
611
614
        is_generator        = function_body.isGenerator(),
 
615
        is_optimized        = not function_body.needsLocalsDict(),
612
616
        emit                = emit,
613
617
        context             = context
614
618
    )
961
965
 
962
966
 
963
967
def generateCallCode(to_name, call_node, emit, context):
 
968
    # There is a whole lot of different cases, for each of which, we create
 
969
    # optimized code, constant, with and without positional or keyword args
 
970
    # each, so there is lots of branches here, pylint: disable=R0912
 
971
 
964
972
    called_name = context.allocateTempName("called")
965
973
 
966
974
    generateExpressionCode(
971
979
    )
972
980
 
973
981
    call_args = call_node.getCallArgs()
974
 
 
975
982
    call_kw = call_node.getCallKw()
976
983
 
977
984
    if call_kw.isExpressionConstantRef() and call_kw.getConstant() == {}:
1215
1222
        assert False, expression
1216
1223
 
1217
1224
    if expression.isExpressionVariableRef():
1218
 
        if expression.getVariable() is None:
1219
 
            Tracing.printError("Illegal variable reference, not resolved.")
1220
 
 
1221
 
            expression.dump()
1222
 
            assert False, (
1223
 
                expression.getSourceReference(),
1224
 
                expression.getVariableName()
1225
 
            )
1226
 
 
1227
1225
        Generator.getVariableAccessCode(
1228
 
            to_name  = to_name,
1229
 
            variable = expression.getVariable(),
1230
 
            emit     = emit,
1231
 
            context  = context
 
1226
            to_name     = to_name,
 
1227
            variable    = expression.getVariable(),
 
1228
            needs_check = expression.mayRaiseException(BaseException),
 
1229
            emit        = emit,
 
1230
            context     = context
1232
1231
        )
1233
1232
    elif expression.isExpressionTempVariableRef():
1234
1233
        Generator.getVariableAccessCode(
1235
 
            to_name  = to_name,
1236
 
            variable = expression.getVariable(),
1237
 
            emit     = emit,
1238
 
            context  = context
 
1234
            to_name     = to_name,
 
1235
            variable    = expression.getVariable(),
 
1236
            needs_check = expression.mayRaiseException(BaseException),
 
1237
            emit        = emit,
 
1238
            context     = context
1239
1239
        )
1240
1240
    elif expression.isExpressionConstantRef():
1241
1241
        Generator.getConstantAccess(
1636
1636
    elif expression.isExpressionBuiltinOriginalRef():
1637
1637
        assert not expression.isExpressionBuiltinRef()
1638
1638
 
1639
 
        Generator.getBuiltinOriginalRefCode(
1640
 
            to_name      = to_name,
1641
 
            builtin_name = expression.getBuiltinName(),
1642
 
            emit         = emit,
1643
 
            context      = context
1644
 
        )
 
1639
        # This is not implemented currently, but ought to be one day.
 
1640
        assert False
1645
1641
    elif expression.isExpressionMakeTuple():
1646
1642
        generateTupleCreationCode(
1647
1643
            to_name  = to_name,
1734
1730
                expression = value
1735
1731
            )
1736
1732
 
1737
 
            base_name = context.allocateTempName("int_base")
 
1733
            base_name = context.allocateTempName("long_base")
1738
1734
 
1739
1735
            makeExpressionCode(
1740
1736
                to_name    = base_name,
3010
3006
    )
3011
3007
 
3012
3008
    Generator.getVariableAssignmentCode(
3013
 
        tmp_name = tmp_name2,
3014
 
        variable = tried_statement.getTargetVariableRef().getVariable(),
3015
 
        emit     = emit,
3016
 
        context  = context
 
3009
        tmp_name      = tmp_name2,
 
3010
        variable      = tried_statement.getTargetVariableRef().getVariable(),
 
3011
        emit          = emit,
 
3012
        needs_release = None,
 
3013
        context       = context
3017
3014
    )
3018
3015
 
3019
3016
    context.setCurrentSourceCodeReference(old_source_ref)
3097
3094
    # The try/finally is very hard for C-ish code generation. We need to react
3098
3095
    # on break, continue, return, raise in the tried blocks with reraise. We
3099
3096
    # need to publish it to the handler (Python3) or save it for re-raise,
3100
 
    # unless another exception or continue, break, return occurs.
 
3097
    # unless another exception or continue, break, return occurs. So this is
 
3098
    # full of detail stuff, pylint: disable=R0914,R0912,R0915
3101
3099
 
3102
3100
    # First, this may be used as an expression, in which case to_name won't be
3103
3101
    # set, we ask the checks to ignore currently set values.
3104
 
    global _temp_whitelist
3105
 
 
3106
3102
    if to_name is not None:
3107
3103
        _temp_whitelist.append(context.getCleanupTempnames())
3108
3104
 
3408
3404
if ( %(keeper_type)s )
3409
3405
{
3410
3406
    NORMALIZE_EXCEPTION( &%(keeper_type)s, &%(keeper_value)s, &%(keeper_tb)s );
3411
 
    PyException_SetContext( %(keeper_value)s, exception_value );
 
3407
    if( exception_value != %(keeper_value)s )
 
3408
    {
 
3409
        PyException_SetContext( %(keeper_value)s, exception_value );
 
3410
    }
 
3411
    else
 
3412
    {
 
3413
        Py_DECREF( exception_value );
 
3414
    }
3412
3415
    Py_DECREF( exception_type );
3413
3416
    exception_type = %(keeper_type)s;
3414
 
    // Py_XDECREF( exception_value );
3415
3417
    exception_value = %(keeper_value)s;
3416
3418
    Py_XDECREF( exception_tb );
3417
3419
    exception_tb = %(keeper_tb)s;
3418
 
 
3419
 
 
3420
3420
}
3421
3421
""" % {
3422
3422
                        "keeper_type"  : keeper_type,
3906
3906
    Generator.getGotoCode(context.getReturnTarget(), emit)
3907
3907
 
3908
3908
 
3909
 
def generateAssignmentVariableCode(variable_ref, value, emit, context):
 
3909
def generateAssignmentVariableCode(statement, emit, context):
 
3910
    variable_ref  = statement.getTargetVariableRef()
 
3911
    value         = statement.getAssignSource()
 
3912
 
3910
3913
    tmp_name = context.allocateTempName("assign_source")
3911
3914
 
3912
3915
    generateExpressionCode(
3917
3920
    )
3918
3921
 
3919
3922
    Generator.getVariableAssignmentCode(
3920
 
        tmp_name = tmp_name,
3921
 
        variable = variable_ref.getVariable(),
3922
 
        emit     = emit,
3923
 
        context  = context
 
3923
        tmp_name      = tmp_name,
 
3924
        variable      = variable_ref.getVariable(),
 
3925
        needs_release = statement.needsReleaseValue(),
 
3926
        emit          = emit,
 
3927
        context       = context
3924
3928
    )
3925
3929
 
3926
 
    if context.needsCleanup(tmp_name):
3927
 
        context.removeCleanupTempName(tmp_name)
 
3930
    # Ownership of that reference should be transfered.
 
3931
    assert not context.needsCleanup(tmp_name)
3928
3932
 
3929
3933
 
3930
3934
def generateStatementOnlyCode(value, emit, context):
4017
4021
 
4018
4022
    if statement.isStatementAssignmentVariable():
4019
4023
        generateAssignmentVariableCode(
4020
 
            variable_ref  = statement.getTargetVariableRef(),
4021
 
            value         = statement.getAssignSource(),
4022
 
            emit          = emit,
4023
 
            context       = context
 
4024
            statement = statement,
 
4025
            emit      = emit,
 
4026
            context   = context
4024
4027
        )
4025
4028
    elif statement.isStatementAssignmentAttribute():
4026
4029
        generateAssignmentAttributeCode(
4239
4242
            emit(
4240
4243
                """PyException_SetTraceback( exception_value, (PyObject *)exception_tb );"""
4241
4244
            )
 
4245
 
4242
4246
        emit(
4243
4247
            "PUBLISH_EXCEPTION( &exception_type, &exception_value, &exception_tb );"
4244
4248
        )
4315
4319
def generateStatementSequenceCode(statement_sequence, context,
4316
4320
                                  allow_none = False):
4317
4321
 
 
4322
    # This is a wrapper that provides also handling of frames, which got a
 
4323
    # lot of variants and details, therefore lots of branches and code.
 
4324
    # pylint: disable=R0912,R0915
 
4325
 
4318
4326
    if allow_none and statement_sequence is None:
4319
4327
        return None
4320
4328
 
4450
4458
 
4451
4459
 
4452
4460
def prepareModuleCode(global_context, module, module_name, other_modules):
 
4461
    # As this not only creates all modules, but also functions, it deals
 
4462
    # with too many details, pylint: disable=R0914
 
4463
 
4453
4464
    assert module.isPythonModule(), module
4454
4465
 
4455
4466
    context = Contexts.PythonModuleContext(
 
4467
        module         = module,
4456
4468
        module_name    = module_name,
4457
4469
        code_name      = Generator.getModuleIdentifier(module_name),
4458
4470
        filename       = module.getFilename(),