summaryrefslogtreecommitdiff
path: root/Rakefile
blob: e04bff11e53639bdfeda72eef4f808ab098206ac (plain)
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
require 'open3'
require 'rake/clean'

CLOBBER.include 'build'

CROSS_GCC = '../riscv32-ilp32d--glibc/bin/riscv32-linux-gcc'
SYSROOT = '../riscv32-ilp32d--glibc/riscv32-buildroot-linux-gnu/sysroot'
QEMU = 'qemu-riscv32'

def assemble_stage(output, compiler, source)
  arguments = [QEMU, '-L', SYSROOT, *compiler]

  puts(arguments * ' ')
  puts
  Open3.popen2(*arguments) do |qemu_in, qemu_out|
    qemu_in.write File.read(*source)
    qemu_in.close

    IO.copy_stream qemu_out, output
    qemu_out.close
  end
end

desc 'Final stage'
task default: ['build/stage2b', 'build/stage2b.s', 'boot/stage2.elna'] do |t|
  exe, previous_output, source = t.prerequisites

  cat_arguments = ['cat', source]
  compiler_arguments = [QEMU, '-L', SYSROOT, exe]
  diff_arguments = ['diff', '-Nur', previous_output, '-']
  Open3.pipeline(cat_arguments, compiler_arguments, diff_arguments)
end

directory 'build'

desc 'Initial stage'
file 'build/stage1' => ['boot/stage1.s', 'boot/common-boot.s', 'build'] do |t|
  source = t.prerequisites.filter { |prerequisite| prerequisite.end_with? '.s' }

  sh CROSS_GCC, '-nostdlib', '-o', t.name, *source
end

file 'build/stage2a.s' => ['build/stage1', 'boot/stage2.elna'] do |t|
  source, exe = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.elna' }

  File.open t.name, 'w' do |output|
    assemble_stage output, exe, source
  end
end

file 'build/stage2a' => ['build/stage2a.s', 'boot/common-boot.s'] do |t|
  sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites
end

file 'build/stage2b.s' => ['build/stage2a', 'boot/stage2.elna'] do |t|
  source, exe = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.elna' }

  File.open t.name, 'w' do |output|
    assemble_stage output, exe, source
  end
end

file 'build/stage2b' => ['build/stage2b.s', 'boot/common-boot.s'] do |t|
  sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites
end

desc 'Print remaining lines to rewrite'
task :statistics do
  def is_false_positive(word)
    word.start_with?('(*') ||
      word.start_with?('*)') ||
      ('A'..'Z').include?(word[0]) ||
      /^[[:alpha:]][[:digit:]]$/.match(word)
  end

  lines = File.read('boot/stage2.elna')
    .split("\n")
    .select { |line| line.start_with? "\t" }
    .map { |line| line.delete_prefix("\t").split(' ') }
    .reject { |words| is_false_positive(words.first) }
    .group_by do |words|
      if words.first.length < 5
        case words.first
        when 'goto'
          'Statements'
        else
          words.first
        end
      else
        'Statements'
      end
    end

  lines.each do |key, value|
    puts "#{key}: #{value.count}"
  end
end