Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit af20325a authored by Mathias Agopian's avatar Mathias Agopian
Browse files

Merge commit 'goog/master' into merge_master

parents 7c5e6139 b7c81e99
Loading
Loading
Loading
Loading
+55 −3
Original line number Diff line number Diff line
@@ -33,6 +33,10 @@
#  endif
#endif

#ifndef HAVE_WIN32_PROC
#include <sys/poll.h>
#endif

typedef struct stinfo stinfo;

struct stinfo {
@@ -267,6 +271,54 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1
#define SHELL_COMMAND "/system/bin/sh"
#endif

static void shell_service(int s, void *command)
{
    char    buffer[MAX_PAYLOAD];
    int     fd, ret = 0;
    unsigned count = 0;

    fd = create_subprocess(SHELL_COMMAND, "-c", (char *)command);

    while (1) {
        while (count < sizeof(buffer)) {
#ifndef HAVE_WIN32_PROC
            /* add a 200ms timeout so we don't block indefinitely with our
               buffer partially filled.
            */
            if (count > 0) {
                struct pollfd pollfd;

                pollfd.fd = fd;
                pollfd.events = POLLIN;
                ret = poll(&pollfd, 1, 200);
                if (ret <= 0) {
                    D("poll returned %d\n", ret);
                    // file has closed or we timed out
                    // set ret to 1 so we don't exit the outer loop
                    ret = 1;
                    break;
                }
            }
#endif
            ret = adb_read(fd, buffer + count, sizeof(buffer) - count);
            D("ret: %d, count: %d\n", ret, count);
            if (ret > 0)
                count += ret;
            else
                break;
        }

        D("writing: %d\n", count);
        adb_write(s, buffer, count);
        count = 0;
        if (ret <= 0)
            break;
    }

    adb_close(fd);
    adb_close(s);
}

int service_to_fd(const char *name)
{
    int ret = -1;
@@ -320,7 +372,7 @@ int service_to_fd(const char *name)
#endif
    } else if(!HOST && !strncmp(name, "shell:", 6)) {
        if(name[6]) {
            ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
            ret = create_service_thread(shell_service, (void *)(name + 6));
        } else {
            ret = create_subprocess(SHELL_COMMAND, "-", 0);
        }
+3 −0
Original line number Diff line number Diff line
@@ -69,6 +69,9 @@ void accGetScriptInfoLog(ACCscript* script,
void accGetScriptLabel(ACCscript* script, const ACCchar * name,
                       ACCvoid** address);

void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
                   ACCsizei maxStringCount, ACCchar** strings);

#ifdef __cplusplus
};
#endif
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ int ashmem_create_region(const char *name, size_t size);
int ashmem_set_prot_region(int fd, int prot);
int ashmem_pin_region(int fd, size_t offset, size_t len);
int ashmem_unpin_region(int fd, size_t offset, size_t len);
int ashmem_get_size_region(int fd);

#ifdef __cplusplus
}
+311 −69
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@

#define LOG_API(...) do {} while(0)
// #define LOG_API(...) fprintf (stderr, __VA_ARGS__)

// #define ENABLE_ARM_DISASSEMBLY

namespace acc {
@@ -389,7 +388,7 @@ class Compiler : public ErrorSink {
        /* returns address to patch with local variable size
        */
        virtual int functionEntry(int argCount) {
            LOG_API(stderr, "functionEntry(%d);\n", argCount);
            LOG_API("functionEntry(%d);\n", argCount);
            // sp -> arg4 arg5 ...
            // Push our register-based arguments back on the stack
            if (argCount > 0) {
@@ -1039,16 +1038,35 @@ class Compiler : public ErrorSink {

    class InputStream {
    public:
        int getChar() {
            if (bumpLine) {
                line++;
                bumpLine = false;
            }
            int ch = get();
            if (ch == '\n') {
                bumpLine = true;
            }
            return ch;
        }
        int getLine() {
            return line;
        }
    protected:
        InputStream() :
            line(1), bumpLine(false) {
        }
    private:
        virtual int get() = 0;
        virtual long tell() = 0;
        int line;
        bool bumpLine;
    };

    class FileInputStream : public InputStream {
    public:
        FileInputStream(FILE* in) : f(in) {}
        virtual int get() { return fgetc(f); }
        virtual long tell() { return ftell(f); }
    private:
        virtual int get() { return fgetc(f); }
        FILE* f;
    };

@@ -1057,14 +1075,12 @@ class Compiler : public ErrorSink {
        TextInputStream(const char* text, size_t textLength)
            : pText(text), mTextLength(textLength), mPosition(0) {
        }

    private:
        virtual int get() {
            return mPosition < mTextLength ? pText[mPosition++] : EOF;
        }
        virtual long tell() {
            return mPosition;
        }

    private:
        const char* pText;
        size_t mTextLength;
        size_t mPosition;
@@ -1091,24 +1107,96 @@ class Compiler : public ErrorSink {
    CodeBuf codeBuf;
    CodeGenerator* pGen;

    static const int ERROR_BUF_SIZE = 512;
    char mErrorBuf[ERROR_BUF_SIZE];
    class String {
    public:
        String() {
            mpBase = 0;
            mUsed = 0;
            mSize = 0;
        }

        ~String() {
            if (mpBase) {
                free(mpBase);
            }
        }

        char* getUnwrapped() {
            return mpBase;
        }

        void appendCStr(const char* s) {
            int n = strlen(s);
            memcpy(ensure(n), s, n + 1);
        }

        void append(char c) {
            * ensure(1) = c;
        }

        void printf(const char* fmt,...) {
            va_list ap;
            va_start(ap, fmt);
            vprintf(fmt, ap);
            va_end(ap);
        }

        void vprintf(const char* fmt, va_list ap) {
            char* temp;
            int numChars = vasprintf(&temp, fmt, ap);
            memcpy(ensure(numChars), temp, numChars+1);
            free(temp);
        }

        size_t len() {
            return mUsed;
        }

    private:
        char* ensure(int n) {
            size_t newUsed = mUsed + n;
            if (newUsed > mSize) {
                size_t newSize = mSize * 2 + 10;
                if (newSize < newUsed) {
                    newSize = newUsed;
                }
                mpBase = (char*) realloc(mpBase, newSize + 1);
                mSize = newSize;
            }
            mpBase[newUsed] = '\0';
            char* result = mpBase + mUsed;
            mUsed = newUsed;
            return result;
        }

        char* mpBase;
        size_t mUsed;
        size_t mSize;
    };

    String mErrorBuf;

    jmp_buf mErrorRecoveryJumpBuf;

    String mPragmas;
    int mPragmaStringCount;

    static const int ALLOC_SIZE = 99999;

    /* depends on the init string */
    static const int TOK_STR_SIZE = 48;
    // Indentifiers start at 0x100 and increase by # (chars + 1) * 8
    static const int TOK_IDENT = 0x100;
    static const int TOK_INT = 0x100;
    static const int TOK_IF = 0x120;
    static const int TOK_ELSE = 0x138;
    static const int TOK_WHILE = 0x160;
    static const int TOK_BREAK = 0x190;
    static const int TOK_RETURN = 0x1c0;
    static const int TOK_FOR = 0x1f8;
    static const int TOK_DEFINE = 0x218;
    static const int TOK_MAIN = 0x250;
    static const int TOK_CHAR = TOK_INT + 4*8;
    static const int TOK_VOID = TOK_CHAR + 5*8;
    static const int TOK_IF = TOK_VOID + 5*8;
    static const int TOK_ELSE = TOK_IF + 3*8;
    static const int TOK_WHILE = TOK_ELSE + 5*8;
    static const int TOK_BREAK = TOK_WHILE + 6*8;
    static const int TOK_RETURN = TOK_BREAK + 6*8;
    static const int TOK_FOR = TOK_RETURN + 7*8;
    static const int TOK_PRAGMA = TOK_FOR + 4*8;
    static const int TOK_DEFINE = TOK_PRAGMA + 7*8;
    static const int TOK_MAIN = TOK_DEFINE + 7*8;

    static const int TOK_DUMMY = 1;
    static const int TOK_NUM = 2;
@@ -1168,8 +1256,10 @@ class Compiler : public ErrorSink {
                ch = dch;
            }
        } else
            ch = file->get();
        /*    printf("ch=%c 0x%x\n", ch, ch); */
            ch = file->getChar();
#if 0
        printf("ch='%c' 0x%x\n", ch, ch);
#endif
    }

    int isid() {
@@ -1197,14 +1287,18 @@ class Compiler : public ErrorSink {
                    pdef(TAG_TOK); /* fill last ident tag */
                    *(int *) tok = SYM_DEFINE;
                    *(char* *) (tok + 4) = dstk; /* define stack */
                }
                /* well we always save the values ! */
                    while (ch != '\n') {
                        pdef(ch);
                        inp();
                    }
                    pdef(ch);
                    pdef(TAG_MACRO);
                } else if (tok == TOK_PRAGMA) {
                    doPragma();
                } else {
                    error("Unsupported preprocessor directive \"%s\"", last_id);
                }

            }
            inp();
        }
@@ -1321,23 +1415,54 @@ class Compiler : public ErrorSink {
#endif
    }

    void doPragma() {
        // # pragma name(val)
        int state = 0;
        while(ch != EOF && ch != '\n' && state < 10) {
            switch(state) {
                case 0:
                    if (isspace(ch)) {
                        inp();
                    } else {
                        state++;
                    }
                    break;
                case 1:
                    if (isalnum(ch)) {
                        mPragmas.append(ch);
                        inp();
                    } else if (ch == '(') {
                        mPragmas.append(0);
                        inp();
                        state++;
                    } else {
                        state = 11;
                    }
                    break;
                case 2:
                    if (isalnum(ch)) {
                        mPragmas.append(ch);
                        inp();
                    } else if (ch == ')') {
                        mPragmas.append(0);
                        inp();
                        state = 10;
                    } else {
                        state = 11;
                    }
                    break;
            }
        }
        if(state != 10) {
            error("Unexpected pragma syntax");
        }
        mPragmaStringCount += 2;
    }

    virtual void verror(const char* fmt, va_list ap) {
        char* pBase = mErrorBuf;
        int bytesLeft = sizeof(mErrorBuf);
        int bytesAdded = snprintf(pBase, bytesLeft, "%ld: ", file->tell());
        bytesLeft -= bytesAdded;
        pBase += bytesAdded;
        if (bytesLeft > 0) {
            bytesAdded = vsnprintf(pBase, bytesLeft, fmt, ap);
            bytesLeft -= bytesAdded;
            pBase += bytesAdded;
        }
        if (bytesLeft > 0) {
            bytesAdded = snprintf(pBase, bytesLeft, "\n");
            bytesLeft -= bytesAdded;
            pBase += bytesAdded;
        }
        mErrorBuf.printf("%ld: ", file->getLine());
        mErrorBuf.vprintf(fmt, ap);
        mErrorBuf.printf("\n");
        longjmp(mErrorRecoveryJumpBuf, 1);
    }

@@ -1563,7 +1688,7 @@ class Compiler : public ErrorSink {
        } else if (tok == '{') {
            next();
            /* declarations */
            decl(1);
            localDeclarations();
            while (tok != '}')
                block(l);
            next();
@@ -1582,36 +1707,125 @@ class Compiler : public ErrorSink {
        }
    }

    /* 'l' is true if local declarations */
    void decl(bool l) {
        intptr_t a;
    typedef int Type;
    static const Type TY_UNKNOWN = 0;
    static const Type TY_INT = 1;
    static const Type TY_CHAR = 2;
    static const Type TY_VOID = 3;
    static const int TY_BASE_TYPE_MASK = 0xf;
    static const int TY_INDIRECTION_MASK = 0xf0;
    static const int TY_INDIRECTION_SHIFT = 4;
    static const int MAX_INDIRECTION_COUNT = 15;

    Type getBaseType(Type t) {
        return t & TY_BASE_TYPE_MASK;
    }

    int getIndirectionCount(Type t) {
        return (TY_INDIRECTION_MASK & t) >> TY_INDIRECTION_SHIFT;
    }

    void setIndirectionCount(Type& t, int count) {
        t = ((TY_INDIRECTION_MASK & (count << TY_INDIRECTION_SHIFT))
                | (t & ~TY_INDIRECTION_MASK));
    }

        while ((tok == TOK_INT) | ((tok != EOF) & (!l))) {
    bool acceptType(Type& t) {
        t = TY_UNKNOWN;
        if (tok == TOK_INT) {
            t = TY_INT;
        } else if (tok == TOK_CHAR) {
            t = TY_CHAR;
        } else if (tok == TOK_VOID) {
            t = TY_VOID;
        } else {
            return false;
        }
        next();
        return true;
    }

    Type acceptPointerDeclaration(Type& base) {
        Type t = base;
        int indirectionCount = 0;
        while (tok == '*' && indirectionCount <= MAX_INDIRECTION_COUNT) {
            next();
            indirectionCount++;
        }
        if (indirectionCount > MAX_INDIRECTION_COUNT) {
            error("Too many levels of pointer. Max %d", MAX_INDIRECTION_COUNT);
        }
        setIndirectionCount(t, indirectionCount);
        return t;
    }

    void expectType(Type& t) {
        if (!acceptType(t)) {
            error("Expected a type.");
        }
    }

    void checkSymbol() {
        if (tok <= TOK_DEFINE) {
            error("Expected a symbol");
        }
    }

    void localDeclarations() {
        intptr_t a;
        Type base;

        while (acceptType(base)) {
            while (tok != ';') {
                    if (l) {
                Type t = acceptPointerDeclaration(t);
                checkSymbol();
                loc = loc + 4;
                *(int *) tok = -loc;
                    } else {
                        *(int* *) tok = (int*) allocGlobalSpace(4);
                    }

                next();
                if (tok == ',')
                    next();
            }
            skip(';');
        }
    }

    void globalDeclarations() {
        while (tok != EOF) {
            Type base;
            expectType(base);
            Type t = acceptPointerDeclaration(t);
            checkSymbol();
            int name = tok;
            next();
            if (tok == ',' || tok == ';') {
                // it's a variable declaration
                for(;;) {
                    *(int* *) name = (int*) allocGlobalSpace(4);
                    if (tok != ',') {
                        break;
                    }
                    next();
                    t = acceptPointerDeclaration(t);
                    checkSymbol();
                    name = tok;
                    next();
                }
                skip(';');
            } else {
                /* patch forward references (XXX: do not work for function
                /* patch forward references (XXX: does not work for function
                 pointers) */
                pGen->gsym(*(int *) (tok + 4));
                pGen->gsym(*(int *) (name + 4));
                /* put function address */
                *(int *) tok = codeBuf.getPC();
                next();
                *(int *) name = codeBuf.getPC();
                skip('(');
                a = 8;
                intptr_t a = 8;
                int argCount = 0;
                while (tok != ')') {
                    Type aType;
                    expectType(aType);
                    aType = acceptPointerDeclaration(aType);
                    checkSymbol();
                    /* read param name and compute offset */
                    *(int *) tok = a;
                    a = a + 4;
@@ -1620,7 +1834,7 @@ class Compiler : public ErrorSink {
                        next();
                    argCount++;
                }
                next(); /* skip ')' */
                skip(')'); /* skip ')' */
                rsym = loc = 0;
                a = pGen->functionEntry(argCount);
                block(0);
@@ -1680,7 +1894,7 @@ class Compiler : public ErrorSink {
        pGlobalBase = 0;
        pVarsBase = 0;
        pGen = 0;
        mErrorBuf[0] = 0;
        mPragmaStringCount = 0;
    }

    void setArchitecture(const char* architecture) {
@@ -1745,15 +1959,18 @@ public:
            pGen->init(&codeBuf);
            file = new TextInputStream(text, textLength);
            sym_stk = (char*) calloc(1, ALLOC_SIZE);
            dstk = strcpy(sym_stk,
                    " int if else while break return for define main ")
                    + TOK_STR_SIZE;
            static const char* predefinedSymbols =
                " int char void"
                " if else while break return for"
                " pragma define main ";
            dstk = strcpy(sym_stk, predefinedSymbols)
                    + strlen(predefinedSymbols);
            pGlobalBase = (char*) calloc(1, ALLOC_SIZE);
            glo = pGlobalBase;
            pVarsBase = (char*) calloc(1, ALLOC_SIZE);
            inp();
            next();
            decl(0);
            globalDeclarations();
            pGen->finishCompile();
        }
        return result;
@@ -1810,8 +2027,26 @@ public:
        return NULL;
    }

    void getPragmas(ACCsizei* actualStringCount,
                    ACCsizei maxStringCount, ACCchar** strings) {
        int stringCount = mPragmaStringCount;
        if (actualStringCount) {
            *actualStringCount = stringCount;
        }
        if (stringCount > maxStringCount) {
            stringCount = maxStringCount;
        }
        if (strings) {
            char* pPragmas = mPragmas.getUnwrapped();
            while (stringCount-- > 0) {
                *strings++ = pPragmas;
                pPragmas += strlen(pPragmas) + 1;
            }
        }
    }

    char* getErrorMessage() {
        return mErrorBuf;
        return mErrorBuf.getUnwrapped();
    }

};
@@ -1988,5 +2223,12 @@ void accGetScriptLabel(ACCscript* script, const ACCchar * name,
    }
}

extern "C"
void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
                   ACCsizei maxStringCount, ACCchar** strings){
    script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
}


} // namespace acc
+449 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading