1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#!/usr/bin/perl -w
#
# $Id: preasm_proc.pl,v 1.3 2010-01-13 16:22:55 vrsieh Exp $
#
# Copyright (C) 2005-2009 FAUmachine Team <info@faumachine.org>.
# This program is free software. You can redistribute it and/or modify it
# under the terms of the GNU General Public License, either version 2 of
# the License, or (at your option) any later version. See COPYING.
sub check_section_spec
{
my ($section);
my ($line);
($line) = @_;
# ".section" keyword
if ($line =~ /^\s*\.section\b\s*([\.\w]*)/) {
$section = $1;
# ".text" keyword
} elsif ($line =~ /^\s*\.text\s*/) {
$section = ".text";
# ".data" keyword
} elsif ($line =~ /^\s*\.data\s*/) {;
$section = ".data";
# No section specifier
} else {
$section = undef;
}
return $section;
}
sub ext_sym_name
{
my ($in_file);
my ($label);
($in_file, $label) = @_;
my ($return);
$module = $in_file;
$module =~ s/\..*$/\_o/;
$module =~ s/\//_/g;
$module =~ s/\-/_/g;
$return = "${LOC_FUNC_PREFIX}${module}_${label}";
}
sub change_label
{
# SEG_/OFF_ prefix and generated variable from faucc
$_ = $_[0];
if (($_ =~ /.*SEG__.*/ or $_ =~ /.*OFF__.*/) ) {
$varlen = length($_);
$varstart = index($_, _,0) + 1;
$str0 = substr($_, 0, $varstart);
$str1 = substr($_, $varstart, $varlen);
if ($DEBUG) {
print "$str0+$str1\n";
}
$_ = "$str0${LOC_FUNC_PREFIX}${module}_$str1";
}
return $_;
}
$PROGRAM = "preasm_proc.pl";
$GEN_HEAD = "\t/* Generated by $PROGRAM */ \n";
$GEN_TAIL = "\t/* End of $PROGRAM */ \n";
$LOC_FUNC_PREFIX = "__LOCAL_";
$DEBUG = 0;
$in_file = $ARGV[0];
$out_file = $ARGV[1];
$as_input = $ARGV[2];
my %local_labels = ();
open(IN, $in_file)
|| die "$PROGRAM: unable to open input file \"$in_file\" ($!)";
my ($cur_sect);
$cur_sect = ".text";
# First pass: collect necessary data
while (defined($line = <IN>)) {
if ($line =~ /\s*(.*):.*/) {
$local_labels{$1} = "defined";
}
}
# Second pass: process it
seek(IN, 0, 0);
open(OUT, ">$out_file")
|| die "$PROGRAM: unable to open output file \"$out_file\" ($!)";
$code_size = 32;
while (defined($line = <IN>)) {
# Detect 32-bit code
if ($line =~ /\s*\.code32\b\s*/) {
print OUT "/* $PROGRAM: 32-bit code */\n";
$code_size = 32;
} elsif ($line =~ /\s*\.code16(|gcc)\b\s*/) {
if ($code_size eq 32) {
$code_size = 16;
print OUT "/* $PROGRAM: End of 32-bit code */\n";
}
}
if ($line =~ /\s*ex_lcall\b\s*\b([\w\.]*)(;|\s)*.*/) {
$tar_label = $1;
$tar_type = undef;
if (grep($tar_label eq $_, keys %local_labels)) {
$tar_ref = ext_sym_name($in_file, $tar_label);
$tar_type= "local";
} else {
$tar_ref = $tar_label;
$tar_type = "external";
}
if ($DEBUG) {
print OUT $GEN_HEAD;
print OUT "\t/* call to $tar_type label $tar_label ($tar_ref) */ \n";
}
print OUT "\t.byte 0x9a \n";
print OUT "\t.word OFF_$tar_ref, SEG_$tar_ref\n";
if ($DEBUG) {
print OUT $GEN_TAIL;
}
}elsif ($line =~ /\s*ex_ljmp\b\s*\b([\w\.]*)(;|\s)*.*/) {
$tar_label = $1;
$tar_type = undef;
$tar_ref = undef;
if (grep($tar_label eq $_, keys %local_labels)) {
$tar_type = "local (diff. section)";
$tar_ref = ext_sym_name($in_file, $tar_label);
} else {
$tar_ref = $tar_label;
$tar_type = "external";
}
if ($DEBUG) {
print OUT $GEN_HEAD;
print OUT "\t/* jump to ${tar_type} label ${tar_label} ref: $tar_label */ \n";
}
print OUT "\t.byte 0xea \n";
print OUT "\t.word OFF_$tar_ref, SEG_$tar_ref \n";
if ($DEBUG) {
print OUT $GEN_TAIL;
}
} elsif ($line =~ /^\s*([a-zA-Z_\.][\.\w]*):.*$/) {
$label = $1;
if ($as_input == 0) {
if ($line =~ /^\s*([a-zA-Z\.][\.\w]*):.*$/) {
$nline = substr($line, 0, length($line)-2);
print OUT "\t.globl $nline\n";
}
}
print OUT $line;
if ($DEBUG) {
print OUT $GEN_HEAD, "\t/* export all labels */ \n";
}
$new_sym_name = ext_sym_name($in_file, $label);
print OUT "\t.globl $new_sym_name \n";
print OUT "$new_sym_name: \n";
} else {
if ($as_input == 0) {
$line = change_label($line);
}
print OUT "$line\n";
}
}
close(IN)
|| die "$PROGRAM: unable to close input file \"$in_file\" ($!)";
close(OUT)
|| die "$PROGRAM: unable to close output file \"$out_file\" ($!)";
|