1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LLVM_LICENSE_HEADER@
5 */
6
7/*
8 *  import_global.c
9 *  libclosure
10 *
11 *  Created by Blaine Garst on 2/25/08.
12 *  Copyright 2008 __MyCompanyName__. All rights reserved.
13 *
14 */
15
16#include "driver.h"
17
18int TestGlobal = 0;
19
20#if __BLOCKS__
21
22int import_global_real(int verbose) {
23    TestGlobal = rand();
24    void (^myClosure)(void) = myClosure = ^ (void) { setGlobalInt(TestGlobal);};
25    TestGlobal += 1000;
26    callVoidVoid(myClosure);
27
28    int globalValue = getGlobalInt();
29    int desiredValue = TestGlobal;
30    if (error_found("import_global_real", globalValue, desiredValue, verbose)) return 1;
31
32    return 0;
33}
34
35#endif __BLOCKS__
36
37// the closure data structure sythesized for the import_byref
38struct import_global_struct {
39  struct Block_basic base;
40};
41
42
43void invoke_import_global(struct import_global_struct *aBlock) {
44  // no return value so just a void invoke
45  // the compound statement rewritten to reference locals via the const copies.
46  {
47    setGlobalInt(TestGlobal);
48   }
49}
50
51int import_global(int verbose) {
52    TestGlobal = rand();
53    struct import_global_struct onStack = {
54        { 0, 0, sizeof(struct import_global_struct),
55            (void (*)(void *))invoke_import_global,
56         }
57    };
58    struct import_global_struct *myClosure = &onStack;
59    TestGlobal += 1000;
60    callVoidVoid(myClosure);
61    int globalValue = getGlobalInt();
62    int desiredValue = TestGlobal;
63    if (error_found("import_global_real", globalValue, desiredValue, verbose)) return 1;
64
65    return 0;
66}
67