VIC™

Developing the compiler

Created by Vikas N Kumar / Selective Intellect

Follow us on Twitter at @_vicash_ / @selectintellect

© 2014. Selective Intellect LLC. All Rights Reserved.

What is needed ?

  • Perl >= 5.8.8
  • Pegex for grammar parsing
  • Moo for backend code generation management
  • Alien::gputils for assembly compiling to .hex
  • gpsim for Simulation

Parsing Expression Grammars

  • Created in 2004 by Bryan Ford
  • Formal language defined as rules
  • Each rule is a collection of atoms
  • Each atom is a regular expression
  • Looks like Context Free Grammar but isn't
  • Recursive Descent Parser: first match is used

Writing Grammar Using Pegex

  • Created by Ingy, inspired by Perl6
  • Pegex::Grammar sub-class generated by Pegex::Parser
  • Grammar can be a string constant or a file to be parsed
  • Grammar compiled only once by developer
  • Grammar compiled into VIC::Grammar

What about Abstract Syntax Tree ?

  • Pegex::Parser invokes abstract class Pegex::Receiver
  • User creates derived class VIC::Receiver
  • On each rule callback got_$RULE invoked
  • Each of these callbacks are optional
  • User has to define callback final

... Abstract Syntax Tree ...

  • Arguments to callbacks can be arrays of parsed tokens
  • In the user's callbacks, user can handle each token
  • Tokens not handled are returned in the parent rules
  • AST is created by user in the sub-class
  • In our case, AST is a hash in VIC::Receiver
  • VIC::Grammar and VIC::Receiver use base class Pegex::Base which is just Mo

Intermediate Code Generation

  • Generated appropriately by each callback as needed
  • The AST stores intermediate code
  • In the final callback
    • Checks for Main block
    • Checks for correct chip features
    • Generates assembly code for the appropriate chip
    • Chip features handled by the various VIC::PIC::* classes

Assembly Generation using Moo::Role

  • Chips are complex
  • Some chips are completely identical except for certain features
  • User should not have to track chip features
  • Compiler has all details, so it can do that

... Assembly Generation ...

  • Each feature defined as a Moo::Role
  • Examples: UART, PWM, I2C, SPI, GPIO
  • Allows:
    • separation of chip details into separate classes
    • separation of chip implementations into separate classes
    • special implementations based on chip internals
    • compiler to tell user that feature not supported
    • compiler to list chip's features on commandline

... Assembly Generation ...

  • Moo::Role used like an interface in VIC™
  • VIC::PIC::Roles::* is a list of all different roles
  • Implementations of those roles separated into VIC::PIC::Functions::*
  • A chip class defines:
    • Pin specifications as required by the role
    • Calls with on the names of the VIC::PIC::Roles::* and VIC::PIC::Functions::*

... Usage ...


package VIC::PIC::Roles::Timer;
{
    use Moo::Role;

    requires qw(timer_enable timer_disable timer timer_prescaler
    wdt_prescaler timer_pins);
}
                

... Usage ...


package VIC::PIC::Functions::Timer;
use Carp;
use Moo::Role;
sub timer_disable {
    my ($self, $tmr) = @_;
    return unless $self->doesroles(qw(Timer Chip));
    unless (exists $self->timer_pins->{$tmr}) {
        carp "$tmr is not a timer.";
        return;
    }
    unless (exists $self->registers->{OPTION_REG} and
        exists $self->registers->{INTCON}) {
        carp $self->pic->type,
        " does not have the register OPTION_REG/INTCON";
        return;
    }
    return << "...";
\tbanksel INTCON
\tbcf INTCON, T0IE ;; disable only the timer bit
\tbanksel OPTION_REG
\tmovlw B'00001000'
\tmovwf OPTION_REG
\tbanksel $tmr
\tclrf $tmr
...
}
                    

... Usage ...


package VIC::PIC::P12F683;
{
    use Moo;
    extends 'VIC::PIC::Base';
    # role CodeGen
    has type => (is => 'ro', default => 'p12f683');
    has include => (is => 'ro', default => 'p12f683.inc');
    # .... MORE RULE IMPLEMENTATIONS....

    my @rolenames = qw(CodeGen Operators Chip GPIO
                    ADC ISR Timer Operations CCP Comparator);
    my @roles = map (("VIC::PIC::Roles::$_",
                    "VIC::PIC::Functions::$_"),
                     @rolenames);
    with @roles;
}
                    

Integration With gputils

  • Install Alien::gputils
  • Install gpsim
  • VIC™ just finds the executables and uses them
  • User need not manually use them unless they want to

Future Work

  • Add more chips
  • Implement missing features
  • Assembly code optimizations
  • Perfect simulations
  • Examples of applications in Robotics

THE END

BY Vikas N Kumar, Selective Intellect