Mercurial > lcfOS
annotate python/ide.py @ 258:04c19282a5aa
Added register allocator
author | Windel Bouwman |
---|---|
date | Mon, 05 Aug 2013 19:46:11 +0200 |
parents | 225f444019b1 |
children | ac603eb66b63 |
rev | line source |
---|---|
99 | 1 #!/usr/bin/python |
2 | |
248 | 3 import sys |
4 import os | |
255 | 5 import logging |
64 | 6 |
7 from PyQt4.QtCore import * | |
8 from PyQt4.QtGui import * | |
1 | 9 |
10 # Compiler imports: | |
106 | 11 import ppci |
100 | 12 from astviewer import AstViewer |
162 | 13 from codeedit import CodeEdit |
131 | 14 stutil = __import__('st-util') |
165 | 15 import c3 |
249 | 16 import zcc |
17 import outstream | |
64 | 18 |
19 class BuildOutput(QTextEdit): | |
248 | 20 """ Build output component """ |
21 def __init__(self, parent=None): | |
255 | 22 super(BuildOutput, self).__init__(parent) |
258 | 23 fmt = logging.Formatter(fmt=zcc.logformat) |
255 | 24 class MyHandler(logging.Handler): |
25 def emit(self2, x): | |
26 self.append(str(fmt.format(x))) | |
27 | |
28 logging.getLogger().addHandler(MyHandler()) | |
29 self.setCurrentFont(QFont('Courier')) | |
30 self.setReadOnly(True) | |
31 logging.info('Build output will appear here!') | |
64 | 32 |
248 | 33 |
167 | 34 class BuildErrors(QTreeView): |
248 | 35 sigErrorSelected = pyqtSignal(object) |
36 def __init__(self, parent=None): | |
37 super(BuildErrors, self).__init__(parent) | |
38 model = QStandardItemModel() | |
39 self.setModel(model) | |
40 self.clicked.connect(self.itemSelected) | |
41 self.errorIcon = QIcon('icons/error.png') | |
42 self.model = QStandardItemModel() | |
43 self.model.setHorizontalHeaderLabels(['Message', 'Row', 'Column']) | |
44 self.setModel(self.model) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
45 |
248 | 46 def setErrorList(self, errorlist): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
47 c = self.model.rowCount() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
48 self.model.removeRows(0, c) |
64 | 49 for e in errorlist: |
167 | 50 item = QStandardItem(self.errorIcon, str(e.msg)) |
64 | 51 item.setData(e) |
167 | 52 irow = QStandardItem(str(e.loc.row)) |
53 irow.setData(e) | |
54 icol = QStandardItem(str(e.loc.col)) | |
55 icol.setData(e) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
56 self.model.appendRow([item, irow, icol]) |
248 | 57 |
58 def itemSelected(self, index): | |
256 | 59 if not index.isValid(): |
60 return | |
61 item = self.model.itemFromIndex(index) | |
62 err = item.data() | |
63 self.sigErrorSelected.emit(err) | |
64 | 64 |
65 | |
66 class AboutDialog(QDialog): | |
248 | 67 def __init__(self, parent=None): |
68 super(AboutDialog, self).__init__(parent) | |
69 self.setWindowTitle('About') | |
70 l = QVBoxLayout(self) | |
71 txt = QTextEdit(self) | |
72 txt.setReadOnly(True) | |
73 aboutText = """<h1>lcfOS IDE</h1> | |
74 <p>An all-in-one IDE for OS development.</p> | |
75 <p>https://www.assembla.com/spaces/lcfOS/wiki</p> | |
76 <p>Author: Windel Bouwman</p> | |
77 """ | |
78 txt.append(aboutText) | |
79 l.addWidget(txt) | |
80 but = QPushButton('OK') | |
81 but.setDefault(True) | |
82 but.clicked.connect(self.close) | |
83 l.addWidget(but) | |
84 | |
64 | 85 |
86 class Ide(QMainWindow): | |
248 | 87 def __init__(self, parent=None): |
88 super(Ide, self).__init__(parent) | |
256 | 89 self.to_open_files = [] |
255 | 90 |
248 | 91 self.setWindowTitle('LCFOS IDE') |
92 icon = QIcon('icons/logo.png') | |
93 self.setWindowIcon(icon) | |
64 | 94 |
248 | 95 # Create menus: |
96 mb = self.menuBar() | |
97 self.fileMenu = mb.addMenu('File') | |
98 self.viewMenu = mb.addMenu('View') | |
99 self.helpMenu = mb.addMenu('Help') | |
64 | 100 |
248 | 101 # Create mdi area: |
102 self.mdiArea = QMdiArea() | |
103 self.mdiArea.setViewMode(QMdiArea.TabbedView) | |
249 | 104 self.mdiArea.setTabsClosable(True) |
256 | 105 self.mdiArea.setTabsMovable(True) |
248 | 106 self.setCentralWidget(self.mdiArea) |
64 | 107 |
248 | 108 # Create components: |
109 def addComponent(name, widget): | |
110 dw = QDockWidget(name) | |
111 dw.setWidget(widget) | |
112 dw.setObjectName(name) | |
113 self.addDockWidget(Qt.RightDockWidgetArea, dw) | |
114 self.viewMenu.addAction(dw.toggleViewAction()) | |
115 return widget | |
64 | 116 |
248 | 117 self.buildOutput = addComponent('Build output', BuildOutput()) |
118 self.astViewer = addComponent('AST viewer', AstViewer()) | |
255 | 119 self.astViewer.sigNodeSelected.connect(lambda node: self.showLoc(node.loc)) |
248 | 120 self.builderrors = addComponent('Build errors', BuildErrors()) |
255 | 121 self.builderrors.sigErrorSelected.connect(lambda err: self.showLoc(err.loc)) |
248 | 122 self.devxplr = addComponent('Device explorer', stutil.DeviceExplorer()) |
123 self.regview = addComponent('Registers', stutil.RegisterView()) | |
124 self.memview = addComponent('Memory', stutil.MemoryView()) | |
125 self.ctrlToolbar = stutil.DebugToolbar() | |
126 self.addToolBar(self.ctrlToolbar) | |
127 self.ctrlToolbar.setObjectName('debugToolbar') | |
128 self.devxplr.deviceSelected.connect(self.regview.mdl.setDevice) | |
129 self.ctrlToolbar.statusChange.connect(self.memview.refresh) | |
130 self.devxplr.deviceSelected.connect(self.memview.setDevice) | |
131 self.devxplr.deviceSelected.connect(self.ctrlToolbar.setDevice) | |
132 self.ctrlToolbar.statusChange.connect(self.regview.refresh) | |
133 self.ctrlToolbar.codePosition.connect(self.pointCode) | |
64 | 134 |
248 | 135 # About dialog: |
136 self.aboutDialog = AboutDialog() | |
137 self.aboutDialog.setWindowIcon(icon) | |
138 # Create actions: | |
139 def addMenuEntry(name, menu, callback, shortcut=None): | |
140 a = QAction(name, self) | |
141 menu.addAction(a) | |
142 a.triggered.connect(callback) | |
143 if shortcut: | |
144 a.setShortcut(shortcut) | |
163 | 145 |
248 | 146 addMenuEntry("New", self.fileMenu, self.newFile, shortcut=QKeySequence(QKeySequence.New)) |
147 addMenuEntry("Open", self.fileMenu, self.openFile, shortcut=QKeySequence(QKeySequence.Open)) | |
148 addMenuEntry("Save", self.fileMenu, self.saveFile, shortcut=QKeySequence(QKeySequence.Save)) | |
149 addMenuEntry("Build", self.fileMenu, self.buildFile, shortcut=QKeySequence("F7")) | |
256 | 150 addMenuEntry("Build and flash", self.fileMenu, self.buildFileAndFlash, shortcut=QKeySequence("F8")) |
152 | 151 |
248 | 152 self.helpAction = QAction('Help', self) |
153 self.helpAction.setShortcut(QKeySequence('F1')) | |
154 self.helpMenu.addAction(self.helpAction) | |
155 addMenuEntry('About', self.helpMenu, self.aboutDialog.open) | |
156 | |
157 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows) | |
158 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows) | |
159 sb = self.statusBar() | |
247 | 160 |
248 | 161 # Load settings: |
162 self.settings = QSettings('windelsoft', 'lcfoside') | |
163 self.loadSettings() | |
164 self.diag = ppci.DiagnosticsManager() | |
165 self.c3front = c3.Builder(self.diag) | |
166 | |
167 # File handling: | |
168 def newFile(self): | |
169 self.newCodeEdit() | |
247 | 170 |
248 | 171 def openFile(self): |
172 filename = QFileDialog.getOpenFileName(self, "Open C3 file...", "*.c3", | |
173 "C3 source files (*.c3)") | |
174 if filename: | |
175 self.loadFile(filename) | |
64 | 176 |
248 | 177 def saveFile(self): |
178 ac = self.activeMdiChild() | |
179 if ac: | |
180 ac.save() | |
101 | 181 |
248 | 182 def loadFile(self, filename): |
183 ce = self.newCodeEdit() | |
184 try: | |
185 with open(filename) as f: | |
186 ce.Source = f.read() | |
187 ce.FileName = filename | |
188 except Exception as e: | |
189 print('exception opening file', e) | |
64 | 190 |
248 | 191 # MDI: |
192 def newCodeEdit(self): | |
193 ce = CodeEdit() | |
249 | 194 ce.textChanged.connect(self.parseFile) |
248 | 195 w = self.mdiArea.addSubWindow(ce) |
249 | 196 self.mdiArea.setActiveSubWindow(w) |
197 ce.showMaximized() | |
248 | 198 return ce |
199 | |
200 def activeMdiChild(self): | |
201 aw = self.mdiArea.activeSubWindow() | |
202 if aw: | |
203 return aw.widget() | |
204 | |
205 def findMdiChild(self, filename): | |
206 for wid in self.allChildren(): | |
207 if wid.filename == filename: | |
208 return wid | |
64 | 209 |
248 | 210 def allChildren(self): |
211 return [w.widget() for w in self.mdiArea.subWindowList()] | |
212 | |
213 # Settings: | |
214 def loadSettings(self): | |
215 if self.settings.contains('mainwindowstate'): | |
216 self.restoreState(self.settings.value('mainwindowstate')) | |
217 if self.settings.contains('mainwindowgeometry'): | |
218 self.restoreGeometry(self.settings.value('mainwindowgeometry')) | |
219 if self.settings.contains('lastfiles'): | |
220 lfs = self.settings.value('lastfiles') | |
256 | 221 self.to_open_files.extend(lfs) |
222 | |
223 def showEvent(self, ev): | |
224 super().showEvent(ev) | |
225 while self.to_open_files: | |
226 fn = self.to_open_files.pop(0) | |
227 self.loadFile(fn) | |
64 | 228 |
248 | 229 def closeEvent(self, ev): |
230 self.settings.setValue('mainwindowstate', self.saveState()) | |
231 self.settings.setValue('mainwindowgeometry', self.saveGeometry()) | |
232 ac = self.activeMdiChild() | |
233 lfs = [ce.FileName for ce in self.allChildren() if ce.FileName] | |
234 self.settings.setValue('lastfiles', lfs) | |
235 ev.accept() | |
236 | |
237 # Error handling: | |
238 def nodeSelected(self, node): | |
239 self.showLoc(node.loc) | |
240 | |
241 def showLoc(self, loc): | |
242 ce = self.activeMdiChild() | |
243 if not ce: | |
244 return | |
245 if loc: | |
246 ce.setRowCol(loc.row, loc.col) | |
247 ce.setFocus() | |
152 | 248 else: |
248 | 249 ce.clearErrors() |
64 | 250 |
248 | 251 def pointCode(self, p): |
249 | 252 # Lookup pc in debug infos: |
253 loc = None | |
254 if hasattr(self, 'debugInfo'): | |
255 for di in self.debugInfo: | |
256 if di.address > p: | |
257 loc = di.info | |
258 break | |
259 if loc: | |
260 ce = self.activeMdiChild() | |
261 if ce: | |
262 ce.ic.arrow = loc | |
263 self.showLoc(loc) | |
247 | 264 |
248 | 265 # Build recepy: |
249 | 266 def parseFile(self): |
248 | 267 ce = self.activeMdiChild() |
268 if not ce: | |
269 return | |
270 self.diag.clear() | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
250
diff
changeset
|
271 pkg = self.c3front.parse(ce.Source) |
64 | 272 |
248 | 273 # Set errors: |
274 self.builderrors.setErrorList(self.diag.diags) | |
275 ce.setErrors(self.diag.diags) | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
250
diff
changeset
|
276 self.astViewer.setAst(pkg) |
255 | 277 logging.info('Done!') |
64 | 278 |
249 | 279 def buildFile(self): |
280 ce = self.activeMdiChild() | |
281 if not ce: | |
282 return | |
283 self.diag.clear() | |
284 outs = outstream.TextOutputStream() | |
285 if not zcc.zcc(ce.Source, outs, self.diag): | |
286 # Set errors: | |
287 self.builderrors.setErrorList(self.diag.diags) | |
288 ce.setErrors(self.diag.diags) | |
289 return | |
290 | |
256 | 291 def buildFileAndFlash(self): |
292 ce = self.activeMdiChild() | |
293 if not ce: | |
294 return | |
295 self.diag.clear() | |
296 outs = outstream.TextOutputStream() | |
297 if not zcc.zcc(ce.Source, outs, self.diag, do_optimize=True): | |
298 # Set errors: | |
299 self.builderrors.setErrorList(self.diag.diags) | |
300 ce.setErrors(self.diag.diags) | |
301 return | |
302 | |
249 | 303 code_s = outs.getSection('code') |
304 self.debugInfo = code_s.debugInfos() | |
250 | 305 if self.ctrlToolbar.device: |
255 | 306 logging.info('Flashing stm32f4 discovery') |
250 | 307 bts = code_s.to_bytes() |
308 self.ctrlToolbar.device.writeFlash(0x08000000, bts) | |
309 stl = self.ctrlToolbar.device.iface | |
310 stl.reset() | |
311 stl.halt() | |
256 | 312 stl.run() |
255 | 313 logging.info('Done!') |
249 | 314 |
256 | 315 |
1 | 316 if __name__ == '__main__': |
258 | 317 logging.basicConfig(format=zcc.logformat, level=logging.DEBUG) |
240 | 318 app = QApplication(sys.argv) |
319 ide = Ide() | |
320 ide.show() | |
255 | 321 logging.info('IDE started') |
240 | 322 app.exec_() |
1 | 323 |