changeset 315:084cccaa5deb

Added console and screen
author Windel Bouwman
date Sat, 21 Dec 2013 10:03:01 +0100
parents 38f5f298ce0e
children 56e6ff84f646
files kernel/make.py python/asm.py python/ppci/c3/astnodes.py python/ppci/c3/codegenerator.py python/ppci/c3/lexer.py python/ppci/c3/parser.py python/ppci/c3/visitor.py python/zcc.py test/testzcc.py user/console.c3 user/ipc.c3 user/lib.c3 user/makeuser.py user/screen.c3
diffstat 14 files changed, 157 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/kernel/make.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/kernel/make.py	Sat Dec 21 10:03:01 2013 +0100
@@ -3,16 +3,16 @@
 import sys
 import os
 
-def make_kernel():
+def make_kernel(args=[]):
     import zcc
     arglist = ['memory.c3', 'kernel.c3', 'syscall.c3', 'process.c3']
     arglist += ['schedule.c3', 'arch_arm.c3']
     arglist += ['--target', 'arm']
-    arglist += ['--log', 'debug']
+    arglist += args
 
     args = zcc.parser.parse_args(arglist)
     zcc.main(args)
 
 if __name__ == '__main__':
     sys.path.insert(0, os.path.join('..', 'python'))
-    make_kernel()
+    make_kernel(sys.argv[1:])
--- a/python/asm.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/asm.py	Sat Dec 21 10:03:01 2013 +0100
@@ -58,16 +58,18 @@
 
 
 class Lexer:
-   def __init__(self, src):
-      self.tokens = tokenize(src)
-      self.curTok = self.tokens.__next__()
-   def eat(self):
-      t = self.curTok
-      self.curTok = self.tokens.__next__()
-      return t
-   @property
-   def Peak(self):
-      return self.curTok
+    def __init__(self, src):
+        self.tokens = tokenize(src)
+        self.curTok = self.tokens.__next__()
+
+    def eat(self):
+        t = self.curTok
+        self.curTok = self.tokens.__next__()
+        return t
+
+    @property
+    def Peak(self):
+        return self.curTok
 
 
 class Parser:
--- a/python/ppci/c3/astnodes.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/ppci/c3/astnodes.py	Sat Dec 21 10:03:01 2013 +0100
@@ -354,3 +354,15 @@
 
     def __repr__(self):
         return 'WHILE-statement'
+
+
+class For(Statement):
+    def __init__(self, init, condition, final, statement, loc):
+        super().__init__(loc)
+        self.init = init
+        self.condition = condition
+        self.final = final
+        self.statement = statement
+
+    def __repr__(self):
+        return 'FOR-statement'
--- a/python/ppci/c3/codegenerator.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/ppci/c3/codegenerator.py	Sat Dec 21 10:03:01 2013 +0100
@@ -110,7 +110,7 @@
             lval = self.genExprCode(code.lval)
             rval = self.genExprCode(code.rval)
             if not self.equalTypes(code.lval.typ, code.rval.typ):
-                msg = 'Cannot assign {} to {}'.format(code.lval.typ, code.rval.typ)
+                msg = 'Cannot assign {} to {}'.format(code.rval.typ, code.lval.typ)
                 raise SemanticError(msg, code.loc)
             if not code.lval.lvalue:
                 raise SemanticError('No valid lvalue {}'.format(code.lval), code.lval.loc)
@@ -146,6 +146,18 @@
             self.genCode(code.statement)
             self.emit(ir.Jump(bbtest))
             self.setBlock(te)
+        elif type(code) is ast.For:
+            bbdo = self.newBlock()
+            bbtest = self.newBlock()
+            te = self.newBlock()
+            self.genCode(code.init)
+            self.emit(ir.Jump(bbtest))
+            self.setBlock(bbtest)
+            self.gen_cond_code(code.condition, bbdo, te)
+            self.setBlock(bbdo)
+            self.genCode(code.statement)
+            self.emit(ir.Jump(bbtest))
+            self.setBlock(te)
         else:
             raise NotImplementedError('Unknown stmt {}'.format(code))
 
--- a/python/ppci/c3/lexer.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/ppci/c3/lexer.py	Sat Dec 21 10:03:01 2013 +0100
@@ -8,7 +8,7 @@
 """
 
 keywords = ['and', 'or', 'not', 'true', 'false',
-            'else', 'if', 'while', 'return',
+            'else', 'if', 'while', 'for', 'return',
             'function', 'var', 'type', 'const',
             'struct', 'cast',
             'import', 'module']
@@ -45,7 +45,7 @@
            ('COMMENTS', r'//.*'),
            ('LONGCOMMENTBEGIN', r'\/\*'),
            ('LONGCOMMENTEND', r'\*\/'),
-           ('LEESTEKEN', r'==|->|<<|>>|!=|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<|{|}|&|\^|\|'),
+           ('LEESTEKEN', r'==|->|<<|>>|!=|\+\+|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<|{|}|&|\^|\|'),
            ('STRING', r"'.*?'")
             ]
         tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
--- a/python/ppci/c3/parser.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/ppci/c3/parser.py	Sat Dec 21 10:03:01 2013 +0100
@@ -2,7 +2,7 @@
 from ppci import CompilerError
 from .astnodes import Member, Literal, TypeCast, Unop, Binop
 from .astnodes import Assignment, ExpressionStatement, Compound
-from .astnodes import Return, While, If, Empty
+from .astnodes import Return, While, If, Empty, For
 from .astnodes import FunctionType, Function, FormalParameter
 from .astnodes import StructureType, DefinedType, PointerType
 from .astnodes import Constant, Variable
@@ -204,6 +204,18 @@
         statements = self.Statement()
         return While(condition, statements, loc)
 
+    def parseFor(self):
+        loc = self.Consume('for').loc
+        self.Consume('(')
+        init = self.Statement()
+        self.Consume(';')
+        condition = self.Expression()
+        self.Consume(';')
+        final = self.Statement()
+        self.Consume(')')
+        statements = self.Statement()
+        return For(init, condition, final, statements, loc)
+
     def parseReturn(self):
         loc = self.Consume('return').loc
         if self.Peak == ';':
@@ -226,6 +238,8 @@
             return self.parseIf()
         elif self.Peak == 'while':
             return self.parseWhile()
+        elif self.Peak == 'for':
+            return self.parseFor()
         elif self.Peak == '{':
             return self.parseCompound()
         elif self.hasConsumed(';'):
@@ -347,7 +361,7 @@
 
     def PostFixExpression(self):
         pfe = self.PrimaryExpression()
-        while self.Peak in ['[', '.', '->', '(']:
+        while self.Peak in ['[', '.', '->', '(', '++']:
             if self.hasConsumed('['):
                 raise NotImplementedError('Array not yet implemented')
             elif self.hasConsumed('->'):
@@ -357,6 +371,9 @@
             elif self.hasConsumed('.'):
                 field = self.Consume('ID')
                 pfe = Member(pfe, field.val, field.loc)
+            elif self.Peak == '++':
+                loc = self.Consume('++').loc
+                pfe = Unop('++', pfe, loc)
             elif self.hasConsumed('('):
                 # Function call
                 args = []
--- a/python/ppci/c3/visitor.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/ppci/c3/visitor.py	Sat Dec 21 10:03:01 2013 +0100
@@ -35,6 +35,11 @@
         elif type(node) is While:
             self.do(node.condition)
             self.do(node.statement)
+        elif type(node) is For:
+            self.do(node.init)
+            self.do(node.condition)
+            self.do(node.final)
+            self.do(node.statement)
         elif type(node) is Assignment:
             self.do(node.lval)
             self.do(node.rval)
--- a/python/zcc.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/zcc.py	Sat Dec 21 10:03:01 2013 +0100
@@ -41,7 +41,6 @@
             print('.. code::', file=f)
             print('', file=f)
             AstPrinter().printAst(record.c3_ast, f)
-            #Writer('  ').write(record.c3_ast, f)
             print('', file=f)
             s += '\n' + f.getvalue()
         if hasattr(record, 'ircode'):
@@ -112,22 +111,25 @@
 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
   help='Possible import module', action='append', default=[])
 
-parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
 parser.add_argument('--optimize', action='store_true', help="Optimize")
 parser.add_argument('--target', help="Backend selection",
     choices=targetnames, required=True)
 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
 parser.add_argument('--hexfile', help='Output hexfile',
     type=argparse.FileType('w'))
-parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
+parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])', 
+                    type=logLevel, default='WARN')
+parser.add_argument('--report', help='Specify a file to write the compile report to', 
+            type=argparse.FileType('w'))
 
 
-def zcc(srcs, imps, tg, outs, diag, dumpir=False):
+def zcc(srcs, imps, tg, outs, diag):
     """
+        Compiler driver
         Compile sources into output stream.
         Sources is an iterable of open files.
     """
-    logging.info('Zcc started')
+    logging.info('Zcc started {}'.format(srcs))
     # Front end:
     c3b = Builder(diag, tg)
     cg = CodeGenerator(tg)
@@ -158,17 +160,22 @@
 
 
 def main(args):
-    #logging.getLogger().setLevel(logging.DEBUG)
-    #logging.getLogger().addHandler(RstLogHandler())
-    #fh = logging.FileHandler('log.rst', mode='w')
-    #fh.setFormatter(RstFormatter())
-    #logging.getLogger().addHandler(fh)
+    # Configure some logging:
+    logging.getLogger().setLevel(logging.DEBUG)
+    ch = logging.StreamHandler()
+    ch.setFormatter(logging.Formatter(logformat))
+    ch.setLevel(args.log)
+    logging.getLogger().addHandler(ch)
+    if args.report:
+        fh = logging.StreamHandler(stream=args.report)
+        fh.setFormatter(RstFormatter())
+        logging.getLogger().addHandler(fh)
 
     tg = targets[args.target]
     diag = ppci.DiagnosticsManager()
     outs = outstream.TextOutputStream()
 
-    res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir)
+    res = zcc(args.source, args.imp, tg, outs, diag)
     if not res:
         diag.printErrors()
         return 1
@@ -186,6 +193,10 @@
         hf = HexFile()
         hf.addRegion(0x08000000, code_bytes)
         hf.save(args.hexfile)
+
+    if args.report:
+        logging.getLogger().removeHandler(fh)
+    logging.getLogger().removeHandler(ch)
     return 0
 
 
--- a/test/testzcc.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/test/testzcc.py	Sat Dec 21 10:03:01 2013 +0100
@@ -30,7 +30,6 @@
         arg_list = [basedir]
         arg_list.append('--target')
         arg_list.append('arm')
-        arg_list.append('--dumpir')
         args = zcc.parser.parse_args(arg_list)
         self.assertEqual(0, zcc.main(args))
 
@@ -55,7 +54,7 @@
     def testBurn2(self):
         self.do(['burn2.c3'], ['stm32f4xx.c3'])
 
-    def testComments(self):
+    def testCommentsExample(self):
         self.do(['comments.c3'])
 
     def testCast(self):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/user/console.c3	Sat Dec 21 10:03:01 2013 +0100
@@ -0,0 +1,14 @@
+module console;
+
+import ipc;
+
+
+function void main()
+{
+    while(true)
+    {
+        var ipc.Msg msg;
+        ipc.receive_message(&msg);
+    }
+}
+
--- a/user/ipc.c3	Wed Dec 18 20:22:20 2013 +0100
+++ b/user/ipc.c3	Sat Dec 21 10:03:01 2013 +0100
@@ -3,7 +3,11 @@
 
 type struct {
     int sender;
-    int data;
+    int cmd;
+    int data1;
+    int data2;
+    int data3;
+    int data4;
 } Msg;
 
 const int MSG_SEND=1;
@@ -19,7 +23,7 @@
     kernelTrap(MSG_SEND, 1, 0)
 }
 
-function void RecvMessage(Msg msg)
+function void receive_message(Msg *msg)
 {
     kernelTrap(MSG_RECV, 2, 0);
 }
--- a/user/lib.c3	Wed Dec 18 20:22:20 2013 +0100
+++ b/user/lib.c3	Sat Dec 21 10:03:01 2013 +0100
@@ -1,4 +1,5 @@
 module lib;
+import ipc;
 
 /*
 Runtime library.
@@ -8,6 +9,8 @@
 function void print(int txt)
 {
     // TODO
+    var ipc.Msg msg;
+    ipc.SendMessage(&msg);
 }
 
 
--- a/user/makeuser.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/user/makeuser.py	Sat Dec 21 10:03:01 2013 +0100
@@ -2,16 +2,20 @@
 
 import sys
 import os
-import zcc
 
-def make_user():
-    arglist = ['lib.c3', 'ipc.c3', 'hello.c3']
+def fix(srcs, extra_args):
+    import zcc
+    arglist = srcs
     arglist += ['--target', 'arm']
-    arglist += ['--log', 'debug']
-
+    arglist += extra_args
     args = zcc.parser.parse_args(arglist)
     zcc.main(args)
 
+def make_user(extra_args=[]):
+    fix(['lib.c3', 'ipc.c3', 'hello.c3'], extra_args)
+    fix(['lib.c3', 'ipc.c3', 'screen.c3'], extra_args)
+    fix(['lib.c3', 'ipc.c3', 'console.c3'], extra_args)
+
 if __name__ == '__main__':
     sys.path.insert(0, os.path.join('..', 'python'))
-    make_user()
+    make_user(sys.argv[1:])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/user/screen.c3	Sat Dec 21 10:03:01 2013 +0100
@@ -0,0 +1,35 @@
+module screen;
+
+import ipc;
+
+const int num_cols = 80;
+const int num_rows = 25;
+
+var int* vidmem;
+
+function void clear()
+{
+    var int row, col;
+
+    for (row = 0; row < num_rows; rows++)
+    {
+        for (col = 0; col < num_cols; cols=cols+1)
+        {
+            *vidmem = 0x20;
+        }
+    }
+}
+
+function void main()
+{
+    // Initialize display
+
+    clear();
+
+    while (true)
+    {
+        var ipc.Msg msg;
+        ipc.receive_message(&msg);
+    }
+}
+